# Filter vs query

**URL:** <https://discuss.elastic.co/t/filter-vs-query/317297>\
**Category:** Elasticsearch\
**Created:** [October 24, 2022, 7:48am UTC](https://discuss.elastic.co/t/filter-vs-query/317297 "2022-10-24T07:48:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ismet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismet/32/103499_2.png) [@Ismet](https://discuss.elastic.co/u/Ismet)\
**Post date:** [October 24, 2022, 7:48am UTC](https://discuss.elastic.co/t/filter-vs-query/317297/1 "2022-10-24T07:48:38Z")

</div>

Hi, can we use only filter without query and can we use query string with wildcard in filter. What are benefits of using only filter and using query string + filter? There is example of using only filter. Is this code ok?

```auto
query: {
          bool: {
            filter: [
              {
                term: { type: passedType },
              },
              {
                range: {
                  mag: {
                    gte: passedMag,
                  },
                },
              },
              {
                match: { place: passedLocation },
              }
            ],
          },
        },

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 24, 2022, 12:20pm UTC](https://discuss.elastic.co/t/filter-vs-query/317297/2 "2022-10-24T12:20:52Z")

</div>

A query participate to the score (how much the query matches the document) where a filter just "filters" the document without computing any score.

With queries, you can sort by the most relevant documents (default behavior).  
With filters, you can just say that a document matches or not.

A filter can be cached. A query can not.

> [@Ismet](#):
>
> query string with wildcard

That sounds like [a bad idea](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-wildcard)...

> Be aware that wildcard queries can use an enormous amount of memory and perform very badly — just think how many terms need to be queried to match the query string `"a* b* c*"` .

---

<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:** [November 21, 2022, 12:21pm UTC](https://discuss.elastic.co/t/filter-vs-query/317297/3 "2022-11-21T12:21:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
