elasticsearch-dsl-py 查询格式

elasticsearch-dsl-py query formation

谁能告诉我如何使用这个 dsl python 模块构建这个示例查询?

到目前为止,对于查询字符串的一部分,我只有类似的东西。

from elasticsearch_dsl import Search, Q, A, query, aggs
s = Search()
s.aggs.bucket('2', 'terms', field = 'Subscriber Type', size=5)

我不确定其余查询的语法是怎样的。非常感谢任何帮助。

所需的查询构造如下。

{
   "size": 0,
   "query": {
   "filtered": {
   "query": {
    "query_string": {
      "query": "lincoln",
      "analyze_wildcard": true
    }
  },
  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "Start date": {
              "gte": 936157359664,
              "lte": 1472701359665,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "must_not": []
    }
  }
}
},
 "aggs": {
 "2": {
    "terms": {
    "field": "Subscriber Type",
    "size": 5,
    "order": {
      "_count": "desc"
    }
  },
  "aggs": {
    "3": {
      "terms": {
        "field": "Start Station",
        "size": 5,
        "order": {
          "_count": "desc"
        }
       }
      }
     }
    }
   }
  }

这应该可以解决问题:

s = Search()
s = s.query("query_string", query="lincoln", analyze_wildcard=True)
s = s.filter("range", **{"Start date": {"gte": 936157359664, "lte": 1472701359665, "format": "epoch_millis"}})
s = s[0:0]
s.aggs.bucket("2", "terms", field="Subscriber Type", size=5)\
    .bucket("3", "terms", field="Start Station", size=5)

请注意,字段名称中的空格会使事情变得有点复杂,没有它们你可以说:s.filter("range", start_date={"gte": 936157359664, "lte": 1472701359665, "format": "epoch_millis"}).