Custom Analyzer elasticsearch-rails

时间:2014-06-18 14:37:18

标签: ruby-on-rails elasticsearch elasticsearch-plugin

我在我的Rails应用程序中使用elasticsearch-rails gem来简化与Elasticsearch的集成。我正在尝试使用phonetic analysis plugin,因此我需要为索引定义自定义分析器和自定义过滤器。

为了使用soundex语音过滤器执行自定义分析,我尝试了这段代码,但它失败并显示异常消息:

  

[!!!]创建索引时出错:Elasticsearch :: Transport :: Transport :: Errors :: BadRequest   [400] {“error”:“MapperParsingException [mapping [call_sentence]];嵌套:MapperParsingException [Analyzer [{tokenizer = standard,filter = [standard,lowercase,metaphoner]}]找不到字段[phonetic]];”, “状态”: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 个答案:

答案 0 :(得分:13)

您也可以在设置调用中指定它:

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

答案 1 :(得分:2)

好吧,我修改了elasticsearch.yml配置以包含语音分析器

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