【问题标题】:How to filter out fields that do not exist in elastic search?如何过滤掉弹性搜索中不存在的字段?
【发布时间】:2015-06-04 02:44:23
【问题描述】:

我想检查一个字段是否存在,并为不存在的文档返回结果。我正在使用 Golang 库 Elastic:https://github.com/olivere/elastic

我尝试了以下方法,但它不起作用:

e := elastic.NewExistsFilter("my_tag")
n := elastic.NewNotFilter(e)
filters = append(filters, n)

【问题讨论】:

    标签: search go elasticsearch lucene filtering


    【解决方案1】:

    好的,我不会深入探讨您的语言查询 API。由于您要搜索不存在的字段(空),请在 must_not 内使用 exists 过滤器(如果您使用布尔过滤器):

    {
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "exists": {
                    "field": "your_field"
                  }
                }
              ]
            }
          }
        }
      },
      "from": 0,
      "size": 500
    }
    

    希望这会有所帮助!

    谢谢

    【讨论】:

      【解决方案2】:

      您可以将exist querybool query must_not 一起使用:

      GET /_search
      {
          "query": {
              "bool": {
                  "must_not": {
                      "exists": {
                          "field": "your_field"
                      }
                  }
              }
          }
      }
      

      在 Elasticsearch 6.5 中测试

      【讨论】:

        【解决方案3】:

        您可以像这样为不存在创建一个布尔查询:

        existsQuery := elastic.NewExistsQuery(fieldName)
        existsBoolQuery := elastic.NewBoolQuery().MustNot(existsQuery)
        

        【讨论】:

          【解决方案4】:

          我不会尝试提供完整的解决方案,因为我并不真正熟悉您使用的库(或者实际上是 go 语言)。

          但是,Lucene 并不像您在此处那样支持纯否定查询。 Lucene 需要被告知 to 匹配什么。像这样的否定严格用于禁止搜索结果,但不会隐式匹配其他所有内容。

          为了做您要查找的内容,您可能需要使用布尔查询将您的非过滤器与全部匹配(我看到的是available in the library)结合起来。

          注意:与任何时候使用 match all 一样,性能可能会受到影响。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-09
            • 1970-01-01
            • 2016-04-03
            • 2015-09-18
            • 2012-11-07
            • 2017-04-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多