首页 > 解决方案 > Elasticsearch Query:通过比较值列表来选择文档(golang)

问题描述

我有一种在 ElasticSearch 中被索引的文档,其简化结构如下:

{
    id: "54"
    properties: ["nice", "green", "small", "dry"]
}

现在我想选择这个索引中的所有文档,它包含properties字段中给定值的列表。

就像是:SELECT * FROM index WHERE properties NOT CONTAINS ["red", "big", "scary"]

我如何在弹性搜索上实现它?(而且我有人知道如何在 Golang 上实现这样的查询,我会更好:-))

谢谢!

标签: elasticsearchgo

解决方案


您可以使用该子句从索引中匹配这些文档bool。它看起来像这样:

{
    "bool": {
        "must_not": [
            { "term": { "properties": "red" }},
            { "term": { "properties": "big" }},
            { "term": { "properties": "scary" }}
         ]
    }
}

查询可能是这样的:

{
  "filtered": {
    "query": {
      "match": { "id": "54" }
    },
    "filter":{
      "bool": {
        "must_not": [
            { "term": { "properties": "red" }},
            { "term": { "properties": "big" }},
            { "term": { "properties": "scary" }}
        ]
      }
    }
  }
}

有关更多信息,您可以查看此链接:过滤查询


推荐阅读