更像是elasticsearch-rails和mongoid什么都没有

时间:2016-02-11 20:54:26

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

我使用MongoDB存储有关鞋子的信息。每只鞋子都有标题(字符串),照片(字符串,照片的网址),链接(字符串,原始网站的网址),尺寸(字符串的aray),颜色(字符串数组)和价格(字符串)和性别(字符串)。

在我的项目/ app / models / shoe.rb文件中,我有:

class Shoe
  include Mongoid::Document
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  field :title
  field :photo
  field :link
  field :price
  field :gender
  field :sizes, type: Array, default: []
  field :colors, type: Array, default: []

  def as_indexed_json
    as_json({only: [:title, :gender, :colors, :price, :sizes]})
  end

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :title, analyzer: 'bulgarian'
      indexes :colors, analyzer: 'bulgarian'
      indexes :gender, analyzer: 'bulgarian'
    end
  end

  def self.more_like_this(shoe)
    __elasticsearch__.search(
      {
        query: {
          bool: {
              must: {match_all: { }},
              should: [
                # {
                #   more_like_this: {
                #     fields: ['title'],
                #     like: [
                #       {
                #         _index: 'shoes',
                #         _type: 'shoe',
                #         _id: shoe.id
                #       }
                #     ],
                #     boost: 2.0
                #   }
                # },
                {
                  more_like_this: {
                    fields: ['gender'],
                    like: [
                      {
                        _index: 'shoes',
                        _type: 'shoe',
                        _id: shoe.id
                      }
                    ],
                  }
                }
              ],
              minimum_should_match: 1
          }
        }
      }
    )
  end
end

# Delete the previous shoes index in Elasticsearch
Shoe.__elasticsearch__.client.indices.delete index: Shoe.index_name rescue nil

# Create the new index with the new mapping
Shoe.__elasticsearch__.client.indices.create \
  index: Shoe.index_name,
  body: { settings: Shoe.settings.to_hash, mappings: Shoe.mappings.to_hash }

# Index all shoe records from the DB to Elasticsearch
Shoe.import

在project / config / initializers / elasticsearch.rb中我有:

config = {
  host: "http://localhost:9200/",
  transport_options: {
    request: { timeout: 5 }
  },
}

if File.exists?("config/elasticsearch.yml")
  config.merge!(YAML.load_file("config/elasticsearch.yml").symbolize_keys)
end

Elasticsearch::Model.client = Elasticsearch::Client.new(config)

unless Shoe.__elasticsearch__.index_exists?
  Shoe.__elasticsearch__.create_index! force: true
  Shoe.import
end

在project / config / mongoid.yml中我有:

    development:
      # Configure available database clients. (required)
      clients:
        # Defines the default client. (required)
        default:
          # Defines the name of the default database that Mongoid can connect to.
          # (required).
          database: cornersport
          # Provides the hosts the default client can connect to. Must be an array
          # of host:port pairs. (required)
          hosts:
            - localhost:27017
    test:
      clients:
        default:
          database: calceus_test
          hosts:
            - localhost:27017
          options:
            read:
              mode: :primary
            max_pool_size: 1

现在,在rails控制台中,在运行elasticsearch的情况下,我调用Shoe.more_like_this(some_shoe_object).results.to_a并返回[]。首先我的查询包括评论部分,但是当我没有得到任何结果时,我评论了它并且我仍然没有得到任何结果,尽管数据库中有更多的记录与some_shoe_object的性别相同。它不应该返回一些东西吗?我使用elasticsearch错了吗?我是初学者。请帮忙!

2 个答案:

答案 0 :(得分:0)

看起来您的索引是通过初始化文件elasticsearch.rb创建的,并且您尚未指定设置。

尝试将初始化程序文件更改为

config = {
  host: "http://localhost:9200/",
  transport_options: {
    request: { timeout: 5 }
  },
}

if File.exists?("config/elasticsearch.yml")
  config.merge!(YAML.load_file("config/elasticsearch.yml").symbolize_keys)
end

Elasticsearch::Model.client = Elasticsearch::Client.new(config)

unless Shoe.__elasticsearch__.index_exists?
  Shoe.__elasticsearch__.client.indices.create index: Shoe.index_name,
   body: { settings: Shoe.settings.to_hash, mappings: Shoe.mappings.to_hash }
  Shoe.import
end

同样从您的shoe.rb文件中删除索引创建/删除部分

# Delete the previous shoes index in Elasticsearch
Shoe.__elasticsearch__.client.indices.delete index: Shoe.index_name rescue nil

# Create the new index with the new mapping
Shoe.__elasticsearch__.client.indices.create \
  index: Shoe.index_name,
  body: { settings: Shoe.settings.to_hash, mappings: Shoe.mappings.to_hash }

# Index all shoe records from the DB to Elasticsearch
Shoe.import

重新启动服务器,ES,然后重试!

<强>更新

尝试更改more_like_it这样的句子:

more_like_this: {
  fields: ['gender'],
    like: [
      {
        _index: 'cornersport',
        _type: 'shoes',
        _id: shoe.id
       },
       shoe.gender
     ]
}

答案 1 :(得分:0)

我这样编辑:

def self.more_like_this(shoe)
  __elasticsearch__.search(
    {
      query: {
        bool: {
            must: {match_all: { }},
            should: [
              {
                more_like_this: {
                  fields: ['gender'],
                  docs: [
                    {
                      _index: 'shoes',
                      _type: 'shoe',
                      _id: shoe.id
                    }
                  ],
                  ids: [shoe.id]
                }
              }
            ],
            minimum_should_match: 1
        }
      }
    }
  )
end

现在效果很好。刚刚将like替换为docs,并添加了ids,如下所示:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-mlt-query.html 到现在为止,我试图让它按照这里解释的方式工作: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html

我正在使用elasticsearch-2.2.0,所以我不知道2.2.0的功能如何对我不起作用,而是1.4功能。也许elasticsearch-rails适用于1.4。