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.

Can't generate an OR query with a nested must_not filter

See original GitHub issue

I’m trying to generate the following query: select all records, where a nested prop1.id is 1 OR nested prop1 doesn't exist.

Here is a working elasticsearch object example which does what I need:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "nested": {
                "path": "prop1",
                "query": {
                  "term": {
                    "prop1.id": "1"
                  }
                }
              }
            },
            {
              "bool": {
                "must_not": [ {
                  "nested": {
                    "path": "prop1",
                    "query": {
                      "exists": {
                        "field": "prop1"
                      }
                    }
                  }
                } ]
              }
            }
          ]
        }
      }
    }
  }
}

The question is - how do I generate such a query with bodybuilder? I can generate the first part with

bodybuilder()
	.orFilter('nested', 'path', 'prop1', q => {
		return q.query('terms', 'prop1.id', '1');
	})
	.build();

But how do I add the nested must_not part to it? Any advices?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
johannes-scharlachcommented, Aug 15, 2019
bodybuilder()
    .orFilter('nested', 'path', 'prop1', q => {
        return q.filter('terms', 'prop1.id', '1');
    })
    .orFilter('bool', q => {
        return q.notFilter('nested', 'path', 'prop1', q2 => {
            return q2.filter('exists', 'prop1')
        })
    })
    .build();

this got me quite close. Does that work for your case?

1reaction
ecdevelopercommented, May 5, 2017

Ok, looks like a could achieve that in a little bit hacky way:

let t = bodybuilder()
	.orFilter('nested', 'path', 'prop1', q => {
		return q.query('terms', 'prop1.id', '1');
	});

let notFilter = bodybuilder().notFilter('nested', 'path', 'prop1', q => q.query('exists', 'prop1'));

t.getFilter().bool.should.push(notFilter.getFilter());
t.build();

But I’m still wondering - is there a way to get the same query object without accessing query object internals?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ElasticSearch should with nested and bool must_not exists
I want to use the categories field to return documents that either: have a score above a threshold in a given category, or;...
Read more >
MUST_NOT not working with EXIST in NESTED query
So I tried a nested query, but it returns no result. POST players/players/_search { "query": { "bool": { "filter": [ { "nested": {...
Read more >
How to Query Elasticsearch With Boolean Queries - Dattell
Boolean queries in Elasticsearch match documents by using the clauses: filter, must, must_not, & should. Read on for examples of bool queries.
Read more >
Invalid sub-query filter. Either data cannot be retrieved (null) or ...
SAP Documentation. Search · SAP Help Portal | SAP Community Network | SAP Support Portal. Expand All Collapse All. Previous topic ...
Read more >
SODA Filter Specifications (Reference) - Oracle Help Center
SODA query operations use a QBE to select all JSON documents in a collection ... That is, you cannot nest a composite filter...
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