# Multiple bool-should-match phrase query optimization

**URL:** https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495
**Category:** Elasticsearch
**Created:** [December 19, 2016, 8:29pm UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495 "2016-12-19T20:29:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dangnh](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dangnh](https://discuss.elastic.co/u/dangnh)
#### Post date: [December 19, 2016, 8:29pm UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/1 "2016-12-19T20:29:37Z")

</div>

Hello all, we are now query elasticsearch to get exactly documents which have ID should match one in the ID array. It's smiliar to the following SQL query:

```sql
SELECT * FROM myindex WHERE transaction_id IN (id1, id2, id3)

```

translate it to Elasticsearch Query:

```javascript
{
	"from": 0,
	"size": 10000,
	"query": {
		"bool": {
			"must": {
				"bool": {
					"should": [
						{
							"match": {
								"transaction_id": {
									"query": "a",
									"type": "phrase"
								}
							}
						},
						{
							"match": {
								"transaction_id": {
									"query": "b",
									"type": "phrase"
								}
							}
						},
						{
							"match": {
								"transaction_id": {
									"query": "c",
									"type": "phrase"
								}
							}
						}
					]
				}
			}
		}
	}
}

```

However, with ~136 million document (continue growing) and size of the ID array is ~5000, this query come extremely slow.  
Any suggestion to optimize this?

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [December 19, 2016, 9:10pm UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/2 "2016-12-19T21:10:10Z")

</div>

The `terms` query ([docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html)) is designed to match one of many terms. Have a look at that one. It isn't analyzed and doesn't support phrase queries, only single terms, but it might help here if you can use it.

---

<div class="post-metadata">

### Author: ![dangnh](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dangnh](https://discuss.elastic.co/u/dangnh)
#### Post date: [January 12, 2017, 9:51pm UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/3 "2017-01-12T21:51:29Z")

</div>

I was profile both queries using profile API and it seems to be they are identical. Confused now ☹

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [January 13, 2017, 12:28am UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/4 "2017-01-13T00:28:52Z")

</div>

Terms query rewrites to a bool query with a bunch of should clauses if  
there are fewer than a certain never of terms iirc.

---

<div class="post-metadata">

### Author: ![dangnh](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dangnh](https://discuss.elastic.co/u/dangnh)
#### Post date: [January 13, 2017, 12:30am UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/5 "2017-01-13T00:30:50Z")

</div>

Thank you 😃 So what happens if there are many terms? Can you guide me to some resources about this?

---

<div class="post-metadata">

### Author: ![dangnh](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dangnh](https://discuss.elastic.co/u/dangnh)
#### Post date: [January 13, 2017, 1:07am UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/6 "2017-01-13T01:07:48Z")

</div>

@nik9000 I was test with 1056 difference term in one query, from profile API I can see that it still rewrite to many of should clauses.

---

<div class="post-metadata">

### Author: ![littlepoint](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/littlepoint/32/13669_2.png) [@littlepoint](https://discuss.elastic.co/u/littlepoint)
#### Post date: [January 18, 2017, 8:07am UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/7 "2017-01-18T08:07:48Z")

</div>

how about using multi-match?  
{  
"query":{  
"multi\_match": {  
"query": q,  
"fields": ["transaction\_id"],  
"type": "cross\_fields",  
}  
}  
}

---

<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: [February 15, 2017, 8:08am UTC](https://discuss.elastic.co/t/multiple-bool-should-match-phrase-query-optimization/69495/8 "2017-02-15T08:08:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
