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 に答える