Elasticsearch 仅从 Start 开始搜索



目前,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": {}
}
}
}


如果我的项目名称为">道路- 区域 1"、"道路 - 区域 2"和"子区域 5 - 道路",并且用户搜索"道路",则只会显示"道路- 区域 1"和"道路- 区域 2",并以黄色突出显示">道路"一词。代码也需要选取最终项目。

我似乎已经想通了。 在原始描述中,当我应该使用ngram分词器时,我正在使用edge_ngram分词器。

发现于: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html#_partial_word_tokenizers

最新更新