maestro
maestro

Reputation: 671

Elasticsearch not analyzed field

I have an analyzed field which contains the following: 'quick brown foxes' and another one which contains: 'quick brown fox'.

I want to find those documents which explicity contains 'foxes' (not fox). As I know I have to create a multi-field with an analyzed and not-analyzed subfield (see my mapping below). But how can I query this?

Here's an example (note that my analyzer is set to hungarian but I guess this not matters here):

{
    "settings" : {
        "number_of_replicas": 0,
        "number_of_shards": 1,      
        "analysis" : {
            "analyzer" : {
                "hu" : {
                    "tokenizer" : "standard",
                    "filter" : [ "lowercase", "hu_HU" ]
                }
            },
            "filter" : {
                "hu_HU" : {
                    "type" : "hunspell",
                    "locale" : "hu_HU",
                    "language" : "hu_HU"
                }               
            }
        }
    },
    "mappings": {
        "foo": {
            "_source": { "enabled": true },
            "properties": {
                "text": {
                    "type": "string",
                    "analyzer": "hu",
                    "store": false,
                    "fields": {
                        "raw": {
                            "type": "string",
                            "index": "not_analyzed",
                            "store": false
                        }
                    }
                }
            }
        }
    }
}

Queries that I tried: match, term, span_term, query_string. All were executed on text and text.raw field.

Upvotes: 3

Views: 1338

Answers (1)

SergeyS
SergeyS

Reputation: 3553

"index": "not_analyzed" means this field will be not analyzed at all (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html). So it will not be even split into words. I believe this is not what you want. Instead of that, you need to add new analyzer, which will include only tokenizer whitespace (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-whitespace-tokenizer.html):

"analyzer" : {
      "hu" : {
          "tokenizer" : "standard",
           "filter" : [ "lowercase", "hu_HU" ]
       },
       "no_filter":{
           "tokenizer" : "whitespace"
       }
}

Then you need to use this new analyzer for your field:

"raw": {
     "type": "string",
     "analyzer": "no_filter",
     "store": false
}

Upvotes: 2

Related Questions