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.

should nested within must

See original GitHub issue

I am curious if it is possible with this module to set up a complex elastic query that has should queries nested within a must.

Simply, I am trying to recreate this SQL query: SELECT * FROM test where status = 'active' and (city = 'New York' or city = 'Toronto')

I would like to get back something similar to the below, but I have only been able to have the MUST and the SHOULD on the same level. Is this functionality possible? It seems like a simple query case, but maybe I am wrong.

{
    "query": {
        "bool": {
            "must": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "status": {
                                    "query": "active"
                                }
                            }
                        },
                        {
                            "bool": {
                                "should": [
                                    {
                                        "match": {
                                            "city": {
                                                "query": "New York"
                                            }
                                        }
                                    },
                                    {
                                        "match": {
                                            "city": {
                                                "query": "Toronto"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
caioflorescommented, May 2, 2017

@gitemconte I made a approach similar to @diegoprd one, I needed something like this in my filters:

  "filter": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "term": {"Field": "Value"}
              },
              {
                "term": {"Field": "Value2"}
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {"Field2": "Value3"}
              },
              {
                "term": {"Field2": "Value4"}
              }
            ]
          }
        }
      ]
    }
  }

To get this result dinamically with bodybuilder I did:

   const query = bodybuilder().query('match_all');

   // Did this to create the must
   query.filter('term', 'Field', 'temporary');
   query.filter('term', 'Field', 'temporary');
   query.getFilter().bool.must.length = 0;
   
  // For each one of my 'shoulds' I created a filter of bodybuilder type
  // and applied the 'orFilter', than I inserted it in the main query
  Object.keys(filters).forEach((key) => {
      const filter = bodybuilder();
       for (let i = 0; i < filters[key].length; i += 1) {
         const value = filters[key][i];
         filter.orFilter('term', key, value);
       }
       query.getFilter().bool.must.push(filter.getFilter());
   });

Obviously this is not a good solution but it works, and I hope it helps someone, if you have any question just ask.

I started using bodybuilder and my project needs many nested queries, @danpaz we can create a PR for this? How can we help?

2reactions
ferronrsmithcommented, Jun 19, 2020

@KillerAyce no worries, well I overlooked too. Glad we figured out, all the best !

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Nested Should query(OR) inside a Must ...
I need to query elastic search to get all the profiles with is_cricketer = 1 and an OR query over the field for...
Read more >
Nested query with should clauses and filter - Elasticsearch
I am using nested query with should clauses in elasticsearch 2.3.2 GET shop/customer/_search { "from": 0, "size": 25, "query": { "bool": ...
Read more >
The existing input element should be nested within a form ...
An element is nested within another element, if it is between the other element's opening and closing tags. i dont get how to...
Read more >
what are the two tags that should always be nested within ...
The <body> element contains the entire content of a webpage. It must be the second element inside of the parent <html> element, following...
Read more >
The Difference Between Crossed and Nested Factors
When there is only one factor in a design, you don't have to worry about crossing and nesting. But once you have at...
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