How to use Phrase Search in Elasticsearch 6

Introduction

It’s true that you can quickly locate a specific phrase in an index in Elasticsearch. To accomplish the task, you’ll want to use Phrase Search. But if you’re just starting phrase search in Elasticsearch, you may be tempted to conduct a multiple word search with the match query.

Here’s why match works for single-word queries but not phrase queries. With match, when you enter a phrase that contains two or more words, the results will also include data with one of those words only. The match query option slows down the process of you receiving relevant results.

Let’s explain further. Say you use the match query to find the phrase “Strawberry Cake.” You just want the results that contain “Strawberry Cake.” The match query will return those results, but also in the mix you could get “Strawberry Ice Cream,” and “Chocolate Cake,” and the rare “Strawberry No-Bake-Almost-Cake-Like Surprise.”

This tutorial shows you step-by-step how to use Phrase Search. If you’re already familiar with the method and would like to skip right to the code, click here to go to Just the Code.

Prerequisites

There are two requirements you must have before we begin.

  • Elasticsearch 6 – This version features smaller sparse fields to counter waste of disk space, faster merge times, and a streamlined file system cache.

  • Access to CURL in Terminal – You’ll need to access CURL to pass GET requests to the URL.

An Example Using the Phrase Search Query

Here’s an illustration in JSON for our dataset query example. It shows a fictitious grocery store index with a list of items the store carries shown as the products type. The “id,” “name,” “price,” “quantity,” and the “department” fields are given along with a few items for our dataset.

| id | name | price | quantity | department

| – | – | – | – | –

1 | Multi-Grain Cereal | 4.99 | 4 | Packaged Foods

2 | 1lb Ground Beef | 3.99 | 29 | Meat and Seafood

3 | Dozen Apples | 2.49| 12 | Produce

4 | Chocolate Bar | 1.29 | 2 | Packaged Foods| Checkout

5 | 1 Gallon Milk | 3.29 | 16 | Dairy

6 | 0.5lb Jumbo Shrimp | 5.29 | 12 | Meat and Seafood

7 | Wheat Bread | 1.29 | 5 | Bakery

8 | Pepperoni Pizza | 2.99 | 5 |Frozen

9 | 12 Pack Cola | 5.29 | 6 | Packaged Foods

10| Lime Juice | 0.99 | 20 | Produce

11| 12 Pack Cherry Cola | 5.599 | 5 | Packaged Foods

Conduct the Phrase Search Query

Your query for the two-word phrase “Cherry Cola” might look similar to this:

$ curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/store/products/_search?pretty -d '

> {

> "query": {

> "match": {

> "name": "Cherry Cola"

> }

> }

> }

> '


{

"took" : 2,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : 2,

"max_score" : 1.219939,

"hits" : [

{

"_index" : "store",

"_type" : "products",

"_id" : "11",

"_score" : 1.219939,

"_source" : {

"id" : "11",

"name" : "12 Pack Cherry Cola",

"price" : 5.59,

"quantity" : 5,

"department" : [

"Packaged Foods"

]

}

},

{

"_index" : "store",

"_type" : "products",

"_id" : "9",

"_score" : 1.112916,

"_source" : {

"id" : "9",

"name" : "12 Pack Cola",

"price" : 5.29,

"quantity" : 6,

"department" : [

"Packaged Foods"

]

}

}

]

}

}

With the match query, our results contain more or less than our two-word phrase query of “Cherry Cola.” The two results it returned included “12 Pack” along with the word “Cola” and “12 Pack”‘ with the words “Cherry Cola.”

For better accuracy, use Phrase Search. This way you’ll receive more precision in your search results. Simply change the match query to match_phrase. Here’s how:

$ curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/store/products/_search?pretty -d '

> {

> "query": {

> "match_phrase": {

> "name": "Cherry Cola"

> }

> }

> }

> '


{

"took" : 8,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : 1,

"max_score" : 1.219939,

"hits" : [

{

"_index" : "store",

"_type" : "products",

"_id" : "11",

"_score" : 1.219939,

"_source" : {

"id" : "11",

"name" : "12 Pack Cherry Cola",

"price" : 5.59,

"quantity" : 5,

"department" : [

"Packaged Foods"

]

}

}

]

}

}

As you can see, the match_phrase query streamlines your two-word or more phrase searches.

Conclusion

This Phrase Search in Elasticsearch 6 tutorial showed you how to save time by using the query match_phrase as opposed to match. This is because the Phrase Search match_phrase query zeros in on exactly the search terms you query–not any more or less.

By the way, phrase searching using the match_phrase query isn’t the only way to search efficiently in Elasticsearch 6. The slop parameter may be of interest to you as well. Read more on it here. Meanwhile, to learn more about match_phrase, view the documentation or reach out to us. We’re here to help.

Just the Code

Here’s a code example showing Phrase Search, the match_phrase query in action.

$ curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/store/products/_search?pretty -d '

> {

> "query": {

> "match_phrase": {

> "name": "Cherry Cola"

> }

> }

> }

> '

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.