Dynamic input generation based on nested object values #1623
-
Hello! I am developing an application that needs to support dynamically generated search fields for arbitrary sets of key/value pairs. Basically, we are doing search on a database of products, and in the backend the product has an "attributes" property that our marketing department populates with arbitrary key/value pairs. So maybe a product has { "color": "red", "size": "10cm" } and so on. Because the keys can appear or disappear from day to day as our data people modify our product catalog, the search inputs have to dynamically reflect what's in the data and can't be hard-coded at all. The way these attributes are represented in our Elasticsearch data looks like this:
"attributes" has a "nested" mapping type, and "key" and "value" are both mapped as "keyword". My goal is to render one SingleItemDropdown instance for each unique value of "attributes.key" within my result set, containing each unique value of "attributes.value" associated with that key. I have succeeded in generating my inputs by appending an aggregation in my ReactiveBase's transformRequest attribute, but I am stuck on how to populate them with their values. Here is what my SingleItemDropdown definition looks like right now:
I can see in my network traffic that this query is working correctly: It retrieves the set of all unique values of "attributes.value" where "attributes.key" is the currently relevant key. So far so good. But now I'm stuck. I can't seem to figure out a way to change this query so that the results will be rendered by the component, and even if I use the I expect there must be something fundamentally wrong with my approach, as I'm new to React, Reactive Search, and Elasticsearch. I'd be very grateful if anyone could point me in the right direction. Thank you! ~ Benjamin |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I seem to have found a solution, which I'll document here in case it's useful to someone else. The problem is that the customized My solution is to use the transformResponse hook in the ReactiveBase element to restructure the result so that it is shaped how ReactiveSearch expects it:
|
Beta Was this translation helpful? Give feedback.
I seem to have found a solution, which I'll document here in case it's useful to someone else.
The problem is that the customized
defaultQuery
produces a result that is not structured in the way that ReactiveSearch expects. Specifically, because there is an outer filter aggregation and then an inner terms aggregation, the result that ReactiveSearch is expecting to be atresponse.aggregations.reactivesearch_nested["attributes.value"]
is nested a level deeper, atresponse.aggregations.reactivesearch_nested.attribute_keys_filter.buckets[0]["attributes.value"]
.My solution is to use the transformResponse hook in the ReactiveBase element to restructure the result so that it is shaped how React…