How to specify an analyzer while creating an index in ElasticSearch

Alfe picture Alfe · Aug 14, 2013 · Viewed 18.1k times · Source

I'd like to specify an analyzer, name it, and use that name in a mapping while creating an index. I'm lost, my ES instance always returns me an error message.

This is, roughly, what I'd like to do:

"settings": {
  "mappings": {
    "alfedoc": {
      "properties": {
        "id": { "type": "string" },
        "alfefield": { "type": "string", "analyzer": "alfeanalyzer" }
      }
    }
  },
  "analysis": {
    "analyzer": {
      "alfeanalyzer": {
        "type": "pattern",
        "pattern":"\\s+"
      }
    }
  }
}

But this does not seem to work; the ES instance always returns me an error like

MapperParsingException[mapping [alfedoc]]; nested: MapperParsingException[Analyzer [alfeanalyzer] not found for field [alfefield]];

I tried putting the "analysis" branch of the dictionary at several places (inside the mapping etc.) but to no avail. I guess a working complete example (which I couldn't find up to now) would help me along as well. Probably I'm missing something rather basic.

Answer

Scott Rice picture Scott Rice · Aug 14, 2013

"analysis" goes in the "settings" block, which goes either before or after the "mappings" block when creating an index.

"settings": {
    "analysis": {
        "analyzer": {
            "alfeanalyzer": {
                "type": "pattern",
                "pattern": "\\s+"
            }
        }
    }
},
"mappings": {
    "alfedoc": { ... }
}

Here's a good complete, example: Example 1