ElasticSearch Filter vs Query

ElasticSearch Filter vs Query Feature Image

ElasticSearch has two ways to limit the number of documents to return, depending on the context. This tutorial gives you overview of these ways which we call Query and Filter.

Related Posts:
Elasticsearch Overview
ElasticSearch – Structure of a Search Request/Response
ElasticSearch Full Text Queries – Basic

1. Query Context

In this context, the query clause answers the question:
“How well does this document match this query clause?”

>> We have 2 main requirements:
– whether or not the document matches
– how well the document matches, relative to other documents (that _score represents)

For example, this query:

GET javasampleapproach/tutorial/_search
{
  "query": {
    "match": { "title": "Angular 4" }
  }
}

will have the response like this:

{
  ...
  "hits": {
    "total": 4,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "Angular 4 Elasticsearch Introduction",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "elasticsearch"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "3",
        "_score": 0.5649868,
        "_source": {
          "title": "Angular 4 Firebase Quick Start",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "2",
        "_score": 0.37227193,
        "_source": {
          "title": "Angular 4 Elasticsearch Create Index",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "elasticsearch"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "4",
        "_score": 0.3256223,
        "_source": {
          "title": "Angular 4 Firebase - CRUD Operations example",
          "post_date": "2017-10-26",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      }
    ]
  }
}

Look at the _score for responsed items. We can see that the _score decreases in order.
Change the query to:

GET javasampleapproach/tutorial/_search
{
  "query": {
    "match": { "title": "angular firebase" }
  }
}

The response will be:

{
  ...
  "hits": {
    "total": 4,
    "max_score": 0.78178394,
    "hits": [
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "4",
        "_score": 0.78178394,
        "_source": {
          "title": "Angular 4 Firebase - CRUD Operations example",
          "post_date": "2017-10-26",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "3",
        "_score": 0.5649868,
        "_source": {
          "title": "Angular 4 Firebase Quick Start",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "Angular 4 Elasticsearch Introduction",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "elasticsearch"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "2",
        "_score": 0.18613596,
        "_source": {
          "title": "Angular 4 Elasticsearch Create Index",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "elasticsearch"
          ]
        }
      }
    ]
  }
}

We recognise that the order is changed with _score.

2. Filter Context

In Filter context, a query clause answers the question:
“Does this document match this query clause?”

>> The response is just a simple Yes or No (without _score).

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. This context is mostly used for filtering structured data.

*Note: With Elasticsearch 5.6, the filtered query is replaced by the bool query.

For example:
– Is post_date from “2017-10-25”?
– Does tags contain “firebase”?

So with this query:

GET javasampleapproach/tutorial/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": { "tags": "firebase" }
        },
        {
          "range": {
            "post_date": { "gte": "2017-10-25" }
          }
        }
      ]
    }
  }
}

We have the response:

{
  ...
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "4",
        "_score": 0,
        "_source": {
          "title": "Angular 4 Firebase - CRUD Operations example",
          "post_date": "2017-10-26",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "3",
        "_score": 0,
        "_source": {
          "title": "Angular 4 Firebase Quick Start",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      }
    ]
  }
}

Notice that _score is constant.

3. Query & Filter Context

Now we mix 2 types of context in a Query Request:

GET javasampleapproach/tutorial/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": { "title": "angular 4" }
        }
      ],
      "filter": [
        { "term": { "tags": "firebase" } },
        { "range": { "post_date": { "gte": "2017-10-25" } } }
      ]
    }
  }
}

– The must and two match clauses are used in Query context, which means that they are used to calculate _score for how well each document matches.
– The filter indicates Filter context in which term and range are used. They will filter out documents which do not match, but NOT affect the _score.

So, we can look at the response:

{
  ...
  "hits": {
    "total": 2,
    "max_score": 0.5649868,
    "hits": [
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "3",
        "_score": 0.5649868,
        "_source": {
          "title": "Angular 4 Firebase Quick Start",
          "post_date": "2017-10-25",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      },
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "4",
        "_score": 0.3256223,
        "_source": {
          "title": "Angular 4 Firebase - CRUD Operations example",
          "post_date": "2017-10-26",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      }
    ]
  }
}

