将 JSON elasticsearch query 更改为 python



我一直在python中使用基本查询进行elasticsearch,如下所示:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch()
def someView(request):
    s = Search().query("regexp", title_en="someword.*")
    response = s.execute()

我想组合一个查询来检查"title_en"或"text_en"字段中是否存在某个单词知道如何做到这一点吗?

在此链接中,我看到了一个使用 JSON 的布尔查询示例,但我不明白如何使用 python 代码完成类似的事情。

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}

找出使用或查询的方法:

q = Q("regexp", text_en='someword.*') | Q("regexp", title_en='someword.*')
c = Search().query(q)
response = c.execute()

最新更新