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

RFC: Nested Obj Filters #135

Open
erhathaway opened this issue Apr 21, 2021 · 10 comments
Open

RFC: Nested Obj Filters #135

erhathaway opened this issue Apr 21, 2021 · 10 comments

Comments

@erhathaway
Copy link
Contributor

This library currently does not support nested object filters. Additionally, I believe the current abstractions are partially incompatible with nested objects. Below is a proposal to support it:

Introspection step:

  • The object key should return a nestedObj type.

  • For each key in the nested obj:

    { my_parent_key: { 
         nested_key_1: <some value>,
         nested_key_2: <some value>,
         nested_key_3: <some value>
    }
    
    

    The fields and their type would be:

    my_parent_key.nested_key_1 === 'nestedObj'
    my_parent_key.nested_key_1 === 'nestedObj'
    my_parent_key.nested_key_1 === 'nestedObj'
    

    Filter step:

    A new attribute is added to the nestedObj filter kind called nestedObjMap. This is a map of parent keys to the nested object. This allows you to tell that the 3 keys above are all part of the same nested object.

     {
        my_parent_key.nested_key_1: 
              { my_parent_key.nested_key_1: <type>  my_parent_key.nested_key_2: <type>,  my_parent_key.nested_key_3: <type> },
        my_parent_key.nested_key_2: 
              { my_parent_key.nested_key_1: <type>  my_parent_key.nested_key_2: <type>,  my_parent_key.nested_key_3: <type> },
        my_parent_key.nested_key_3: 
              { my_parent_key.nested_key_1: <type>  my_parent_key.nested_key_2: <type>,  my_parent_key.nested_key_3: <type> },
     }
     
    

    When a nested obj field filter is selected, the sibling fields are looked up in the nestedObjMap. This allows the filter to know which types are available on the nested object and which fields should be grouped together b/c they are part of the same nested object.

    From here, the logic is similar to a terms filter....i think.

@erhathaway
Copy link
Contributor Author

@dearsaturn @markrsocialnative you guys may be interested in this ^

@erhathaway
Copy link
Contributor Author

If there is agreement on this approach, the next step would be figuring out the pseudo code for how nested filters should be applied in the filter application to the request object step. We can just post ideas inline here.

@dearsaturn
Copy link

How would the experience differ from an API perspective of this package?

@erhathaway
Copy link
Contributor Author

erhathaway commented Apr 21, 2021

@dearsaturn good question. I was thinking that it wouldn't change. I'd like to perserve the public interface.

explorer.filter.nestedObj.setFilter('questions', { 
   question_filter_1: { inclusion: 'include', fields: [
       { name: 'question', value: 'how are you today?', kind: 'terms'  }, 
       { name: 'answer', value: 'good', kind: 'match' }
   ]}, 
   question_filter_2: { inclusion: 'exclude', , fields: [
       { name: 'id', value: { lt: 2, gt: 4 }, kind: 'range'  }  
   ]},
   <additional_filters>: <filter obj>, etc...
})

I think something like this would work. It follows the same pattern that exist for terms and match filters, but extends them with a fields key to handle info about the nested field and search type.

@erhathaway
Copy link
Contributor Author

In a similar vein, you could use the addFilter to set a single filter for the nested obj field:

explorer.filter.nestedObj.addFilter('questions', 'question_filter_2', 
  { inclusion: 'exclude', fields: [
       { name: 'id', value: { lt: 2, gt: 4 }, kind: 'range'  }  
  ]}
)

@dearsaturn
Copy link

In this case, is nestedObj a reference to a particular nested object field?

@erhathaway
Copy link
Contributor Author

In the above examples, nestedObj is a filter type that handles nested object filtering. Does that answer your question?

@dearsaturn
Copy link

Oh, I see. And questions is the nested object, name is the field in the object, and question_filter_2 is a name for the filter? I'm not super familiar with the elastic-composer API at the moment.

@erhathaway
Copy link
Contributor Author

@dearsaturn yeah exactly:

  • questions is the field name which corresponds to the name of the nested object (aka in the schema, the 'questions' field has a nested object.
  • question_filter_2 is the name for the filter. All fields have the ability to have multiple filters. So for geo or range filters you could search multiple locations or multiple ranges for a single field. You can reference a fields filter by the name. When you set a filter you can use either the bulk add setFilter method or the single add addFilter method.

The first example in the readme is a pretty good way to get acquainted with the API.

You basically have:

  • explorer.filters (set filters for a field)
  • explorer.suggestions (get suggestions to help fill out filters for a field)
  • explorer (a manager instance)
    • access what fields are available and their respective filter and suggestions instances.
    • access the results of the elasticsearch query
  • history (basic forward, backwards, etc.... history commands)

@dearsaturn
Copy link

Got it. LGTM!

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

No branches or pull requests

2 participants