Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Analyzer elasticsearch-rails

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
like image 430
badawym Avatar asked Jun 18 '14 14:06

badawym


2 Answers

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
like image 131
Cam Price-Austin Avatar answered Oct 13 '22 01:10

Cam Price-Austin


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
like image 2
badawym Avatar answered Oct 13 '22 01:10

badawym