【问题标题】:Elasticsearch: Search fields containing arrays using arraysElasticsearch:使用数组搜索包含数组的字段
【发布时间】:2020-12-18 18:46:37
【问题描述】:

我有一个 ES 索引,其中一个映射存储了一个简单的命名实体数组,该数组在摄取点预先设置。

我正在尝试使用给定的实体数组搜索我的索引,以返回包含许多相同实体的文档。

一些用于说明的代码...

GET /test_data/_search
{  
"query": {
    "match": {
      "entities": ['Trump', 'CNN', 'Oklahoma', 'Tiktok', 'Tulsa']
    }
  }
}

但是,这会返回一个解析异常——使用另一个数组搜索包含数组的字段的最佳方法是什么?

谢谢

【问题讨论】:

    标签: elasticsearch kibana elk


    【解决方案1】:

    如果您正在寻找完全匹配的内容,请将 match 更改为 terms -- 这用作 OR 查询:

    GET /test_data/_search
    {
      "query": {
        "terms": {
          "entities": [
            "Trump",
            "CNN",
            "Oklahoma",
            "Tiktok",
            "Tulsa"
          ]
        }
      }
    }
    

    否则使用 match 查询的 bool-should 数组:

    GET /test_data/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "entities": "Trump"
              }
            },
            {
              "match": {
                "entities": "CNN"
              }
            },
            {
              "match": {
                "entities": "Oklahoma"
              }
            },
            ...
          ]
        }
      }
    }
    

    您可以定义其中有多少应与minimum_should_match 参数匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2015-12-14
      • 2021-11-23
      相关资源
      最近更新 更多