如何在Elasticsearch中过滤掉不存在的字段?

huangapple go评论112阅读模式
英文:

How to filter out fields that do not exist in elastic search?

问题

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

我尝试了以下代码,但它不起作用:

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

英文:

I would like to check if a field exists, and return results for documents where it does not exist. I am using the Golang library Elastic: https://github.com/olivere/elastic

I tried the following but it does not work:

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

答案1

得分: 12

好的,我不会深入研究你的语言查询 API。由于你想要在一个不存在的字段(null)上进行搜索,你可以在一个 must_not(如果你使用布尔过滤器)内部使用一个 exists 过滤器:

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

希望这能帮到你!

谢谢!

英文:

Ok, I wont go deep in your language query API. Since you want to search on a field not existing (null), use an exists filter inside a must_not (if you use bool filters):

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

Hope this helps!

Thanks

答案2

得分: 10

你可以使用存在查询布尔查询的must_not来实现:

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

在 Elasticsearch 6.5 中测试通过。

英文:

You can use exist query with bool query must_not:

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

Tested in Elasticsearch 6.5

答案3

得分: 4

你可以这样创建一个不存在的布尔查询:

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

英文:

You can create a bool query for not exists like this:

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

答案4

得分: 1

我不会尝试提供一个完整的解决方案,因为我对你使用的库(或者说go语言)并不熟悉。

然而,Lucene不支持纯负面查询,就像你在这里所做的那样。Lucene需要告诉它要匹配什么。像这样的否定只是严格禁止搜索结果,但不会隐式地匹配其他所有内容。

为了实现你想要的效果,你需要使用布尔查询将你的非过滤器与一个匹配所有的条件结合起来(我在库中看到这是可用的)。

注意:使用匹配所有时,性能可能会受到影响。

英文:

I won't try to provide a complete solution, being that I'm not really familiar with the library your using (or, indeed, the go language).

However, Lucene doesn't support pure negative querying as you have here. Lucene needs to be told what to match. Negations like this serve strictly to prohibit search results, but do not implicitly match everything else.

In order to do what you are looking for, you would want to use a boolean query to combine your not filter with a match all (which I see is available in the library).

Note: As with anytime you use a match all, performance may suffer.

huangapple
  • 本文由 发表于 2015年3月31日 06:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/29357148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定