使用 python 列表的弹性搜索查询

Elastic search query using python list

如何将列表作为查询字符串传递给 match_phrase 查询?

这个有效:

{"match_phrase": {"requestParameters.bucketName": {"query": "xxx"}}},

这不是:

        {
            "match_phrase": {
                "requestParameters.bucketName": {
                    "query": [
                        "auditloggingnew2232",
                        "config-bucket-123",
                        "web-servers",
                        "esbck-essnap-1djjegwy9fvyl",
                        "tempexpo",
                    ]
                }
            }
        }

match_phrase根本不支持多值

您可以使用 should 查询:

GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "requestParameters.bucketName": {
              "value": "auditloggingnew2232"
            }
          }
        },
        {
          "match_phrase": {
            "requestParameters.bucketName": {
              "value": "config-bucket-123"
            }
          }
        }
      ]
    },
    ...
  }
}

或者,正如@Val 指出的那样,terms 查询:

{
  "query": {
    "terms": {
      "requestParameters.bucketName": [
        "auditloggingnew2232",
        "config-bucket-123",
        "web-servers",
        "esbck-essnap-1djjegwy9fvyl",
        "tempexpo"
      ]
    }
  }
}

确切地说,它的功能类似于 OR

我假设 1) 有问题的存储桶名称是唯一的,并且 2) 您不是在寻找部分匹配项。如果是这样的话,再加上字段 bucketName 上几乎没有设置任何分析器,甚至可能不需要 match_phraseterms 就可以了。 termmatch_phrase 查询之间的区别很好地解释了 here