1

我正在使用elasticsearch-modelelasticsearch-rails宝石。

在我的个人资料模型中,我有

settings index: { number_of_shards: 3 } do
    mappings dynamic: 'false'  do
      indexes :profile_type, analyzer: 'keyword'
      indexes :profile do |p|
        indexes :name, type: 'multi_field' do
          indexes :name, analyzer: 'snowball'
          indexes :tokenized, analyzer: 'keyword'
        end
      end
    end
 end

我尝试添加suggest字段

settings index: { number_of_shards: 3 } do
    mappings dynamic: 'false'  do
      indexes :profile_type, analyzer: 'keyword'
      indexes :suggest, type: 'suggest' # not sure about this
      # do not really know how to index this field
      indexes :profile do |p|
        indexes :name, type: 'multi_field' do
          indexes :name, analyzer: 'snowball'
          indexes :tokenized, analyzer: 'keyword'
        end
      end
    end
 end

我正在尝试提出建议profile.name,我已添加as_indexed_json

def as_indexed_json(options={})
    suggest= {
     suggest: {
      :input => profile.name,
      :output => profile.name,
      :payload => {
        name: profile.name
      }
     }
    }
    self.as_json(
      :only => [:profile_type, :slug],
      :include => {
        :profile => {}
      }
    ).merge(suggest)
end

任何帮助将不胜感激,任何建议。

4

1 回答 1

1

我想出了如何使用elasticsearch-railsgem映射数据

settings index: { number_of_shards: 3 } do
    mappings dynamic: 'false'  do
      indexes :profile_type, analyzer: 'keyword'
      indexes :suggest, type: 'completion', index_analyzer: 'simple', search_analyzer: 'simple', payloads: true
      # will do the trick
      indexes :profile do |p|
        indexes :name, type: 'multi_field' do
          indexes :name, analyzer: 'snowball'
          indexes :tokenized, analyzer: 'keyword'
        end
      end
    end
 end

然后创建索引

def as_indexed_json(options={})
    suggest= {
     suggest: {
      :input => profile.name,
      :output => profile.name,
      :payload => {
        name: profile.name
      }
     }
    }
    self.as_json(
      :only => [:profile_type, :slug],
      :include => {
        :profile => {}
      }
    ).merge(suggest)
end

然后不要使用search方法,而是使用suggest方法

  def self.suggest query
    __elasticsearch__.client.suggest(:index => index_name, :body => {
      :suggestions => {
        :text => query,
        :completion => {
          :field => 'suggest',
          :size => 20
        }
      }
    })
  end

这样你得到complete suggestion

于 2015-07-14T19:36:15.653 回答