简体   繁体   English

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

[英]How to build a nested structure with Rails and ElasticSearch?

I have a Feature model that belongs_to FeatureKey and FeatureValue. 我有一个belongs_to beauty_to 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. 我想生成一个嵌套的聚合结构来构建购物车过滤器(构面)导航。

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? 谁能建议我该怎么做?

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. 您最有可能希望按类别进行过滤,因此为了简单起见,我将其保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM