Elasticsearch documentation描述了为msearch API查询构建简单查询主体的方法。但是如果你使用聚合,这不再适用于使用curl或任何版本的elasticsearch-py库。 Github对应ticket。 示例查询:
{
"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与包含聚合的查询一起使用?
答案 0 :(得分:0)
实际上一切正常,这是我在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]的普通列表。