首页 文章

如何使用Tire / ElasticSearch检索评级/类别数量?

提问于
浏览
0

我试图从ElasticSearch查询中检索嵌套数据,基本上试图从Movie模型中获取:

title
ratings
categories

现在,我尝试了2个轮胎设置,但两者都只提供电影 Headers ,而不是评级或类别 . 例如 . 索引似乎只是:

curl -X POST "http://localhost:9200/movies/_search?pretty=true" -d '
 {
    "query" : { "query_string" : {"query" : "t*"} },
    "facets" : {
      "categories" : { "terms" : {"field" : "categories"} }
    }
  }
'
{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "movies",
      "_type" : "movie",
      "_id" : "13",
      "_score" : 1.0, "_source" : {"description":null,"id":13,"title":"Tiny Plastic Men"}
    }, {
      "_index" : "movies",
      "_type" : "movie",
      "_id" : "32",
      "_score" : 1.0, "_source" : {"description":null,"id":32,"title":"The Extreme Truth"}
    }, {
      "_index" : "movies",
      "_type" : "movie",
      "_id" : "39",
      "_score" : 1.0, "_source" : {"description":null,"id":39,"title":"A Time of Day"}
    } ]
  },
  "facets" : {
    "categories" : {
      "_type" : "terms",
      "missing" : 3,
      "total" : 0,
      "other" : 0,
      "terms" : [ ]
    }
  }

这是我的电影模型:

class Movie  :categorizations
  belongs_to :user

  has_many :ratings

  mapping do
     indexes :id, type: 'integer'
     indexes :title, boost: 40
     indexes :description, analyzer: 'snowball'
     indexes :categories do
       indexes :id, type: 'integer'
       indexes :name, type: 'string', index: 'not_analyzed'
     end
     indexes :ratings do
       indexes :id, type: 'integer'
       indexes :stars, type: 'integer'
     end
   end

end

我的搜索实验的分支在这里:https://github.com/mulderp/moviedb/tree/categories

我怎样才能为搜索工作制作方面,例如类型 - >评级 .

1 回答

  • 2

    你're not actually indexing the categories or ratings: they'在映射中,但轮胎的默认实现 to_indexed_json 只调用activerecord提供的 to_json 方法 .

    例如,您需要覆盖它以包含类别/评级信息

    def to_indexed_json
      to_json(:include => [:categories, :ratings])
    end
    

相关问题