Sorry, maybe the question is stupid, but I already broke my whole brain.

Task: Filter products by color.

There is an index

{ "settings" : { "analysis" : { "analyzer" : { "default": { "type": "custom", "filter": [ "standard", "lowercase", "english_stemmer", "english_stop_words" ], "tokenizer": "lowercase" } }, "filter" : { "english_stemmer" : { "type" : "stemmer", "language" : "english" }, "english_stop_words" : { "type" : "stop", "stopwords" : "_english_" } } } }, "mappings" : { "products": { "properties" : { "id" : { "type" : "integer"}, "name" : { "type" : "string" }, "description" : { "type" : "string" }, "price" : { "type" : "integer"}, "meta_title" : { "type" : "string" }, "meta_keywords" : { "type" : "string" }, "meta_description" : { "type" : "string" }, "rating" : { "type" : "integer"}, "category_id" : { "type": "string", "index": "not_analyzed" }, "category_name" : { "type": "string" }, "color" : { "type": "string" }, "brand_id" : { "type": "integer" }, "brand_name" : { "type": "string" } } } } } 

Color is a string.

I have already tried a bunch of options and none of them filters the results.

 { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "bool": { "should": [{ "term": { "color": "White" } }] } } } } } 

or

 { "bool": { "filter" : [ { "terms" : { "color" : "white" } } ] } } 

or

 { "query" : { "term" : { "color" : "White" } } } 

And probably with a thousand different options from the Internet, and still ES just gives the results as he pleases, among which there may not even be the color of White even the occurrence of this line.

Can anyone explain how you can filter by a string field in ElasticSearch?

    1 answer 1

    You have a problem in mapping. By default, all lines go through the work of the analyzers. It is necessary to update the mapping to be

     { "color": { "type": "string", "index": "not_analyzed" } } 

    After the mapping has changed, the data in the index needs to be updated. And only after that term-y filtering will work for you.

    Read more about this here .

    • Thank you very much, very helpful)) - Roman Dubrovin