Skip to content
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

Closed
agustik opened this issue Sep 13, 2017 · 8 comments
Closed

Way to toggle filter #150

agustik opened this issue Sep 13, 2017 · 8 comments

Comments

@agustik
Copy link

agustik commented Sep 13, 2017

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" : { ... } } ');

@ferronrsmith
Copy link
Collaborator

ferronrsmith commented Sep 13, 2017

// 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

@agustik
Copy link
Author

agustik commented Sep 13, 2017

Yes. I know how to create a query with filter.
The question is how can i remove the filter? Or is there a way to toggle filter / Remove the filter if it is already on.

@ferronrsmith
Copy link
Collaborator

ferronrsmith commented Sep 13, 2017

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')
}

@agustik
Copy link
Author

agustik commented Sep 13, 2017

Well.. there is still no way to remove the filter if it has been applied..

User clicks "Filter by myFiled:myValue"

 builder.filter('term', 'myField', 'myValue')

User clicks "Filter by myFiled:myValue" again

 // Does the query have the filter already?
 builder.removeFilter('term', 'myField', 'myValue')

@ferronrsmith
Copy link
Collaborator

ferronrsmith commented Sep 13, 2017

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())
}

@ferronrsmith
Copy link
Collaborator

Also added suggestion in #123 which is to use monkey-patching.

@agustik
Copy link
Author

agustik commented Sep 14, 2017

Thanks. I might look into that!.

@johannes-scharlach
Copy link
Collaborator

Also, is there a way to load elasticsearch query into bodybuilder object?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants