【问题标题】:ElasticSearch missing and term queryElasticSearch 缺失和术语查询
【发布时间】:2018-01-21 12:41:18
【问题描述】:

我正在尝试获取缺少字段 "topic.description" 并匹配术语 "fundedUnder.programme": "ABC" 的文档。

映射:

...
"fundedUnder": {
    "properties": {
        "programme": {
            "type": "string"
        },
        "subprogramme": {
            "type": "string"
        }
    }
},
"topics": {
    "type": "nested",
    "include_in_parent": true,
    "properties": {
        "code": {
            "type": "string",
            "analyzer": "analyzer_keyword"
         },
         "description": {
             "type": "string",
             "analyzer": "analyzer_keyword"
         },
         "title": {
             "type": "string",
             "analyzer": "analyzer_keyword"
         }
    }
},
...

我的查询看起来像:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "missing": {
                "field": "topics.description"
              }
            },
            {
                "term": {
                    "fundedUnder.programme" : "ABC" 
                }
            }   
          ]
        }
      }
    }
  }
}

这个查询什么也没找到,那是错误的,因为我在索引中使用了 fundedUnder.programme == "ABC" 和缺少字段 topic.description 的很多文档。

提前致谢。

ElasticSearch 1.7.5 版

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我相信这应该可行:

    编辑:更新为使用 1.7 版查询 DSL

    {
      "query": {
        "filtered": { 
          "query": {
            "match": { "fundedUnder.programme" : "ABC" }
          },
          "filter": {
            "missing": { "field": "topics.description" }
          }
        }
      }
    }
    

    【讨论】:

    • @aaron-m-eshbach 感谢您的回复,但它不起作用 ("SearchPhaseExecutionException") 。我认为这是因为 Elastic 版本。
    • @Joozty,很抱歉,我查看了 1.7 版文档并更新了我的答案。我无法访问 1.7 服务器进行测试,所以我不能 100% 确定,但我认为它应该可以工作。
    • @aaron-m-eshbach 现在查询有效,但与我的查询相同。它什么也不返回。当我删除匹配 fundedUnder.programme 的术语时,它会返回所有缺少字段的文档。
    • @Joozty,您是否尝试过 Match 查询而不是 Term 查询?我再次更新了示例。如果它仍然不起作用,您可以尝试不使用过滤器的 match 或 term 子句,看看您是否至少可以找到您的 programme = 'ABC' 结果?
    • @aaron-m-eshbach 非常感谢。你帮了我很多。
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多