Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:17
Show Gist options
  • Save up1/e2bae6c453e3fe9e1200 to your computer and use it in GitHub Desktop.
Save up1/e2bae6c453e3fe9e1200 to your computer and use it in GitHub Desktop.
Elasticsearch :: Distinct Result
เท่าที่มา สิ่งที่บอกว่า Solr ทำได้คือ แบบนี้
http://blog.jteam.nl/2009/10/20/result-grouping-field-collapsing-with-solr/
ทำให้สามารถ distinct result กลับออกมาเพียง กลุ่มละ 1 ตัวได้
ตัวอย่างข้อมูล 3 ข้อมูล
PUT ec/product/1
{
"name" : "ABC",
"price" : 123,
"category" : ["1", "2"]
}
PUT ec/product/2
{
"name" : "ABC",
"price" : 1,
"category" : ["1", "2"]
}
PUT ec/product/3
{
"name" : "XYZ",
"price" : 1,
"category" : ["1", "2"]
}
สิ่งที่ต้องการคือ จะส่งข้อมูล หรือ ผลลัพธ์กลับมาเพียง 2 คือ ABC กับ XYZ
***** แต่ Feature นี้มันทำให้ SOLR ไม่รองรับ Distributed Search ได้ นั่นคือ ทำให้มันไม่สามารถ Scale ได้ในแนวนอน หรือ เพิ่มเครื่อง ( ทำได้เพียงขยายเครื่อง )
ซึ่งทำให้ใน Elasticsearch ไม่มีความสามารถนี้ อ้างอิงจาก Issue
https://github.com/elastic/elasticsearch/issues/256
แต่ถ้ายังต้องการดึงข้อมูลจาก Elasticsearch ในรูปแบบนี้
สามารถทำได้ 2 แบบคือ
แบบที่ 1. ใช้ Aggregation TopHit แบบนี้
GET ec/_search?search_type=count
{
"aggs": {
"terms": {
"terms": {
"field": "name",
"size" : 10
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"_source": {
"include": [
"name", "price", "category"
]
}
, "size" : 1
}
}
}
}
}
}
แบบที่ 2. ต้องใช้การกรองข้อมูล หรือ จัดกลุ่มข้อมูลใน code เอาเอง
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment