Best way to filter elasticsearch results?

We are considering using Elastic search in our web application as a replacement for SOLR. I was hoping to get some guidance/advice on the best approach for one aspect of how we use search in our product.

A user's access to search results can be narrowed by 'object filters', which can give access to, say, 'All objects that start with 'A*’. In Elasticsearch, a 'filter' clause can be used to achieve these results (example at bottom.)

What we need help with is deciding if that approach will be the best way to implement this with Elasticsearch. There could potentially be dozens of filters that will apply - each with multiple fields to evaluate - would we run into size limits on the query string with this approach? Any other potential issues doing it this way?

The other option is to not use the filter clause, and use a plugin of our own to filter the results before showing the viewer.

thanks for any help,
Tom

… TMI/E.G:

This is example of the type of query we would use. The user enters a β€˜*’ string to return all objects, and filter query would return only those that start with T or C:

GET objects/_search
{
   "query": { ## User’s actual query entered in search form, 
                     ## Objects whose name matches β€˜*’
      "bool": {
         "must": [
            {"query": {"wildcard": {"name": "*"}}}
         ],
         "filter": { ## Filter query constructed from object filters
            "query": {
               "bool": {
                  "should": [
                     {
                        "bool": {
                           "must": [
                              {"wildcard": {"name": "T*"}}
                           ]
                        }
                     },
                     {
                        "bool": {
                           "must": [
                              {"wildcard": {"name": "C*"}}
                           ]
                        }

....

Hi Tom,

I would filter the results as close to the source as possible and stick with queries instead of a plugin. W.r.t. to your question with size limits: We currently have an open ticket where we consider to restrict the search request size to a (configurable) maximum but it is highly unlikely that you hit this limit even with dozens of filters.

If you need to build the filter clauses in a flexible manner I'd use query builders for that (see also the docs for the Java client).

In your case I'd also rather use a prefix query instead of a wildcard query. If you need to do auto-completion have a look at suggesters.

I hope this gets you started.

Daniel

1 Like

Thanks for the response Daniel, much appreciated.

Glad I could help. :slight_smile: