如何使用Rails和ElasticSearch构建嵌套结构?

时间:2021-10-13 14:30:51

I have a Feature model that belongs_to FeatureKey and FeatureValue.

我有一个功能模型,其属于FeatureKey和FeatureValue。

FeatureKey#name => 'color'
FeatureValue#name => 'red'

I would like to generate a nested aggregations structure to build a shopping cart filter (facet) navigation.

我想生成一个嵌套的聚合结构来构建购物车过滤器(facet)导航。

Ideally, the structure would like something like

理想情况下,结构会像

{ features: {
  { key: color, values: [ red, blue, yellow ] },
  { key: size, values: [ large, medium, small ]}
}}

Can anyone anyone suggest how I can do this?

任何人都可以建议我怎么做到这一点?

1 个解决方案

#1


What I'm currently using:

我目前使用的是:

{  
    "size":1000,
    "fields":[  
        "id",
        "name",
        "price"
    ],
    "query":{  
        "filtered":{  
            "filter":{  
                "bool":{  
                    "must":[  
                        {  
                            "term":{  
                                "categories":4838
                            }
                        }
                    ]
                }
            }
        }
    },
    "aggs":{  
        "price":{  
            "stats":{  
                "field":"price"
            }
        },
        "discounted":{  
            "terms":{  
                "field":"discounted"
            }
        },
        "stock":{  
            "filter":{  
                "range":{  
                    "stock":{  
                        "gt":0
                    }
                }
            }
        },
        "colour":{  
            "terms":{  
                "field":"colour"
            }
        },
        "size":{  
            "terms":{  
                "field":"size"
            }
        }
    }
}

Add or remove aggregations as you wish. You most likely wish to filter by category, so I left that in for simplicity's sake.

根据需要添加或删除聚合。你很可能希望按类别进行过滤,所以为了简单起见,我把它留了下来。

#1


What I'm currently using:

我目前使用的是:

{  
    "size":1000,
    "fields":[  
        "id",
        "name",
        "price"
    ],
    "query":{  
        "filtered":{  
            "filter":{  
                "bool":{  
                    "must":[  
                        {  
                            "term":{  
                                "categories":4838
                            }
                        }
                    ]
                }
            }
        }
    },
    "aggs":{  
        "price":{  
            "stats":{  
                "field":"price"
            }
        },
        "discounted":{  
            "terms":{  
                "field":"discounted"
            }
        },
        "stock":{  
            "filter":{  
                "range":{  
                    "stock":{  
                        "gt":0
                    }
                }
            }
        },
        "colour":{  
            "terms":{  
                "field":"colour"
            }
        },
        "size":{  
            "terms":{  
                "field":"size"
            }
        }
    }
}

Add or remove aggregations as you wish. You most likely wish to filter by category, so I left that in for simplicity's sake.

根据需要添加或删除聚合。你很可能希望按类别进行过滤,所以为了简单起见,我把它留了下来。