7
votes

I'm using elasticsearch-rails gem in my Rails app to simplify integration with Elasticsearch. I'm trying to use the phonetic analysis plugin, so I need to define a custom analyzer and a custom filter for my index.

I tried this piece of code in order to perform the custom analysis with a soundex phonetic filter, but It fails with an exception message:

[!!!] Error when creating the index: Elasticsearch::Transport::Transport::Errors::BadRequest [400] {"error":"MapperParsingException[mapping [call_sentence]]; nested: MapperParsingException[Analyzer [{tokenizer=standard, filter=[standard, lowercase, metaphoner]}] not found for field [phonetic]]; ","status":400}

# Set up index configuration and mapping
#
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
  mapping do
    indexes :text, type: 'multi_field' do
      indexes :processed, analyzer: 'snowball'
      indexes :phone, {analyzer: {
        tokenizer: "standard",
        filter: ["standard", "lowercase", "metaphoner"]
      }, filter: {
        metaphoner: {
            type: "phonetic",
            encoder: "soundex",
            replace: false
        }
      }}
      indexes :raw, analyzer: 'keyword'
    end
  end
end
2

2 Answers

13
votes

You can also specify it in the settings call:

settings index: { 
    number_of_shards: 1, 
    number_of_replicas: 0,
    analysis: {
      filter: {
        metaphoner: { 
          type: 'phonetic',
          encoder: doublemetaphone,
          replace: true,
        } 
      },
      analyzer: {
        phonetic_analyzer: {
          tokenizer: 'standard',
          filter: ["standard", "lowercase", "metaphoner"],
        }
      }
    }
  } do
    mapping do
      indexes :text, type: 'multi_field' do
        indexes :processed, analyzer: 'snowball'
        indexes :phone, analyzer: 'phonetic_analyzer'
        indexes :raw, analyzer: 'keyword'
      end
    end
end
2
votes

Alright, I modified elasticsearch.yml config to include the phonetic analyzer

#################################### Index ####################################
index: 
  analysis: 
    analyzer: 
      phonetic_analyzer: 
        tokenizer: standard
        filter: [metaphoner]
    filter: 
      metaphoner: 
        type: phonetic
        encoder: doublemetaphone
        replace: true