我有三个带有“用户名”字段的文档:
- '布里安迪利'
- 'briangumble'
- 'briangriffen'
当我搜索“brian”时,我按预期得到所有三个,但是当我搜索“briandilley”时,我仍然得到所有三个。分析 API 告诉我它在我的搜索字符串上使用了 ngram 过滤器,但我不知道为什么。这是我的设置:
索引设置:
{
"analysis": {
"analyzer": {
"username_index": {
"tokenizer": "keyword",
"filter": ["lowercase", "username_ngram"]
},
"username_search": {
"tokenizer": "keyword",
"filter": ["lowercase"]
}
},
"filter": {
"username_ngram": {
"type": "edgeNGram",
"side" : "front",
"min_gram": 1,
"max_gram": 15
}
}
}
}
映射:
{
"user_follow": {
"properties": {
"targetId": { "type": "string", "store": true },
"followerId": { "type": "string", "store": true },
"dateUpdated": { "type": "date", "store": true },
"userName": {
"type": "multi_field",
"fields": {
"userName": {
"type": "string",
"index": "not_analyzed"
},
"autocomplete": {
"type": "string",
"index_analyzer": "username_index",
"search_analyzer": "username_search"
}
}
}
}
}
}
搜索:
{
"from" : 0,
"size" : 50,
"query" : {
"bool" : {
"must" : [ {
"field" : {
"targetId" : "51888c1b04a6a214e26a4009"
}
}, {
"match" : {
"userName.autocomplete" : {
"query" : "brian",
"type" : "boolean"
}
}
} ]
}
},
"fields" : "followerId"
}
我尝试了 matchQuery、matchPhraseQuery、textQuery 和 termQuery(java DSL api),每次都得到相同的结果。