【问题标题】:Or in a Elasticsearch filter或者在 Elasticsearch 过滤器中
【发布时间】:2017-08-19 20:34:50
【问题描述】:

我想查询我的 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”的所有文档

【问题讨论】:

  • 试试 should 而不是 filter 给你想要的结果。
  • @Pit 应该会在我的查询中暗示得分,我不想放慢速度

标签: python apache python-3.x elasticsearch


【解决方案1】:

您可以将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 版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多