将JSON elasticsearch查询更改为python

问题描述 投票:0回答:1

我一直在python中使用弹性搜索的基本查询,如下所示:

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”中是否存在某个字段。任何想法如何实现这一点?

this链接中,我看到了一个使用JSON的bool查询的示例,但我不明白如何使用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
    }
  }
}
python json django elasticsearch
1个回答
0
投票

找出使用或查询的方式:

q = Q("regexp", text_en='someword.*') | Q("regexp", title_en='someword.*')

c = Search().query(q)
response = c.execute()
© www.soinside.com 2019 - 2024. All rights reserved.