elasticsearch查询结果按多个字段分组

时间:2016-04-14 15:04:03

标签: elasticsearch

你好,先谢谢你,我对弹性搜索很新,我对查询有疑问,我有一个有很多字段的媒体列表:

{

“title”:“xxx”

“userId”:“1”

“image”:“zzz.jpg”

“city”:“paris”

.....

}

我需要获取城市巴黎的所有媒体,并为每个用户推送前x个图像的分数,以便不会从同一个用户获取结果。例如:

让我们说我们想要为每个用户推送前2张图片的分数:

user1 image1

user1 image2

user1 image3

user1 image4

user2 image1

user2 image2

user2 image3

user2 image4

user2 image5

user2 image6

user3 image1

user3 image2

user3 image3

........

预期产出:

user1 image1

user1 image2

user2 image1

user2 image2

user3 image1

user3 image2

user1 image3

user1 image4

user2 image3

user2 image4

user2 image5

user2 image6

user3 image3

1 个答案:

答案 0 :(得分:0)

您可以尝试以下查询:

GET _search
{
  "query": {
    "query_string": {
      "query": "*"
    }
  },
  "size": 0, 
  "aggs": {
    "@user_id": {
      "terms": {
        "field": "user_id",
        "size": 0,
        "order": {
              "_term": "asc"
            }
      },
      "aggs": {
        "@image": {
          "terms": {
            "field": "image",
            "size": 2,
            "order": {
              "_term": "asc"
            }
          },
          "aggs": {
            "@source": {
              "top_hits": {
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}

" size":@image aggs中的2允许您指定图像的数量。 输出如下:

    {
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 16,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "@user_id": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "user1",
               "doc_count": 4,
               "@image": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 2,
                  "buckets": [
                     {
                        "key": "image1",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVaph2LY9bI6YPTNWp",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user1",
                                       "image": "image1"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "image2",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVareVLY9bI6YPTPCg",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user1",
                                       "image": "image2"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            },
            {
               "key": "user2",
               "doc_count": 4,
               "@image": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 2,
                  "buckets": [
                     {
                        "key": "image1",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVau9JLY9bI6YPTSPx",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user2",
                                       "image": "image1"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "image2",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVavh1LY9bI6YPTSP0",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user2",
                                       "image": "image2"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            },
            {
               "key": "user3",
               "doc_count": 4,
               "@image": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 2,
                  "buckets": [
                     {
                        "key": "image1",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVayTFLY9bI6YPTVZA",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user3",
                                       "image": "image1"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "image2",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVazP4LY9bI6YPTWnK",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user3",
                                       "image": "image2"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            },
            {
               "key": "user4",
               "doc_count": 4,
               "@image": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 2,
                  "buckets": [
                     {
                        "key": "image1",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVa16pLY9bI6YPTbgL",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user4",
                                       "image": "image1"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "image2",
                        "doc_count": 1,
                        "@source": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "test",
                                    "_type": "test",
                                    "_id": "AVQVa2a3LY9bI6YPTcuX",
                                    "_score": 1,
                                    "_source": {
                                       "user_id": "user4",
                                       "image": "image2"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}
相关问题