Match_all query -- differences between the two queries?

Hello everyone,

I am studying the two queries, and tried but cannot find what are the differences with and without match_all part?

https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html

# Term filter with number
GET /my_store/products/_search
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}

# Same as above, without the `match_all` query
GET /my_store/products/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}

thanks in advance,
Lin

There is no difference here. When you don't specify an actual query when using a filtered query, it defaults to a match_all query:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filtering_without_a_query

In either of your examples, you are simply filtering all documents where price is equal to 20.

1 Like

@Joshua_Rich, thanks and my confusion is I just need to filter the document to match the exact price value, why and how match_all works here? Match_all makes me think returning all document -- seems not related here to find/filter for an exact value.

regards,
Lin

Remember, you are using the search API here, your filter simply acts in the context of the search. You want to filter on price, but you still need to find some documents to apply this filter to. In your case, you don't care about specific documents, like documents containing a certain string etc. So you just search against all documents (a match_all query) and then filter those for price. Later on, you might want to just filter on documents with a certain feature/quality, like those containing a certain name or piece of text. That's when you'd actually specify some kind of query, then your filter just acts on those documents.

1 Like

@Joshua_Rich, thanks a lot. For your comments, "you are using the search API here", do you mean I can use other types API for the same purposes (find document whose price is 20)? Thanks.

Not really, just pointing out the fact that Elasticsearch is at its core, a way to search through your data. You ask it to find a bunch of documents, then you filter that result set and then maybe you might do some statistical analysis of it (i.e. an aggregation). This is the way it works, you can't really use it the way you want without searching.

1 Like

@Joshua_Rich, thanks a lot. If I just want to match some documents based on some conditions (no need for aggregation or other kinds of statistics), e.g. price equal some values, what is the most efficient way to query? Not sure whether what I post in original post in the best (query performance perspective) solution?

regards,
Lin

What you are doing is the best way. You don't care about relevancy here, just absolutes, so using a filtered_query as you've done is the optimal way already :smile:

1 Like

@Joshua_Rich, thanks for the confirmation. In the past, I may have a wrong understanding for the two queries,

  1. If I explicitly write match_all then filter, then I think ElasticSearch will fetch all documents to client side (maybe less performance efficient dues to transfer all documents from network), then do a filter;
  2. If I do not explicitly write match_all, then ElasticSearch is smart enough to fetch all matched documents at backend in a smarter way without transfer all documents.

Please feel free to correct me if it is not the case. :smile: