简体   繁体   中英

ElasticSearch + Tire searching only on id field

I've looked everywhere but nothing can help me. And I can't figure out what is the problem.

my model

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' gives no results,but there are Article with title "love", I've tried to build many requests but nothing works.

all times the same results:

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

But If I type: Article.search "cbc267c955464f22d72a0100" it gives me article with title: "love"

So it seems to me that tire create indexes only on ID field, regardless mapping indexes on model.

When I recreate indexes

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

my mapping becomes:

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

UPDATED

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

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

After implementing this initialize, all seems to work fine

I too encountered such problems while using tire . After deleting the index, you can try Article.create_elasticsearch_index and Article.tire.index.import Article.all .

The _source field in your indexed document should have title , id and body included in it, which should make it available for search.

Anyways, tire is getting retired as elasticsearch gem has now been released.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM