question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Creating a filter aggregation dynamically?

See original GitHub issue

Hello!

So Iā€™m trying to create a query based on the amount of filter items selected. So the default filter for a specific list type looks like:

bodybuilder()
    .aggregations('terms', 'listingType.keyword', 'Type')
    .build()

Which works as expected. I have the aggregation bucket I need.

What If an item was selected? Iā€™d want to add that filter to it.

This is what I want it to look like:

bodybuilder()
    .addFilter('term', 'itemTypeId', 2)
    .aggregations('terms', 'listingType.keyword', 'Type')
    .build()

Or if multiple items were selected thenā€¦

bodybuilder()
    .addFilter('term', 'itemTypeId', id)
    .addFilter('term', 'moo.keyword', moo)
    .addFilter('term', 'make.keyword', make)
    .aggregations('terms', 'listingType.keyword', 'Type')
    .build()

I have an array of selected filters. I figured I could do something like:

const activeFilters = [{type: 'make.keyword, value: 'SAMSUNG'}, {type: 'moo.keyword', value: 'moo'}]

if (activeFilters) {
    activeFilters.map(item => {
        addFilter(item.type, item.value)
    }
}

//OR

bodybuilder()
    activeFilters.map(item => addFilter(item.type, item.value)
    .aggregation('terms' , 'listingType.keyword', 'Type')
    .build()

Something along those lines. Any recommendations?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ferronrsmithcommented, May 3, 2019

it would be

const activeFilters = [{type: 'make.keyword', value: 'SAMSUNG'}, {type: 'moo.keyword', value: 'moo'}]
const builder = bodybuilder();
activeFilters.forEach(item => builder.addFilter(item.type, item.value))
builder.aggregation('terms', 'listingType.keyword', 'Type').build()   

You should use forEach instead of map. Map is really mutation iterator vs forEach which just loops through.

0reactions
ferronrsmithcommented, May 3, 2019

ok np šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically add conditions or filters to aggregate ...
Hi, I would like to add or remove conditions or filters based on the input to the function. For ex, if a subcategory...
Read more >
View filter effects on dynamic aggregation
View filter effects on dynamic aggregation. Dynamic aggregation occurs when an attribute is moved between the report layout and the Report Objects pane....
Read more >
Elasticsearch Filter Dynamically Aggregated Fields
Save this question. Show activity on this post. I am trying to serach/filter results based on front-end selections in available acategories:
Read more >
How to dynamically filter on Aggregate Values
I have got transactional data of names and prices. I like to create a filter on the names whose TOTAL PRICE is above...
Read more >
Filter over dynamic nested fields with aggregation
I have products with dynamic attributes (filters). I need to get dynamically counts for each value for the filter according to the searchĀ ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found