49

I've got a field in an ElasticSearch field which I do not want to have analyzed, i. e. it should be stored and compared verbatim. The values will contain letters, numbers, whitespace, dashes, slashes and maybe other characters.

If I do not give an analyzer in my mapping for this field, the default still uses a tokenizer which hacks my verbatim string into chunks of words. I don't want that.

Is there a super simple analyzer which, basically, does not analyze? Or is there a different way of denoting that this field shall not be analyzed?

I only create the index, I don't do anything else. I can use analyzers like "english" for other fields which seems to be built-in names for pre-configured analyzers. Is there a list of other names? Maybe there's one fitting my needs (namely doing nothing with the input).

This is my mapping currently:

{
  "my_type": {
    "properties": {
      "my_field1": { "type": "string", "analyzer": "english" },
      "my_field2": { "type": "string" }
    }
  }
}

my_field1 is language-dependent; this seems to work. my_field2 shall be verbatim. I'd like to give an analyzer there which simply does not do anything.

A sample value for my_field2 would be "B45c 14/04".

Alfe
  • 56,346
  • 20
  • 107
  • 159

4 Answers4

58
"my_field2": {
    "properties": {
        "title": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
}

Check you here, https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-core-types.html, for further info.

fernandosavio
  • 9,849
  • 4
  • 24
  • 34
Scott Rice
  • 2,430
  • 22
  • 18
  • 1
    Ah! That's what I was looking for. I stumbled across that `not_analyzed` several times, but always thought using it would mean that it cannot be searched at all (apparently that's what `no` is used for). The link to the documentation was enlightening, thank you! (And given time I will accept this answer unless sth even more helpful appears.) – Alfe Aug 14 '13 at 17:46
  • 1
    @Alfe, you can have at [this answer for more info](http://stackoverflow.com/questions/16911633/not-indexed-field-is-stored-in-index/16923084#16923084) including the option `index: no` – ramseykhalaf Aug 15 '13 at 09:22
  • 2
    Can we set it globally so, it doesn't analyze string type for all? – coderek Aug 27 '16 at 14:00
  • By default if we dont add 'index': 'not_analyzed' what gonna happen then? does there a default analyzer? – famas23 Oct 12 '18 at 00:44
53

This is no longer true due to the removal of the string (replaced by keyword and text) type as described here. Instead you should use keyword type with "index": true | false.

For Example OLD:

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

becomes NEW:

{
  "foo": {
    "type" "keyword",
    "index": true
  }
}

This means the field is indexed but as it is typed as keyword not analyzed implicitly. If you would like to have the field analyzed, you need to use text type.

Honza
  • 974
  • 10
  • 18
vanthome
  • 4,816
  • 37
  • 44
  • It seems it's "enabled": true/false instead of "index" https://www.elastic.co/guide/en/elasticsearch/reference/6.5/enabled.html – theseadroid May 28 '19 at 17:11
  • @theseadroid No, `"enabled":false` will prevent receiving the field at all (except manual parsing of _source field), while `"index":false` will prevent only searching/aggregations. – Honza Jan 14 '20 at 16:52
3

keyword analyser can be also used.

// don't actually use this, use "index": "not_analyzed" instead
{
  "my_type": {
    "properties": {
      "my_field1": { "type": "string", "analyzer": "english" },
      "my_field2": { "type": "string", "analyzer": "keyword" }
    }
  }
}

As noted here: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-keyword-analyzer.html, it makes more sense to mark those fields as not_analyzed.

But keyword analyzer can be useful when it is set by default for whole index.

UPDATE: As it said in comments, string is no longer supported in 5.X

denis.peplin
  • 9,585
  • 3
  • 48
  • 55
  • 4
    actually, the `keyword` analyzer does analyze the field, but only just once as a whole. it might be not desired when this is setted up on some large text fields like ~MB contents - it will go into the index and eat some resources. – ulkas Feb 22 '16 at 12:13
  • 1
    Moreover, the `string` field is unsupported for indexes created in 5.x in favor of the `text` and `keyword` fields. – lifeisfoo May 25 '17 at 20:09
1

for API 8.5 the old answers wont work, and I found the solution by accident, just set property to "enabled=false", check the official doc, there is example inside https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html

Josh Lin
  • 2,397
  • 11
  • 21