# Match\_all query -- differences between the two queries?

**URL:** <https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352>\
**Category:** Elasticsearch\
**Created:** [August 13, 2015, 10:35pm UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352 "2015-08-13T22:35:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [August 13, 2015, 10:35pm UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/1 "2015-08-13T22:35:29Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [August 14, 2015, 12:01am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/2 "2015-08-14T00:01:27Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [August 14, 2015, 12:14am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/3 "2015-08-14T00:14:10Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [August 14, 2015, 2:00am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/4 "2015-08-14T02:00:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [August 14, 2015, 7:37am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/5 "2015-08-14T07:37:05Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [August 16, 2015, 11:09pm UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/6 "2015-08-16T23:09:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [August 17, 2015, 8:57am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/7 "2015-08-17T08:57:44Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [August 18, 2015, 12:21am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/8 "2015-08-18T00:21:59Z")

</div>

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 😄

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [August 18, 2015, 1:57am UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/9 "2015-08-18T01:57:51Z")

</div>

@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. 😄

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:55pm UTC](https://discuss.elastic.co/t/match-all-query-differences-between-the-two-queries/27352/10 "2017-07-05T23:55:26Z")

</div>


