我使用以下设置和映射:
索引设置:
PUT /my_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
映射:
PUT /my_index/my_type/_mapping
{
"my_type": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
插入记录:
POST /my_index/my_type/_bulk
{ "index": { "_id": 1 }}
{ "name": "men's shaver" }
{ "index": { "_id": 2 }}
{ "name": "women's shaver" }
查询:
1. 通过精确短语匹配搜索 --> "men's"
POST /my_index/my_type/_search
{
"query": {
"match": {
"name": "men's"
}
}
}
上述查询结果会在返回结果中包括"men's shaver"这个词条。
2. 按部分单词匹配搜索 --> "en's"
POST /my_index/my_type/_search
{
"query": {
"match": {
"name": "en's"
}
}
}
上面的查询没有返回任何内容。
我也尝试了以下查询
POST /my_index/my_type/_search
{
"query": {
"wildcard": {
"name": {
"value": "%en's%"
}
}
}
}
仍然没有获取到任何结果。我认为这是由于索引上的“edge_ngram”类型过滤器无法找到“部分单词/子字符串匹配”。我也尝试了“n-gram”类型过滤器,但它会显著减慢搜索速度。
请建议我如何在相同的索引设置下实现精确短语匹配和部分短语匹配。