或者在弹性搜索中

2024-04-26 05:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想查询我的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操作。我希望是手术室。如何更改查询以使所有文档都包含“blibli.com网站“或”blabla.com““


Tags: 文档comurl过滤器sourcematchdisplay关键字
1条回答
网友
1楼 · 发布于 2024-04-26 05:48:28

您可以将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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

在es5.3上测试,您可以使用Explain API检查这是否也适用于您的Elasticsearch版本。你知道吗

相关问题 更多 >

    热门问题