我们有一组数据,如下面的ElasticSearch中所示。它的产品列表是从电子商务后端索引的。
“hts” : [
{
"_index": "test",
"_type": "commerce_products_index",
"_id": "466174",
"_score": 1,
"_source": {
"id": 261776,
"changed": "1516367458",
"commerce_price:amount": "2700",
"field_product_node:nid": [
"66741"
],
"field_uom_type": "g",
"field_weight": "337",
"product_id": "261776",
"title": "Brown Lobia",
}
},
{
"_index": "test",
"_type": "commerce_products_index",
"_id": "466175",
"_score": 1,
"_source": {
"id": 261781,
"changed": "1526448108",
"commerce_price:amount": "5900",
"field_product_node:nid": [
"66741"
],
"field_uom_type": "g",
"field_weight": "339",
"product_id": "261781",
"title": "Brown Lobia",
}
},
{
"_index": "test",
"_type": "commerce_products_index",
"_id": "466176",
"_score": 1,
"_source": {
"id": 466176,
"changed": "1515568794",
"commerce_price:amount": "5400",
"commerce_store": "651",
"field_product_node:nid": [
"84651"
],
"field_uom_type": "g",
"field_weight": "337",
"product_id": "466176",
"title": "Maggi Rich Tomato Ketchup",
}
}
]
如您所见,前两个文档的field_product_node:nid
相同。 (即66741)。这是同一产品的两种不同尺寸(变化)。
在搜索中,我们希望将这些相同的产品展示为一个。对于它,我们需要使用字段field_product_node:nid
的结果,这对于每个相同的产品都是唯一的。例如,白米1公斤&白米500g在field_product_node:nid
中具有相同的值。因此,搜索时,产品详细信息应分组在一个nid下。
目前,我们每个产品都会获得不同的文档。但是,我们希望将这两种产品作为单个文档。
我们尝试了以下查询:
GET /commerce_products_index/_search
{
"size": 20,
"query" : {
"bool": {
"must": [
{ "match": { "commerce_store": "651"}}
]
}
},
"aggs": {
"group_by_node": {
"terms": {
"field": "field_product_node:nid"
}
}
}
}
GET /commerce_products_index/_search
{
"aggregations": {
"grp_report": {
"terms": {
"field": "field_product_node:nid"
},
"aggregations": {
"nested_node": {
"nested": {
"path": "node"
},
"aggregations": {
"filters_customer": {
"filters": {
"filters": [
{
"match": {
"node.commerce_store": "651"
}
}
]
}
}
}
}
}
}
},
"query" : {
"bool": {
"must": [
{ "match": { "commerce_store": "651"}}
]
}
},
"from": 0,
"size": 100
}
我们无法找出正确的方法。如果这不可能,我们必须继续重新开发索引部分,并尝试将具有相同nid的多个产品索引到单个文档中。这将是一个相当大的改写。
答案 0 :(得分:1)
我试过以下查询。它适用于我们的问题。
GET /commerce_products_index/_search
{
"size": 20,
"aggs": {
"by_node": {
"terms": {
"field": "field_product_node:nid",
"size": 11,
"order": {
"max_score": "desc"
}
},
"aggs": {
"by_top_hit": {
"top_hits": {
"size": 15
}
},
"max_score": {
"max": {
"field": "field_product_node:nid",
"script": "_score"
}
}
}
}
}
}
这可能有助于遇到同样问题的人。