Searching with Query DSL: Full Text Queries

Published on November 12, 2016 by

Now that we have taken a look at how to search with query strings, I will now show you how to perform searches by defining queries within the request body in JSON. This approach is referred to as the Query DSL.

The first query I am going to show you, is a query that matches all documents, similar to what we did with an asterisk wildcard in the query string in the previous article. Now, instead of adding a q parameter to the URI, we specify our query within the request body as JSON.

GET /ecommerce/product/_search
{
  "query": { "match_all": {} }
}

The query property contains the query definition, where the match_all part is the type of query that we would like to execute. This query type does not need any configuration, so we just leave its object as empty.

Now that I have shown you the match_all query, let’s move on to the match query, which is used to search one or more fields for values. I’ll use the same examples as in the previous lectures and show you how to accomplish the same thing with the query DSL. First, I’ll find every product with the term pasta in the name.

GET /ecommerce/product/_search
{
  "query": {
    "match": {
      "name": "pasta"
    }
  }
}

But what if you wanted to search multiple fields? Theoretically, you could do this with boolean logic by using the bool query (which we’ll see in one of the next lectures). However, you would then have to construct many similar queries where only the field is different. Instead, one can use a query called multi_match, which allows for match queries to be run against multiple fields. Let me show you an example where I search for the term pasta in the name and description fields.

GET /ecommerce/product/_search
{
  "query": {
    "multi_match": {
      "query": "pasta", 
      "fields": [ "name", "description" ]
    }
  }
}

Next, I will show you how to search for phrases with the query DSL, by using the match_phrase query.

GET /ecommerce/product/_search
{
  "query": {
    "match_phrase": {
      "name": "pasta spaghetti"
    }
  }
}

Now we get a single result that contains the terms pasta and spaghetti in the name, and in that order. Remember that if we switched the terms around, this document would no longer match.

That’s all for full text searches when using the query DSL. Thank you for reading and happy searching!

Featured

Learn Elasticsearch today!

Take an online course and become an Elasticsearch champion!

Here is what you will learn:

  • The architecture of Elasticsearch
  • Mappings and analyzers
  • Many kinds of search queries (simple and advanced alike)
  • Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
  • ... and much more!
Elasticsearch logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

One comment on »Searching with Query DSL: Full Text Queries«

  1. Mac

    how do I checkout your previous post – you have not provided any link to it. And I tried finding it on your website – its a bit difficult to do that as well.

Leave a Reply

Your e-mail address will not be published.