【发布时间】:2018-08-28 09:06:37
【问题描述】:
目前,Elasticsearch 仅从字符串的开头搜索映射项,而不是整个字符串。 我有一个自定义分析器,以及一个自定义边缘 ngram 标记器。 我目前正在使用 javascript 中的 bool 查询来搜索索引。
索引
{
"homestead_dev_index": {
"aliases": {},
"mappings": {
"elasticprojectnode": {
"properties": {
"archived": {
"type": "boolean"
},
"id": {
"type": "text",
"analyzer": "full_name"
},
"name": {
"type": "text",
"analyzer": "full_name"
}
}
}
},
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "homestead_dev_index",
"creation_date": "1535439085947",
"analysis": {
"analyzer": {
"full_name": {
"filter": [
"standard",
"lowercase",
"asciifolding"
],
"type": "custom",
"tokenizer": "mytok"
}
},
"tokenizer": {
"mytok": {
"type": "edge_ngram",
"min_gram": "3",
"max_gram": "10"
}
}
},
"number_of_replicas": "1",
"uuid": "iCa7qKJVRU-_MA8sCYIAXw",
"version": {
"created": "5060399"
}
}
}
}
}
查询正文
{
"query": {
"bool": {
"should": [
{ "match": { "name": this.searchString } },
{ "match": { "id": this.searchString } }
]
}
},
"highlight": {
"pre_tags": ["<b style='background-color:yellow'>"],
"post_tags": ["</b>"],
"fields": {
"name": {},
"id": {}
}
}
}
示例
如果我的项目名称为“Road - Area 1”、“Road - Area 2”和“Sub-area 5 - Road”并且用户搜索“Road”,只有“Road - Area 1”和“Road - Area 2”会显示“ 道路”以黄色突出显示。
代码也需要选择最终项目。
【问题讨论】:
标签: elasticsearch