0

Elasticsearch文档描述了一种为 msearch API 查询构建简单查询体的方法。但是,如果您使用聚合,则使用 curl 或任何版本的 elasticsearch-py 库都不再适用。Github 对应票证。示例查询:

{ 
  "query": {
    "filtered": {
    "filter": { "term": {"applicationType":"myapptype"} },
    "query": {"match": {"search_key": "my_key_value"}}
    }
  },
  "aggs": {"client": {"terms": {"field": "client"}}},
  "size": 0
}

Python代码:

es = Elasticsearch()
es.msearch(body=[
   {"index": "index20150315"},
   {"query": {'filtered': {'filter': {'term': {'applicationType': 'myapptype'}}, 'query': {'match': {'search_key': 'my_key_value'}}}}, 'aggs': {'client': {'terms': {'field': 'client'}}}, 'size': 0}

])

输出如下:

{u'responses': [{u'error': u'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[BRYrrx8dR4i-ukxlXbuNEw][index20150315][1]: RemoteTransportException[[i-1f30c5fb][inet[/10.101.2.234:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][1]: query[filtered(search_key:my_key_value)->cache(applicationType:myapptype)],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filtered": {"filter": {"term": {"applicationType": "myapptype"}}, "query": {"match": {"search_key": "my_key_value"}}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "aggs"]; }{[BRYrrx8dR4i-ukxlXbuNEw][index20150315][2]: RemoteTransportException[[i-1f30c5fb][inet[/10.101.2.234:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][2]: query[filtered(search_key:my_key_value)->cache(applicationType:myapptype)],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filtered": {"filter": {"term": {"applicationType": "myapptype"}}, "query": {"match": {"search_key": "my_key_value"}}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "aggs"]; }{[No9GT_PtQh6XoOvfz5uTmw][index20150315][0]: RemoteTransportException[[i-1d30c5f9][inet[/10.101.2.237:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][0]: query[filtered(search_key:my_key_value)->cache(applicationType:myapptype)],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filtered": {"filter": {"term": {"applicationType": "myapptype"}}, "query": {"match": {"search_key": "my_key_value"}}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "aggs"]; }]'}]}

有没有人设法使 msearch API 与包含聚合的查询一起工作?

4

1 回答 1

1

事实上一切正常,这是我在 github 票证中使用的错误查询(上面发布的那个是正确的)。因此,命令如下:

es = Elasticsearch()
es.msearch(body=[
   {"index": "index20150315"},
   {'query': {'filtered': {'filter': {'term': {'applicationType': 'myapptype'}}, 'query': {'match': {'search_key': 'my_key_value1'}}}}, 'aggs': {'client': {'terms': {'field': 'client'}}}, 'size': 0},
   {}, {'query': {'filtered': {'filter': {'term': {'applicationType': 'myapptype'}}, 'query': {'match': {'search_key': 'my_key_value2'}}}}, 'aggs': {'client': {'terms': {'field': 'client'}}}, 'size': 0}])

对 curl 做同样的事情也很好。顺便说一句,elasticsearch-py文档告诉您在正文中需要换行符,但它可以作为 [index1, query1, index2, query2] 的普通列表工作。

于 2015-08-28T15:03:46.467 回答