elasticsearch-rails映射如何转换为elasticsearch索引配置?

时间:2016-04-25 17:49:15

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

我使用的是elasticsearch-rails(elasticsearch-model)gem,我对模型dsl最终如何转换为索引配置感到有些困惑。

我有以下代码来直接设置索引,它运行正常:

require 'elasticsearch'

client = Elasticsearch::Client.new log: true

# edge_ngram settings
settings_with_ngram = {
  "analysis": {
    "filter": {
      "nGram_filter": {
         "type": "nGram",
         "min_gram": 3,
         "max_gram": 8,
         "token_chars": [
            "letter",
            "digit",
            "punctuation",
            "symbol"
         ]
      }
    },
    "analyzer": {
      "nGram_analyzer": {
         "type": "custom",
         "tokenizer": "whitespace",
         "filter": [
            "lowercase",
            "asciifolding",
            "nGram_filter"
         ]
      },
      "whitespace_analyzer": {
         "type": "custom",
         "tokenizer": "whitespace",
         "filter": [
            "lowercase",
            "asciifolding"
         ]
      }
    }
  }
}

# item
client.indices.delete index: "test_matchables"
client.indices.create index: "test_matchables", body: {
  settings: settings_with_ngram,

  mappings: {
    matchable: {
      "_all": {
         "analyzer": "nGram_analyzer",
         "search_analyzer": "whitespace_analyzer"
      }
    }
  }
}

但是,当我(尝试)在模型中执行相同操作时,我收到错误:

  elasticsearch_settings = {
    index: {
      number_of_shards: 1,
      number_of_replicas: 0
    },
    analysis: {
      filter: {
        nGram_filter: {
           type: "nGram",
           min_gram: 3,
           max_gram: 8,
           token_chars: [
              "letter",
              "digit",
              "punctuation",
              "symbol"
           ]
        }
      },
      analyzer: {
        nGram_analyzer: {
           type: "custom",
           tokenizer: "whitespace",
           filter: [
              "lowercase",
              "asciifolding",
              "nGram_filter"
           ]
        },
        whitespace_analyzer: {
           type: "custom",
           tokenizer: "whitespace",
           filter: [
              "lowercase",
              "asciifolding"
           ]
        }
      }
    }
  }

  settings(elasticsearch_settings) do
    mappings do
      indexes :_all, analyzer: 'nGram_analyzer', search_analyzer: 'whitespace_analyzer'
    end
  end

错误讯息:

MapperParsingException[Failed to parse mapping [matchable]: Field [_all] is defined twice in [matchable]]; nested: IllegalArgumentException[Field [_all] is defined twice in [matchable]];

如果我可以使用elasticsearch-model(oder elasticsearch)向我展示在创建索引时发送给elasticsearch的整个请求,那将会很有帮助。

感谢您的任何建议:)

0 个答案:

没有答案