elasticsearch - 嵌套字段上的术语查询在 Elasticsearch 中不返回结果

标签 elasticsearch elasticsearch-query

我的映射中有一个嵌套类型字段。当我在嵌套字段上使用 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/

相关文章:

elasticsearch - ElasticSearch突出显示内部字段

elasticsearch - Elasticsearch Helm chart 上的HTTPS

elasticsearch - 在单个 “and”子句中​​使用多个过滤器和指定多个过滤器之间的区别

elasticsearch - Elasticsearch 路口查询

elasticsearch - 当至少一个对象不包含必要字段时,按对象数组选择文档 Elasticsearch

elasticsearch - Kibana 将@timestamp 字段显示为可读格式

python - 返回属性错误 : 'int' object has no attribute 'encode'

Elasticsearch 聚合 : Sum of latest childrens per parent

elasticsearch - 是否有过滤掉 Elasticsearch 查询响应的选项,例如 _shards?

elasticsearch - 查询时查询上下文和过滤上下文之间的区别