Defining analyzer per query

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).

Here is what you should do instead:

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"
            }
        }
    }
}

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

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

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

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

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

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