Kibana 在elasticsearch中不仅获取指定的元素

6psbrbz9  于 2023-09-28  发布在  Kibana
关注(0)|答案(1)|浏览(174)

我有以下文件:

POST colours/_doc/1
{
  "colour": ["green", "red", "blue", "yellow", "pink", "orange", "dark", "bright", "lilac", "turquoise", "gold", "silver"]
}

POST colours/_doc/2
{
  "colour": ["blue", "yellow"]
}

POST colours/_doc/3
{
  "colour": ["green"]
}

我只想获取ID为1和2的对象,因为我不想只获取ID为绿色的对象。我还想计算在这种情况下出现的次数,所以它会返回2。我管理了这个查询:

GET colours/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "colour": ["red",
              "blue",
              "yellow",
              "pink",
              "orange",
              "dark",
              "bright",
              "lilac",
              "turquoise",
              "gold",
              "silver"]
          }
        }
      ]
    }
  }
}

但是,我不想在terms查询中指定所有颜色,因为在我的用例中,数组可以包含数百个值。理想情况下是这样的:

GET colours/_search
{
  "query": {
    "bool": {
      "not_only": [
        {
          "terms": {
            "colour": ["green"]
          }
        }
      ]
    }
  }
}

有什么想法吗?

wydwbb8l

wydwbb8l1#

我们可以这样做:

  • 求条件:查找colour数组,其中只有一个元素green
  • 否定上述条件以得到其他条件

第一个条件是这样的:

"bool": {
    "must": [
        {
            "match": {
                "colour": "green"
            }
        }
    ],
    "filter": {
        "script": {
            "script": "doc['colour.keyword'].length == 1"
        }
    }
}

要否定上面的条件,简单地把它放在另一个bool中,如下所示:

{
    "query": {
        "bool": {
            "must_not": [
                {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "colour": "green"
                                }
                            }
                        ],
                        "filter": {
                            "script": {
                                "script": "doc['colour.keyword'].length == 1"
                            }
                        }
                    }
                }
            ]
        }
    }
}

这取决于colour的Map,我使用colour.keyword是因为我的颜色Map是文本,关键字必须用于脚本。

"properties": {
    "colour": {
        "type": "text",
        "fields": {
            "keyword": {
                "type": "keyword",
                "ignore_above": 256
            }
        }
    }
}

结果是除了["green"]以外的所有内容

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "color",
                "_id": "1",
                "_score": 0.0,
                "_source": {
                    "colour": [
                        "green",
                        "red",
                        "blue",
                        "yellow",
                        "pink",
                        "orange",
                        "dark",
                        "bright",
                        "lilac",
                        "turquoise",
                        "gold",
                        "silver"
                    ]
                }
            },
            {
                "_index": "color",
                "_id": "2",
                "_score": 0.0,
                "_source": {
                    "colour": [
                        "blue",
                        "yellow"
                    ]
                }
            }
        ]
    }
}

相关问题