代码之家  ›  专栏  ›  技术社区  ›  Code_Worm

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

  •  1
  • Code_Worm  · 技术社区  · 7 年前

    我的映射中有一个嵌套类型字段。当我使用 学期 在我的嵌套字段上搜索查询Elasticsearch不会返回任何结果,而当我更改 学期 火柴 查询时,它工作正常,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"
          }
         }
        ]
       }
     }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Green Moshe    7 年前

    之所以会发生这种情况,是因为大写字母与弹性分析。

    当你使用 term 橡皮筋正在寻找你给出的准确值。 直到现在听起来还不错, 但是 在它试图匹配术语之前,你给出的值会经过一个弹性分析器,它会操纵你的值。 例如,在你的情况下,它也将 HasParking hasparking .

    然后它会尝试匹配它,当然会失败。他们在书中有很好的解释 documentation in the "Why doesn’t the term query match my document" 部分当您使用 match 这就是你得到结果的原因。