如何读取分面搜索查询的JSON输出?

时间:2013-05-04 21:42:41

标签: elasticsearch

我正在播放属于某种类型并且有多个评级的电影。使用ElasticSearch,我想先在Genres上进行分面搜索,然后再进行评分。

我在这里读到了这个想法:http://www.elasticsearch.org/guide/reference/api/search/facets/

但我很困惑如何理解这个Curl查询的输出:

curl -X POST "http://localhost:9200/movies/_search?pretty=true" -d '
 {
    "query" : { "query_string" : {"query" : "T*"} },
    "facets" : {
      "categories" : { "terms" : {"field" : "categories"} }
    }
  }
'
{
  "took" : 35,
  "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" : {"category_id":2,"created_at":"2013-05-03T16:40:21Z","description":null,"title":"Tiny Plastic Men","updated_at":"2013-05-03T16:40:21Z","user_id":null}
    }, {
      "_index" : "movies",
      "_type" : "movie",
      "_id" : "32",
      "_score" : 1.0, "_source" : {"category_id":14,"created_at":"2013-05-03T16:55:02Z","description":null,"title":"The Extreme Truth","updated_at":"2013-05-03T16:55:02Z","user_id":null}
    }, {
      "_index" : "movies",
      "_type" : "movie",
      "_id" : "39",
      "_score" : 1.0, "_source" : {"category_id":7,"created_at":"2013-05-03T16:55:02Z","description":null,"title":"A Time of Day","updated_at":"2013-05-03T16:55:02Z","user_id":null}
    } ]
  },
  "facets" : {
    "categories" : {
      "_type" : "terms",
      "missing" : 3,
      "total" : 0,
      "other" : 0,
      "terms" : [ ]
    }
  }

我正在制作一些以'T'开头的电影,但另外我会期待来自流派/类别'Thriller'的电影。

因此,我可以从上面的JSON中读到什么?

2 个答案:

答案 0 :(得分:3)

您的方面似乎与您应该使用的文档中的任何字段都不匹配:

curl -X POST "http://localhost:9200/movies/_search?pretty=true" -d '
 {
    "query" : { "query_string" : {"query" : "T*"} },
    "facets" : {
      "categories" : { "terms" : {"field" : "category_id"} }
    }
  }
'

然后你可以获得category_id列表和每个category_id

中的文档数量

答案 1 :(得分:2)