Searching with Query Strings: Phrase Query

Published on November 12, 2016 by

In this article, we will be talking about using the phrase query when searching with query strings.

By default, all terms are optional, as long as at least one term matches. This is because the default boolean operator is OR. We can see this if we search for pasta spaghetti.

GET /ecommerce/product/_search?q=name:pasta spaghetti

If we take a look at the results, we can see that they include products that only has pasta in the name.

But what if we wanted to search for a specific phrase? This can be done by enclosing the phrase within quotation marks. Let’s add quotation marks to the query that we just executed.

GET /ecommerce/product/_search?q=name:"pasta spaghetti"

Now we only get one result, which is the document that contains both pasta and spaghetti in its name, and in that order. Just to demonstrate that the order matters, let’s just try to switch the terms within the phrase around.

GET /ecommerce/product/_search?q=name:"spaghetti pasta"

Notice that the product no longer matches, because the ordering of the terms does not match. Let’s just reverse the order again and go back to the previous search result.

GET /ecommerce/product/_search?q=name:"pasta spaghetti"

Notice that there is a hyphen in the product name as well (“Pasta – Spaghetti“), so why does this phrase match? The answer has to do with how Elasticsearch analyzes documents when they are added to an index or their values are modified. By default, the standard analyzer is used, so let’s just check how this product name is analyzed. To do this, there is a convenient _analyze API, which returns the terms after the supplied value has been tokenized. We will set the analyzer to standard and pass in the product name as the text to analyze.

GET /_analyze?analyzer=standard&text=Pasta - Spaghetti

The result is a list of tokens, with the hyphen being dropped during the tokenization process by the standard analyzer. This is why our previous search query matched with a phrase that did not include the hyphen.

Those were the basics of the phrase query.

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!

Leave a Reply

Your e-mail address will not be published.