【问题标题】:ElasticSearch + Tire searching only on id fieldElasticSearch + Tire 仅在 id 字段上搜索
【发布时间】:2013-10-18 03:14:36
【问题描述】:

我到处寻找,但没有任何帮助。而且我不知道是什么问题。

我的模特

class Article
 include Mongoid::Document
 include Mongoid::Timestamps


 include Tire::Model::Search
 include Tire::Model::Callbacks
  mapping do
    indexes :_id, :index => :not_analyzed
    indexes :title
    indexes :body
  end


 def to_indexed_json
   self.to_json
 end

http://localhost:9200/articles/_mapping
{
  "articles": {
    "article": {
      "properties": {
        "$oid": {
          "type": "string"
        },
        "body": {
          "type": "string"
        },
        "title": {
          "type": "string"
        }
      }
    }
  }
}

Article.search 'love' 没有结果,但是有标题为“爱”的文章,我尝试构建了很多请求,但没有任何效果。

结果都是一样的:

"hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}

但如果我输入:Article.search "cbc267c955464f22d72a0100",它会给我标题:“爱”的文章

所以在我看来,轮胎只在 ID 字段上创建索引,而不管模型上的映射索引。

当我重新创建索引时

Article.index_name
  => "articles" 
Tire.index('articles').delete
  => true 
Article.import

我的映射变成:

{
  "articles": {
    "article": {
      "properties": {
        "$oid": {
          "type": "string"
        }
      }
    }
  }
}

更新

module BSON
  class ObjectId
    def as_json(*args)
      to_s()
    end

    def to_json(*args)
      MultiJson.encode(as_json())
    end
  end
end

实现这个初始化后,一切似乎都正常

【问题讨论】:

    标签: ruby-on-rails mongoid elasticsearch tire


    【解决方案1】:

    我在使用tire时也遇到了这样的问题。删除索引后,可以试试Article.create_elasticsearch_indexArticle.tire.index.import Article.all

    您的索引文档中的_source 字段应包含titleidbody,这应使其可供搜索。

    无论如何,tire 即将退休,因为 elasticsearch gem 现已发布。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2017-04-07
      • 2013-12-17
      • 2012-12-09
      • 2019-01-20
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多