【问题标题】:Elasticsearch-rails completion suggester, map data, create indexes, suggest methodElasticsearch-rails 补全建议器、地图数据、创建索引、建议方法
【发布时间】:2015-06-23 10:00:41
【问题描述】:

我正在使用 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

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

【问题讨论】:

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


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-21
      • 2017-06-04
      • 2015-05-07
      • 1970-01-01
      • 2011-07-02
      • 2015-02-19
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多