ผลลัพธ์การค้นหาแบบยืดหยุ่นจัดกลุ่มตามหลายฟิลด์

สวัสดี ขอบคุณล่วงหน้า ฉันเป็นคนใหม่ใน ElasticSearch และฉันมีคำถามเกี่ยวกับข้อความค้นหา ฉันมีรายการสื่อที่มีหลายช่อง:

{

"ชื่อเรื่อง": "xxx"

"รหัสผู้ใช้": "1"

"image": "zzz.jpg"

"เมือง": "ปารีส"

.....

}

ฉันจำเป็นต้องได้รับสื่อทั้งหมดสำหรับเมืองปารีส และให้คะแนนสำหรับจำนวน x รูปภาพแรกสำหรับผู้ใช้ทุกคน ดังนั้นผลลัพธ์จะไม่ถูกดึงมาจากผู้ใช้คนเดียวกัน ตัวอย่างเช่น:

สมมติว่าเราต้องการผลักดันคะแนนของ 2 ภาพแรกสำหรับผู้ใช้แต่ละคน:

ผู้ใช้1 รูปภาพ1

ผู้ใช้1 ภาพที่2

ผู้ใช้1 ภาพที่3

ผู้ใช้1 ภาพที่4

ผู้ใช้2 ภาพที่1

ผู้ใช้2 รูปภาพ2

ผู้ใช้2 รูปภาพ3

ผู้ใช้2 รูปภาพ4

ผู้ใช้2 รูปภาพ5

ผู้ใช้2 ภาพที่6

ผู้ใช้ 3 ภาพที่ 1

ผู้ใช้3 รูปภาพ2

ผู้ใช้3 รูปภาพ3

........

ผลลัพธ์ที่คาดหวัง:

ผู้ใช้1 รูปภาพ1

ผู้ใช้1 ภาพที่2

ผู้ใช้2 ภาพที่1

ผู้ใช้2 รูปภาพ2

ผู้ใช้ 3 ภาพที่ 1

ผู้ใช้3 รูปภาพ2

ผู้ใช้1 ภาพที่3

ผู้ใช้1 ภาพที่4

ผู้ใช้2 รูปภาพ3

ผู้ใช้2 รูปภาพ4

ผู้ใช้2 รูปภาพ5

ผู้ใช้2 ภาพที่6

ผู้ใช้3 รูปภาพ3


person A.Malak    schedule 14.04.2016    source แหล่งที่มา
comment
ยินดีต้อนรับสู่ StackOverflow ขณะนี้คำถามของคุณไม่ชัดเจนว่าคุณกำลังถามอะไร ลองเพิ่มโค้ดและระบุจุดที่คุณมีปัญหาโดยเฉพาะ   -  person buczek    schedule 14.04.2016


คำตอบ (1)


คุณสามารถลองค้นหาดังต่อไปนี้:

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": 2 ใน @image aggs ให้คุณระบุจำนวนภาพ ผลลัพธ์จะเป็นดังนี้:

    {
   "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"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}
person Кирилл Полищук    schedule 14.04.2016
comment
ขอบคุณเพื่อนสำหรับคำตอบ แต่ฉันพยายามที่จะได้ผลลัพธ์จากจำนวนการเข้าชมที่ไม่รวมอยู่ในผลรวม - person A.Malak; 18.04.2016