我有一个对 ElasticSearch 感兴趣的用户列表:
这是我的映射:
mappings: {
users: {
properties: {
username: {
type: 'text',
analyzer: 'autocomplete',
search_analyzer: 'autocomplete'
},
interests: {
type: 'text'
},
locations: {
type: 'text'
},
roles: {
type: 'text'
}
}
}
},
settings: {
analysis: {
analyzer: {
autocomplete: {
type: 'custom',
tokenizer: 'autocomplete',
filter: ['lowercase']
}
},
tokenizer: {
autocomplete: {
type: 'edge_ngram',
min_gram: 3,
max_gram: 10,
token_chars: [
'letter', 'digit'
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例文档:
{
"_index": "users",
"_type": "users",
"_id": "4",
"_score": 1.0,
"_source": {
"username": null,
"interests": [
"live events"
],
"locations": [
"abington",
"connecticut"
],
"roles": [
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在当我搜索:
{
terms: ["live events"]
}
Run Code Online (Sandbox Code Playgroud)
我没有得到任何结果。我查了一下,结果发现如果该术语包含 aspace那么就会发生这种情况。我的猜测是,术语会寻找完全匹配的项,但我无法理解这里发生的情况。
term/terms您应该仅对关键字数据类型使用查询。在您的情况下,您正在查询带有 的字段text datatype,如映射中所写:
interests: {
type: 'text'
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您必须使用匹配查询,因此:
{
"query": {
"match" : {
"interests" : "live events"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3793 次 |
| 最近记录: |