ElasticSearch with Tire doesn't include custom

2019-08-26 21:45发布

问题:

I have an STI model which I want to be searchable with ElasticSearch and Tire. The issue I am having is when Tire creates the mappings it seems to ignore my custom analyzers for the second model. Below is an example of my models.

class Account < ActiveRecord::Base
  attr_accessible :name, :type

  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.settings :analysis => {
          :analyzer => {
          "custom_search_analyzer" => {
              "tokenizer" => "keyword",
              "filter" => "lowercase"
          },
          "custom_index_analyzer" => {
              "tokenizer" => "keyword",
              "filter" => ["lowercase","substring"]
          }
      },
      :filter => {
          :substring => {
              "type" => "nGram",
              "min_gram" => 1,
              "max_gram" => 20
          }
      }
  } do
    mapping do
      indexes :id, :type => 'integer', :include_in_all => false
      indexes :name, :type => 'string', :search_analyzer => :custom_search_analyzer,     :index_analyzer=>:custom_index_analyzer
    end
  end

  def to_indexed_json
    hash = {}
    hash[:id] = id
    hash[:name] = name
    hash.to_json
  end
end

class StandardAccount < Account
  tire.index_name 'accounts'
end

class SuperAccount < Account
  tire.index_name 'accounts'
end

When I create the index through tire, either with the rake task or through creating a model it creates the mappings but for the inherited models it doesn't apply the custom analyzers to them. If I look at the mappings using

curl -XGET 'http://127.0.0.1:9200/accounts/_mapping?pretty=1'  

I get:

{
  "accounts" : {
    "account" : {
      "properties" : {
        "id" : {
          "type" : "integer",
          "include_in_all" : false
        },
        "name" : {
          "type" : "string",
          "index_analyzer" : "custom_index_analyzer",
          "search_analyzer" : "custom_search_analyzer"
        }
      }
    },
    "standard_account" : { 
      "properties" : {
        "id" : {
          "type" : "long"
        }
        "name" : {
          "type" : "string"
        }
      }
    },
    "super_account" : {
      "properties" : {
        "id" : {
          "type" : "long"
        }
        "name" : {
          "type" : "string"
        }
      }
    }
  }
}

Even if I move the mapping declarations to the inherited classes it seems to be only the first model created that picks up the extra options. I can manually create the indexes through ElasticSearch but was wondering if there was a way to do it with Tire? Or do I have something set up incorrectly

回答1:

You might have figured out the answer already, but I think this might help you, or others having the same problem:

https://stackoverflow.com/a/13660263/1401343

-Vlad