【问题标题】:Keep the documents with a missing field in the filter query results保留过滤查询结果中缺少字段的文档
【发布时间】:2017-09-26 23:00:43
【问题描述】:

我正在使用以下查询查询我的索引:

获取 /dbpedia201510/entity/_search { “来自”:0, “大小”:10000, “询问”: { “布尔”:{ “必须”: { “请求参数”:{ “字段”:[“名称”,“别名”,“假名”], "query": "Elias~Franck~", “default_operator”:“或”, “模糊”:“自动” } }, “筛选”: { “布尔”:{ “必须”: [ { “条款”:{ "job.keyword":["建筑师","政治家","裁缝"] } } ] } } } } }

job 字段是一个字符串数组,查询按预期工作。不过,有些文档没有job 字段,我也想获取这些文档。

我该怎么做?

【问题讨论】:

    标签: arrays elasticsearch filter


    【解决方案1】:

    您需要在这里使用should clause,当job 字段存在并且匹配术语查询 job 字段不存在时匹配。

    您的最终查询应如下所示:

    GET /dbpedia201510/entity/_search
    {
      "from": 0,
      "size": 10000,
      "query": {
        "bool": {
          "must": {
            "query_string": {
              "fields": ["name","alias","pseudonyme"],
              "query": "Elias~ Franck~",
              "default_operator": "OR",
              "fuzziness": "auto"
            }
          },
          "filter": {
            "bool": {
              "should": [
                {
                  "terms": {
                    "job.keyword": ["Architect","Politician","Tailor"]
                  }
                },
                {
                  "bool": {
                    "must_not": {
                      "exists": {
                        "field": "job"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

    • 很高兴它有帮助。如果它完全解决了您的问题,您可以接受它作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2018-11-23
    相关资源
    最近更新 更多