# Cannot search tags with multi\_match phrase \[ElasticSearch 5.1.2\]

**URL:** <https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748>\
**Category:** Elasticsearch\
**Created:** [January 25, 2017, 9:48am UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748 "2017-01-25T09:48:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorcom](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lorcom](https://discuss.elastic.co/u/lorcom)\
**Post date:** [January 25, 2017, 9:48am UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/1 "2017-01-25T09:48:21Z")

</div>

Hi to everybody,

I have an index containing topics of a forum.

I match the phrase (specified by users) with multi\_match in fields title, body and tags.  
Users can also "search any words" or "search the phrase"; if use "search any words" all works fine.

If user choose "search the phrase" I use a multi\_match type "phrase" but the search failed.

Into elasticsearch logs I found:

> field "tags" was indexed without position data; cannot run PhraseQuery

When I had created index I had used this mapping:  
"mappings" : {  
"topics" : {  
"date\_detection" : false,  
"dynamic\_templates" : [  
{  
"generic\_not\_analyzed" : {  
"match" : ".+",  
"unmatch" : "(title)|(body)|(clean\_body)",  
"match\_pattern" : "regex",  
"mapping" : {  
"index": "not\_analyzed"  
}  
}  
}  
],  
"properties" : {  
"created": {"type": "date", "format": "dateTimeNoMillis"},  
"lastupdate": {"type": "date", "format": "dateTimeNoMillis"},  
"ts\_author\_oldest\_post": {"type": "date", "format": "dateTimeNoMillis"},  
"ts\_sort\_dashboard": {"type": "date", "format": "dateTimeNoMillis"},  
"tags" : {"type" : "string", "index" : "not\_analyzed"},  
"gallery": {  
"properties": {  
"approved": {"type": "date", "format": "dateTimeNoMillis"},  
"thumbs\_update": {"type": "date", "format": "dateTimeNoMillis"}  
}  
},  
"template": {  
"properties": {  
"approved": {"type": "date", "format": "dateTimeNoMillis"}  
}  
},  
"posts" : {  
"type": "nested",  
"include\_in\_parent": true,  
"properties": {  
"submitted" : {"type": "date", "format": "dateTimeNoMillis"},  
"body" : {"type": "string", "analyzer": "answ\_html\_default"}  
}  
},  
"followers" : {  
"type": "nested",  
"include\_in\_parent": true,  
"properties": {  
"id": {"type": "integer"},  
"level": {"type": "string"},  
"name": {"type": "string"},  
"read": {"type": "boolean"},  
"date\_read": {"type": "date", "format": "dateTimeNoMillis"},  
"date\_follow": {"type": "date", "format": "dateTimeNoMillis"}  
}  
}  
}  
}  
}

Someone can help me?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 25, 2017, 10:08am UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/2 "2017-01-25T10:08:37Z")

</div>

> [@lorcom](#):
>
> the search failed.

I suspect it is something to do with the tags field being "not\_analyzed" but I was unable to reproduce here. Can you share a problem query and maybe a doc with the irrelevant fields removed?

---

<div class="post-metadata">

**Author:** ![lorcom](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lorcom](https://discuss.elastic.co/u/lorcom)\
**Post date:** [January 25, 2017, 10:45am UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/3 "2017-01-25T10:45:37Z")

</div>

I had tried to remove "not\_analyzed", but when logs wrote:

> Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 25, 2017, 10:51am UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/4 "2017-01-25T10:51:13Z")

</div>

Let's back up a little.  
Do you really need to perform phrase queries on a `tags` field?  
Would you ever need to search for docs where the order/proximity of tags in a doc are important?

---

<div class="post-metadata">

**Author:** ![lorcom](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lorcom](https://discuss.elastic.co/u/lorcom)\
**Post date:** [January 25, 2017, 1:48pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/5 "2017-01-25T13:48:45Z")

</div>

Tags in my documents are made by one or more words.

Example: "error", "generic", "generic error"

When user search "generic error" choosing "search the phrase", I want to find all douments with tags, title or body containing "generic error".

After your consideration, since I was already using a custom analyzer, I tried to use another custom analyzer with a keyword tokenizer only used when use choose "search the phrase".

But the results aren't as I expected, without errors.

I'm thinking my ways are wrong...

Have you got any idea?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 25, 2017, 1:57pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/6 "2017-01-25T13:57:26Z")

</div>

> [@lorcom](#):
>
> When user search "generic error" choosing "search the phrase", I want to find all douments with tags, title or body containing "generic error".

And presumably if they search for "error" you also want to match docs tagged with "generic error"?

---

<div class="post-metadata">

**Author:** ![lorcom](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lorcom](https://discuss.elastic.co/u/lorcom)\
**Post date:** [January 25, 2017, 1:58pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/7 "2017-01-25T13:58:15Z")

</div>

Yes exactly

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 25, 2017, 2:08pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/8 "2017-01-25T14:08:18Z")

</div>

So we are talking about support for free-text search, analyzers, phrase queries etc on this `tags` field.  
But also I expect you want to have a structured, "not analyzed" form of the tag for analytics e.g. bar charts in Kibana.  
These 2 goals are achieved by defining the mapping of the `tags` field to support both operations using separate logical fields in the index. An example of search and analytics below:

```
DELETE test
PUT test
{
   "settings": {
	  "index": {
		 "number_of_shards": 1
	  }    
   },
   "mappings": {
	  "log": {
		 "properties": {
			"tags": {
			   "type": "keyword",
			   "fields":{
				   "4search":{
					   "type":"text"
				   }
			   }
			}
		 }
	  }
   }
}
POST test/log
{
	"tags":["generic error"]
}

GET test/log/_search
{
   "query": {
	  "multi_match": {
		 "query": " error",
		 "fields": ["tags.4search"]
	  }
   }
}
GET test/log/_search
{
   "query": {
	  "multi_match": {
		 "query": "generic error",
		 "fields": ["tags.4search"],
		 "type":"phrase"
	  }
   }
}
GET test/log/_search
{
   "aggs": {
	  "tagTypes": {
		 "terms": {
			"field": "tags"
		 }
	  }
   }
}
```

---

<div class="post-metadata">

**Author:** ![lorcom](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lorcom](https://discuss.elastic.co/u/lorcom)\
**Post date:** [January 25, 2017, 2:24pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/9 "2017-01-25T14:24:49Z")

</div>

Thank you very much!!!

I implement it immediately.

---

<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 22, 2017, 2:25pm UTC](https://discuss.elastic.co/t/cannot-search-tags-with-multi-match-phrase-elasticsearch-5-1-2/72748/10 "2017-02-22T14:25:28Z")

</div>

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