【问题标题】:ElasticSearch should/must clause not working as expectedElasticSearch 应该/必须子句未按预期工作
【发布时间】:2017-05-10 10:35:22
【问题描述】:

下面是我的弹性查询

 GET _search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "marriages.marriage_year": "1630"
        }
      },
      "should": {
        "match": {
          "first_name": {
            "query": "mary",
            "fuzziness": "2"
          }
        }
      },
      "must": {
        "range": {
          "marriages.marriage_year": {
            "gt": "1620",
            "lte": "1740"
          }
        }
      }
    }
  }
}

它正在返回带有marriages.marriage_year= "1630" 的数据,其中 Mary 作为 first_name 作为最高分。我还想在 1620 到 1740 之间包含marriages.marriage_year,这些未显示在结果中。它仅显示 marriage_year 1630 的数据

【问题讨论】:

    标签: elasticsearch kibana nest elasticsearch-marvel


    【解决方案1】:

    这是因为您有两个 bool/must 子句,而第二个子句在解析 JSON 查询时被消除。像这样重写它,它会工作:

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "marriages.marriage_year": "1630"
              }
            },
            {
              "range": {
                "marriages.marriage_year": {
                  "gt": "1620",
                  "lte": "1740"
                }
              }
            }
          ],
          "should": {
            "match": {
              "first_name": {
                "query": "mary",
                "fuzziness": "2"
              }
            }
          }
        }
      }
    }
    

    更新

    然后您需要以不同的方式进行操作,在 bool/must 中您只需要使用 range 查询并将 match 移动到 bool/should 部分中:

    {
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "marriages.marriage_year": {
                  "gt": "1620",
                  "lte": "1740"
                }
              }
            }
          ],
          "should": [
            {
              "match": {
                "first_name": {
                  "query": "mary",
                  "fuzziness": "2"
                }
              }
            },
            {
              "match": {
                "marriages.marriage_year": "1630"
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 试过上面的查询它仍然只返回与 1630 相关的婚姻年。
    • 这个很完美。谢谢
    • 太棒了,很高兴它有帮助!
    猜你喜欢
    • 2021-05-20
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多