2013-12-17 71 views
1

我有我想要运行在ElasticSearch在聚合查询以下JSON结构:ElasticSearch聚合查询可能吗?

{ 
    "user_id": 1, 
    "gender_id": 1, 
    "locale_id": 6, 
    "age": 38, 
    "hometown_city_id": 1002, 
    "current_city_id": 672, 
    "books": [ 
     { 
      "b_id": 2065, 
      "b_name": "aut qui assumenda ut", 
      "b_cat": 56 
     }, 
     { 
      "b_id": 2527, 
      "b_name": "libero et laudantium", 
      "b_cat": 132 
     }, 
     ... 
    ] 
} 

我基本上想要做的就是为例子来说明前5本书与“gender_id”用户:“ 1“(男)已阅读,其中也读有”b_id“的书:2065.

作为ElasticSearch的初学者我还没有遇到过解决方案。我知道有v1.0的aggegation模块(https://github.com/elasticsearch/elasticsearch/issues/3300),但我无法运行。

如果有人已经实现了类似的东西,请帮助!提前感谢!

+0

我认为你需要创建一个过滤器(gender_id和b_id)并让elasticsearch在书上创建一个方面。 – phoet

回答

0

感谢@mconlin的提示。我得到它的工作的查询如下:

{ 
    "query": { 
     "filtered": { 
      "filter": { 
       "and" : [ 
        { "term": { "gender_id": 1 } }, 
        { "term": { "books.b_id": 2065} } 
       ] 
      } 
     } 
    }, 
    "facets": { 
     "book_cnt": { 
      "terms": { 
       "field": "books.b_id", 
       "size": 5 
      } 
     } 
    } 
} 

原来这是更好地使用过滤器在查询本身,而不是作为一个单独的方面。

1

也许这样的事情(约嵌套books.field不知道):

{ 
    "query" : { 
     "match_all" : { } 
    }, 
    "facets" : { 
     "filter_one" : { 
      "filter" : { 
       "and" : [ 
         {"filter" : {"term" : { "gender_id" : 1 }}, 
         {"filter" : {"term" : { "books.b_id" : 2065 }}, 
       ] 
      } 
     }, 
     "book_cnt" : { 
      "terms" : { 
       "field" : "books.b_name", 
       "size" : 5 
      } 
     } 
    } 
}