python - 或者在 Elasticsearch 过滤器中

标签 python apache python-3.x elasticsearch

我想查询我的elasticsearch(使用python库)并且我想过滤一些文档。由于我不想获得分数,因此我仅使用过滤器,而不能使用关键字:

{
        "_source": ["entities"], 
        "query": {
            "bool": {
                "must_not": [
                  {"exists": {"field": "retweeted_status"}}
                ],
                "filter": [
                  {"match": {"entities.urls.display_url": "blabla.com"}},
                  {"match": {"entities.urls.display_url": "blibli.com"}}]
            }
        }
    }

这是我所做的查询,但问题是在同一个过滤器中,它显然是执行的 AND 操作。我希望它是一个 OR。如何更改查询以获取包含“blibli.com”或“blabla.com”的所有文档

最佳答案

您可以将 bool 嵌套在另一个 bool 中,这样您就可以编写如下查询:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "retweeted_status"
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "entities.urls.display_url": "blabla.com"
                }
              },
              {
                "match": {
                  "entities.urls.display_url": "blibli.com"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

在ES 5.3上测试,可以使用Explain API检查这是否也适用于您的 Elasticsearch 版本。

关于python - 或者在 Elasticsearch 过滤器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041271/

相关文章:

python - 使用 networkx 在两个节点之间绘制多条边

python - 如何访问 Google Colab 笔记本上的文件

php - Jorani - 无法加载请求的语言文件 : language/fr/global_lang. php

python-3.x - 在 Python 中创建路径长度超过 260 个字符的目录

Python beautifulsoup 解析速度提升

python - Python 采样模块

php - 使 www.example.com 和 example.com 使用相同 session 变量的最佳方法?

html - (HTTP) 在 URL 中隐藏页面扩展

python - 如何在 Python 3 中将 -0.00 转换为 0.00

python - 如何使用python读取文本文件中的数字?