-
-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Way to toggle filter #150
Comments
// Appends to query.
// Documentation : http://bodybuilder.js.org/docs/
bodybuilder()
.query('term', 'myField', 'myValue')
.build() Short answer for the second part is no. You can't construct a bodybuilder object from ES query DSL |
Yes. I know how to create a query with filter. |
That's not supported. Why not use a if block in your code ? // filter state manged by some other context
const toggleFilter = (builder, filter) => {
// make these args, think you get the gist
if(filter) builder.filter('term', 'myField', 'myValue')
} |
Well.. there is still no way to remove the filter if it has been applied.. User clicks "Filter by myFiled:myValue"
User clicks "Filter by myFiled:myValue" again
|
It all depends on your setup. If you're preserving the state of the builder then yes, but if you're recreating the builder each time then it's no. // filter state manged by some other context
const toggleFilter = (builder, filter, params) => {
// make these args, think you get the gist
if(filter) builder.filter(params.type, params.field, params.value)
}
/// build query
const buildQuery = (field, value, shouldFilter) => {
var builder = bodybuilder()
// construct whatever here
// context switching
toggleFilter(builder, shouldFilter, {type:'term', field, value})
return queryES(builder.build())
} |
Also added suggestion in #123 which is to use monkey-patching. |
Thanks. I might look into that!. |
Actually there is a way to do this, @agustik ! We haven't documented this yet, because nobody knew about it, but with bodybuilder's flexible syntax it's actually quite easy to add object to your query: const existingQuery = {
term: { foo: 'bar' }
}
bodybuilder()
.query('bool', 'must', existingQuery)
.build() You can even pass in an array of existing queries (because ES allows for that). This approach won't always get you the simplest queries, but the behaviour of the query is 100% as desired. |
Hey there!
I can't see that there is a way to toggle filter/query
query.filter('term', 'myField', 'myValue') only appends to the filter array.
It would be nice to do query.toggleFilter('term', 'myField', 'myValue');
Also, is there a way to load elasticsearch query into bodybuilder object?
like
var query = bodybuilder().loadQueryFromJson(' { "query" : { ... } } ');
The text was updated successfully, but these errors were encountered: