ElasticSearch-dsl 创建查询

Mar*_*ino 5 python elasticsearch elasticsearch-dsl

大家好:

我已经尝试使用 ElasticSearch-dsl Search() 类复制此查询很长一段时间,但不幸的是我无法得到它。

我想要复制的查询是:

{
    "_source": {
            "includes": [ "SendingTime","Symbol","NoMDEntries","*"]
        },
        "from" : 0, "size" : 10000,
  "query": {
    "bool": {
      "must": [
        {
            "range": {
            "SendingTime": {
              "gte": "Oct 3, 2018 08:00:00 AM",
              "lt": "Oct 3, 2018 02:00:59 PM"
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

日期时间最终将被变量替换。

到目前为止我唯一能做的就是:

search = Search(using=elastic_search, index="bcs-md-bmk-prod")\
    .query("bool", range= {"SendingTime" : {'gte': format_date(datetime.now() - relativedelta(days=1)), 'lt': format_date(datetime.now())}})\
Run Code Online (Sandbox Code Playgroud)

我知道我离我想要的东西还很远,所以如果有人能帮助我,我将不胜感激。

JoY*_*ord 6

在elasticsearch-dsl中有多种方法可以构建相同的查询,这是为了方便用户,但有时(也许经常)会让新用户更加困惑。

首先,每个原始查询和elasticsearch-dsl查询之间存在一对一的匹配。例如,以下内容是等效的:

# 1
'query': {
    'multi_match': {
        'query': 'whatever you are looking for',
        'fields': ['title', 'content', 'footnote']
    }
}
# 2
from elasticsearch_dsl.query import MultiMatch
MultiMatch(query='whatever you are looking for', fields=['title', 'content', 'footnote'])
Run Code Online (Sandbox Code Playgroud)

其次,这些对在elasticsearh-dsl中是等效的:

# 1 - using a class
from elasticsearch_dsl.query import MultiMatch
MultiMatch(query='whatever you are looking for', fields=['title', 'content', 'footnote'])
# 2 - using Q shortcut
Q('multi_match', query='whatever you are looking for', fields=['title', 'content', 'footnote'])
Run Code Online (Sandbox Code Playgroud)

# 1 - using query type + keyword arguments 
Q('multi_match', query='whatever your are looking for', fields=['title', 'content', 'footnote'])
# 2 - using dict representation
Q({'multi_match': {'query': 'whatever your are looking for', 'fields': ['title', 'content', 'footnote']}})
Run Code Online (Sandbox Code Playgroud)

# 1 - using Q shortcut
q = Q('multi_match', query='whatever your are looking for', fields=['title', 'content', 'footnote'])
s.query(q)
# 2 - using parameters for Q directly
s.query('multi_match', query='whatever your are looking for', fields=['title', 'content', 'footnote'])
Run Code Online (Sandbox Code Playgroud)

bool现在,如果我们回想一下查询的结构,它由布尔子句组成,每个子句都有一个“类型化的出现”(must、should、must_not 等)。由于每个子句也是一个“查询”(在您的情况下为“range查询”),因此它遵循与“查询”相同的模式,这意味着它可以用 Q 快捷方式表示。

因此,我构建查询的方式是:

search = Search(using=elastic_search, index="bcs-md-bmk-prod")
          .query(Q('bool', must=[Q('range', SendingTime={"gte": "Oct 3, 2018 08:00:00 AM", "lt": "Oct 3, 2018 02:00:59 PM"})]))
          .source(includes=["SendingTime","Symbol","NoMDEntries","*"])
Run Code Online (Sandbox Code Playgroud)

请注意,为简单起见,可以删除第一个 Q,从而形成该行:

.query('bool', must=[Q('range', SendingTime={"gte": "Oct 3, 2018 08:00:00 AM", "lt": "Oct 3, 2018 02:00:59 PM"})])
Run Code Online (Sandbox Code Playgroud)

但我会保留它以便更容易理解。请随意在不同的表示形式之间进行权衡。

最后但并非最不重要的一点是,当您在 elasticsearch-dsl 中构建查询遇到困难时,您始终可以使用类from_dict()方法回退到原始字典表示形式。elasticsearch_dsl.Search