We can see that _score is calculated.
Now we change range of filter:

GET javasampleapproach/tutorial/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": { "title": "angular 4" }
        }
      ],
      "filter": [
        { "term": { "tags": "firebase" } },
        { "range": { "post_date": { "gte": "2017-10-26" } } }
      ]
    }
  }
}

The response is:

{
  ...
  "hits": {
    "total": 1,
    "max_score": 0.3256223,
    "hits": [
      {
        "_index": "javasampleapproach",
        "_type": "tutorial",
        "_id": "4",
        "_score": 0.3256223,
        "_source": {
          "title": "Angular 4 Firebase - CRUD Operations example",
          "post_date": "2017-10-26",
          "author": {
            "name": "JavaSampleApproach",
            "role": "admin"
          },
          "tags": [
            "angular",
            "angular 4",
            "firebase"
          ]
        }
      }
    ]
  }
}

The _score for “Angular 4 Firebase – CRUD Operations example” still does not change the value.

96 thoughts on “ElasticSearch Filter vs Query”

  1. Thank you for every other informative web site. Where else may just I am getting that type of info written in such a perfect method? I’ve a project that I am simply now running on, and I have been on the glance out for such information.

  2. Youre so cool! I dont suppose Ive read anything such as this before. So nice to discover somebody by original applying for grants this subject. realy thanks for beginning this up. this website is one thing that is required on-line, a person after a little originality. helpful problem for bringing new things for the world wide web!

  3. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more clear from this post. I am very glad to see such fantastic information being shared freely out there.

  4. I precisely had to thank you very much yet again. I do not know the things that I might have accomplished in the absence of the ideas shown by you concerning such a area of interest. This has been an absolute fearsome case for me personally, however , observing the specialized mode you dealt with it made me to jump over fulfillment. I am just happy for your guidance and even hope you comprehend what an amazing job that you are undertaking training other individuals through a blog. I am certain you’ve never encountered any of us.

  5. hello there and thank you for your info – I’ve definitely picked up anything new from right here. I did however expertise some technical issues using this web site, since I experienced to reload the web site many times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my email and could look out for much more of your respective interesting content. Make sure you update this again very soon..

  6. What i do not realize is in truth how you are now not actually much more neatly-preferred than you may be right now.
    You are so intelligent. You understand therefore considerably when it comes to this topic,
    made me individually consider it from a lot of various angles.
    Its like men and women don’t seem to be involved unless it is something to do with
    Lady gaga! Your personal stuffs outstanding. Always deal
    with it up!

  7. Hello this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

  8. I and also my pals were actually following the good suggestions found on your web page while immediately developed a horrible feeling I had not expressed respect to the site owner for those strategies. All of the young men are actually so happy to read all of them and have truly been tapping into these things. Thank you for genuinely quite kind and then for going for some ideal themes most people are really needing to be aware of. Our own honest apologies for not expressing appreciation to earlier.

  9. Very good blog you have here but I was wanting to know if you knew of any forums that cover the same topics discussed in this article? I’d really love to be a part of online community where I can get feedback from other experienced people that share the same interest. If you have any recommendations, please let me know. Cheers!

  10. I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  11. Thanks for another informative site. Where else could I get that kind of info written in such an ideal way? I have a project that I’m just now working on, and I’ve been on the look out for such info.

  12. I have been surfing online more than 3 hours today, yet I by no means found any interesting article like yours. It is beautiful value sufficient for me. Personally, if all website owners and bloggers made just right content as you did, the net might be a lot more useful than ever before.

  13. I have not checked in here for a while since I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend 🙂

  14. Great ?V I should certainly pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, web site theme . a tones way for your client to communicate. Nice task..

  15. hello there and thank you in your information – I have certainly picked up anything new from right here. I did then again experience several technical points the use of this web site, as I experienced to reload the website a lot of instances prior to I may get it to load properly. I have been considering in case your web hosting is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could injury your high quality rating if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Well I’m including this RSS to my email and could look out for a lot more of your respective fascinating content. Make sure you update this again soon..

  16. If you happen to be still on the fence: get your favored earphones, brain down in direction of a Perfect Order and talk to towards plug them into a Zune then an iPod and check out which one particular seems better towards oneself, and which interface can make you smile additional. Then you can recognize which is immediately for your self.

  17. I’ve recently started a website, the information you offer on this website has helped me tremendously. Thank you for all of your time & work. “The man who fights for his fellow-man is a better man than the one who fights for himself.” by Clarence Darrow.

  18. We’re a group of volunteers and starting a new scheme in our community. Your website provided us with valuable info to work on. You’ve done a formidable job and our entire community will be thankful to you.

  19. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  20. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  21. Hey just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.

  22. My developer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am anxious about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any kind of help would be really appreciated!

  23. I haven’t checked in here for some time because I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend 🙂

  24. I’m so happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that’s at the other blogs. Appreciate your sharing this best doc.

  25. You actually make it seem so easy together with your presentation but I find this matter to be actually something that I think I’d by no means understand. It sort of feels too complex and very large for me. I’m looking forward in your next submit, I’ll try to get the hold of it!

  26. An attention-grabbing dialogue is worth comment. I think that you should write extra on this topic, it won’t be a taboo topic but usually people are not sufficient to talk on such topics. To the next. Cheers

  27. I’ve been exploring for a little for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i am happy to convey that I’ve a very good uncanny feeling I discovered just what I needed. I most certainly will make certain to don’t forget this site and give it a look regularly.

  28. It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I desire to suggest you some interesting things or tips. Perhaps you could write next articles referring to this article. I desire to read even more things about it!

  29. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more clear from this post. I am very glad to see such excellent info being shared freely out there.

  30. Fantastic blog! Do you have any helpful hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed .. Any ideas? Thanks a lot!

  31. Have you ever thought about publishing an ebook or guest authoring on other blogs? I have a blog based upon on the same subjects you discuss and would really like to have you share some stories/information. I know my viewers would appreciate your work. If you’re even remotely interested, feel free to shoot me an e mail.

  32. I beloved up to you will receive carried out proper here. The sketch is tasteful, your authored material stylish. nevertheless, you command get got an nervousness over that you wish be delivering the following. in poor health undoubtedly come more previously again as exactly the same just about very incessantly inside of case you shield this hike.

  33. I was just searching for this information for a while. After six hours of continuous Googleing, at last I got it in your site. I wonder what’s the lack of Google strategy that don’t rank this kind of informative web sites in top of the list. Generally the top websites are full of garbage.

  34. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your weblog? My blog site is in the exact same area of interest as yours and my visitors would really benefit from a lot of the information you present here. Please let me know if this ok with you. Thank you!

  35. Everything is very open with a really clear clarification of the challenges. It was really informative. Your site is very helpful. Many thanks for sharing!

  36. I’m no longer sure where you are getting your information, however good topic. I must spend a while finding out more or understanding more. Thanks for excellent info I used to be on the lookout for this information for my mission.

  37. Howdy! This is kind of off topic but I need some guidance from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any tips or suggestions? Cheers

  38. Whats up very nice web site!! Man .. Beautiful .. Superb .. I will bookmark your site and take the feeds additionallyKI am satisfied to search out a lot of useful info here in the publish, we want work out extra techniques on this regard, thanks for sharing. . . . . .

  39. My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am anxious about switching to another platform. I have heard great things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any help would be really appreciated!

  40. I think other web-site proprietors should take this web site as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

  41. Great write-up, I?¦m normal visitor of one?¦s site, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  42. Woah! I’m really digging the template/theme of this website. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appeal. I must say that you’ve done a superb job with this. In addition, the blog loads super quick for me on Firefox. Outstanding Blog!

Leave a Reply

Your email address will not be published. Required fields are marked *