elasticsearch-dsl-py查询形成

时间:2016-09-01 05:56:24

标签: python elasticsearch elasticsearch-dsl

有人可以告诉我如何使用这个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"
        }
       }
      }
     }
    }
   }
  }

1 个答案:

答案 0 :(得分:8)

这应该可以解决问题:

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"})