# Defining analyzer per query

**URL:** <https://discuss.elastic.co/t/defining-analyzer-per-query/173273>\
**Category:** Elasticsearch\
**Created:** [March 21, 2019, 9:02am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273 "2019-03-21T09:02:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xxviktor](https://avatars.discourse-cdn.com/v4/letter/x/87869e/32.png) [@xxviktor](https://discuss.elastic.co/u/xxviktor)\
**Post date:** [March 21, 2019, 9:02am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/1 "2019-03-21T09:02:57Z")

</div>

Hi,  
I have read in the documentation that it should be possible to specify analyzer per query. I tried a few examples with custom ngram analyzer set for match query, but it was not working as I expected.

```
put people
{
	"mappings": {
		"people": {
			"properties": {
				"name": {
					"type": "text"
				}
			}
		}
	},
	"settings": {
		"analysis": {
			"analyzer": {
				"ngramAn": {
					"type": "custom",
					"filter": [
						"lowercase",
						"ngramFilt"
					],
					"tokenizer": "standard"
				}
			},
			"filter": {
				"ngramFilt": {
					"type": "ngram",
					"max_gram": 4,
					"min_gram": 3
				}
			}
		}
	}
}
POST people/people/
{
    "name" : "kimchy"
}

GET people/_search
{
    "query": {
        "match" : {
            "name" : {
                "query" : "kim",
                "analyzer": "ngramAn"
            }
        }
    }
}

```

Have I done something wrong or is the problem in the bad understanding of analyzers (it is not clear from documentation if the query analyzer does not apply only search term - so in this case only "kim" is analyzed by ngramAn and the results of this analysis is matched against "name" field with standard analyzer).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 21, 2019, 9:41am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/2 "2019-03-21T09:41:49Z")

</div>

Here is what you should do instead:

```auto
DELETE people
PUT people
{
	"mappings": {
		"_doc": {
			"properties": {
				"name": {
					"type": "text",
					"analyzer": "ngramAn",
					"search_analyzer": "simple"
				}
			}
		}
	},
	"settings": {
		"analysis": {
			"analyzer": {
				"ngramAn": {
					"type": "custom",
					"filter": [
						"lowercase",
						"ngramFilt"
					],
					"tokenizer": "standard"
				}
			},
			"filter": {
				"ngramFilt": {
					"type": "ngram",
					"max_gram": 4,
					"min_gram": 3
				}
			}
		}
	}
}
POST people/_doc
{
    "name" : "kimchy"
}

GET people/_search
{
    "query": {
        "match" : {
            "name" : {
                "query" : "kim"
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![xxviktor](https://avatars.discourse-cdn.com/v4/letter/x/87869e/32.png) [@xxviktor](https://discuss.elastic.co/u/xxviktor)\
**Post date:** [March 21, 2019, 9:48am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/3 "2019-03-21T09:48:30Z")

</div>

Yes, I know how to set custom analyzer in mapping, but I was curious about "The `analyzer` defined in a [full-text query](https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html)." mentioned in [https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 21, 2019, 9:53am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/4 "2019-03-21T09:53:07Z")

</div>

In your example, the analyzer `ngramAn` is applied to `kim`.  
This is producing the following terms: `["kim"]`.

At index time you indexed `kimchy` which was analyzed as `["kimchy"]`.

So `kim` does not match `kimchy`. That's why you can't find the document.

Reason is that `analyzer` set on a query is applied to the searched terms not onto the existing index.

HTH

---

<div class="post-metadata">

**Author:** ![xxviktor](https://avatars.discourse-cdn.com/v4/letter/x/87869e/32.png) [@xxviktor](https://discuss.elastic.co/u/xxviktor)\
**Post date:** [March 21, 2019, 9:55am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/5 "2019-03-21T09:55:11Z")

</div>

Thats what was not clear to me from documentation, thank you for your help.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 21, 2019, 10:10am UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/6 "2019-03-21T10:10:30Z")

</div>

Would you like to contribute a change in the documentation to make it more explicit?

---

<div class="post-metadata">

**Author:** ![xxviktor](https://avatars.discourse-cdn.com/v4/letter/x/87869e/32.png) [@xxviktor](https://discuss.elastic.co/u/xxviktor)\
**Post date:** [March 21, 2019, 4:33pm UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/7 "2019-03-21T16:33:25Z")

</div>

I am afraid that my english level is not good enough for this 🙂

---

<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:** [April 18, 2019, 4:33pm UTC](https://discuss.elastic.co/t/defining-analyzer-per-query/173273/8 "2019-04-18T16:33:34Z")

</div>

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