question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

msearch query with filter and aggregations doesn't work

See original GitHub issue

Hello. Is there a way to run more complex queries with filters and aggregations than in https://github.com/elastic/elasticsearch-py/issues/59? For example,

{   "size": 0,
    "query": {
        "match": {"search_key": "my_key_value"}
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "fquery": {
                        "query": {
                            "query_string": {
                                "query": "applicationType:(\"myapptype\")"
                            }
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "client": {
            "terms": {
               "field": "client"
            }
        }
    }
}

If I do the following, the query doesn’t run:

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

The output is:

{u'responses': [{u'error': u'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[BRYrrx8dR4i-ukxlXbuNEw][index20150315][1]: RemoteTransportException[[i-1f34c5fb][inet[/10.101.2.234:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filter": {"bool": {"must": [{"fquery": {"query": {"query_string": {"query": "applicationType:(\\"myapptype\\")"}}}}]}}, "query": {"match": {"search_key": "my_key_value"}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: QueryParsingException[[index20150315] No query registered for [filter]]; }{[BRYrrx8dR4i-ukxlXbuNEw][index20150315][2]: RemoteTransportException[[i-1f30c5fb][inet[/10.101.2.234:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filter": {"bool": {"must": [{"fquery": {"query": {"query_string": {"query": "applicationType:(\\"myapptype\\")"}}}}]}}, "query": {"match": {"search_key": "my_key_value"}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: QueryParsingException[[index20150315] No query registered for [filter]]; }{[No9GT_PtQh6XoOvfz5uTmw][index20150315][0]: RemoteTransportException[[i-1d30c5f9][inet[/10.101.2.237:9300]][search/phase/query]]; nested: SearchParseException[[index20150315][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"filter": {"bool": {"must": [{"fquery": {"query": {"query_string": {"query": "applicationType:(\\"myapptype\\")"}}}}]}}, "query": {"match": {"search_key": "my_key_value"}}, "aggs": {"client": {"terms": {"field": "client"}}}, "size": 0}}]]]; nested: QueryParsingException[[index20150315] No query registered for [filter]]; }]'}]}

This is yet with just one query in there though I need quite a lot of similar to be processed by msearch. Could you please provide some instructions how to form a query in this case to match to what msearch is expecting? Software versions: Python 2.6.6, elasticsearch-py 1.4.0, Elasticsearch 1.3.2.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
roman-parkhunovskyi-spcommented, Aug 28, 2015

This turned out to be the improperly constructed query and it should have looked like:

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

And everything works then.

0reactions
honzakralcommented, Mar 19, 2015

Yes, it should speed things up quite a bit - the queries will be run in parallel in the cluster while also eliminating a lot of network traffic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Elastic search query filter and aggregation is not working
1 Answer 1 ... Using your mapping and creating recommendation of type "nested". You need to use nested filter in aggregation. Using nested...
Read more >
Is using "filter" inside "aggs" more efficient than "query" at the ...
Using filters to eliminate documents from search results is more efficient than using full text search and only taking into account the highest ......
Read more >
$eq inside $filter does not work on array fields - MongoDB
The problem is one of the fields by which I want to perform the filter is array ( p.metadata.category ) and Mongo $eg...
Read more >
Aggregate awareness | Looker - Google Cloud
Looker uses aggregate awareness logic to find the smallest, most efficient table available in your database to run a query while still ...
Read more >
Bucket Aggregations - OpenSearch documentation
If the significant_terms aggregation doesn't return any result, you might have not filtered the results with a query. Alternatively, the distribution of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found