# Speed up filtered multi\_match bool phrase query

**URL:** https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205
**Category:** Elasticsearch
**Created:** [January 27, 2016, 9:31am UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205 "2016-01-27T09:31:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edeak](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@edeak](https://discuss.elastic.co/u/edeak)
#### Post date: [January 27, 2016, 9:31am UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205/1 "2016-01-27T09:31:10Z")

</div>

Hi guys,

I'm trying to speed up a complex query and currently I have the following:

`{ "query" : { "filtered": { "query": { "bool": { "should": [{"multi_match": { "query": "term1", "type":"phrase","fields": [ "field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "term2", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}},indent preformatted text by 4 spaces {"multi_match": { "query": "term3", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "term4", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "term5", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "term6", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "term7", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}} ] } }, "filter" : { "range" : { "createDate" : { "gte":"2015-01-01 00:00:01", "lte":"2015-01-05 23:59:59", "format": "yyyy-MM-dd HH:mm:ss" } } } } } }`

It's really slow. I have 15m documents and field1, field2... field6 is analyzed.

Note that if I run the following:  
`{ "query" : { "multi_match": { "query": "term", "type": "phrase", "fields": ["field1", "field2", "field3", "field4", "field5", "field6"] } }, "filter" : { "range" : { "createDate" : { "gte":"2015-01-01 00:00:01", "lte":"2015-01-05 23:59:59", "format": "yyyy-MM-dd HH:mm:ss" } } } } }`

it's quite fast. I know that the two queries are not the same but I have to search in several analyzed fields based on several keywords. How can I speed up the first query?

---

<div class="post-metadata">

### Author: ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)
#### Post date: [January 27, 2016, 10:23am UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205/2 "2016-01-27T10:23:45Z")

</div>

> [@edeak](#):
>
> "query": "term2"

Are you searching for only 1 term in every multi\_match query?

---

<div class="post-metadata">

### Author: ![edeak](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@edeak](https://discuss.elastic.co/u/edeak)
#### Post date: [January 27, 2016, 10:28am UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205/3 "2016-01-27T10:28:12Z")

</div>

Well, I started to figure out how to speed up and currently it seems that in one search I have to search for phrases and for single words also, so a typical query would be something like this:  
`{ "query" : { "filtered": { "query": { "bool": { "should": [{"multi_match": { "query": "phrase1", "type":"phrase","fields": [ "field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "phrase2", "type":"phrase","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, {"multi_match": { "query": "word1 word2 word3", "type":"most_fields","fields": ["field1", "field2", "field3", "field4", "field5", "field6"]}}, ] } }, "filter" : { "range" : { "createDate" : { "gte":"2015-01-01 00:00:01", "lte":"2015-01-05 23:59:59", "format": "yyyy-MM-dd HH:mm:ss" } } } } } }`

The number of single words and phrases is dynamic so in worst case it can be a few tens of different phrases.

---

<div class="post-metadata">

### Author: ![edeak](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@edeak](https://discuss.elastic.co/u/edeak)
#### Post date: [February 2, 2016, 9:18am UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205/4 "2016-02-02T09:18:00Z")

</div>

I did some improvements so I started to index my data with single terms and shingles (min 2, max 3), with the following settings and mapping:  
`{ "myIndex": { "mappings": { "myType": { "properties": { "_id": { "type": "string", "store": true }, "createDate": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" }, "field1": { "type": "string", "index": "not_analyzed" }, "field2": { "type": "string", "analyzer": "hu", "fields": { "shingles": { "type": "string", "analyzer": "hu" } } }, "field3": { "type": "string", "analyzer": "hu", "fields": { "shingles": { "type": "string", "analyzer": "hu" } } }, "field4": { "type": "string", "index": "not_analyzed" } } } }, "settings": { "index": { "analysis": { "filter": { "hu_HU": { "type": "hunspell", "locale": "hu_HU", "language": "hu_HU" }, "shingle": { "type": "shingle", "min_shingle_size": "2", "max_shingle_size": "3", "output_unigrams": "true" } }, "analyzer": { "hu": { "filter": ["lowercase", "hu_HU", "shingle"], "tokenizer": "standard" } } } } } } }`

In this case, based on [this](https://www.elastic.co/guide/en/elasticsearch/guide/current/shingles.html') article I changed my query as the following:

`{ "query" : { "filtered": { "query": { "bool": { "should": [{"multi_match": { "query": "my phrase1", "type":"most_fields","fields": [ "field2.shingles", "field3.shingles"]}}, {"multi_match": { "query": "my phrase2", "type":"most_fields","fields": ["field2.shingles", "field3.shingles"]}}, {"multi_match": { "query": "word1 word2 word3", "type":"most_fields","fields": ["field2", "field3"]}}, ] } }, "filter" : { "range" : { "createDate" : { "gte":"2015-01-01 00:00:01", "lte":"2015-01-05 23:59:59", "format": "yyyy-MM-dd HH:mm:ss" } } } } } }`

My question is: can it be more efficient? Does this query make sense in point of query optimization?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 11:19pm UTC](https://discuss.elastic.co/t/speed-up-filtered-multi-match-bool-phrase-query/40205/5 "2017-07-05T23:19:45Z")

</div>


