简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM