我的映射中有一个嵌套类型字段。当我在嵌套字段上使用 Term 搜索查询时,Elasticsearch 不会返回任何结果,而当我将 Term 更改为 Match 查询时,它工作正常并且Elasticsearch 返回预期结果
这是我的映射,假设我的类型映射中只有一个嵌套字段
{
"homing.estatefiles": {
"mappings": {
"estatefile": {
"properties": {
"DynamicFields": {
"type": "nested",
"properties": {
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ValueBool": {
"type": "boolean"
},
"ValueDateTime": {
"type": "date"
},
"ValueInt": {
"type": "long"
}
}
}
}
}
}
}
}
这是我的术语查询(不返回结果)
{
"from": 50,
"size": 50,
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"term": {
"DynamicFields.Name":{"value":"HasParking"}
}
},
{
"term": {
"DynamicFields.ValueBool": {
"value": true
}
}
}
]
}
},
"path": "DynamicFields"
}
}
]
}
}
}
这是我的查询,它返回预期结果(通过将术语查询更改为匹配查询)
{
"from": 50,
"size": 50,
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"match": {
"DynamicFields.Name":"HasParking"
}
},
{
"term": {
"DynamicFields.ValueBool": {
"value": true
}
}
}
]
}
},
"path": "DynamicFields"
}
}
]
}
}
}
最佳答案
发生这种情况是因为大写字母带有弹性分析器。
当您使用term
时,弹性元件正在寻找您给出的确切值。
到目前为止,这听起来不错,但是在它尝试匹配该术语之前,您给出的值会通过弹性分析器来操纵您的值。
例如,在您的情况下,它还会将 HasParking
转换为 hasparking
。
然后它会尝试匹配它,当然会失败。他们在documentation in the "Why doesn’t the term query match my document"中有很好的解释。部分。当您使用 match
查询时,该分析器不会在该值上激活,这就是您得到结果的原因。
关于elasticsearch - 嵌套字段上的术语查询在 Elasticsearch 中不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52735054/