How to use Phrase Search in Elasticsearch 6

Have a Database Problem? Speak with an Expert for Free
Get Started >>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
$ 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
$ 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ 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!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

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.