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.

Cannot build valid 'nested' queries in filter context

See original GitHub issue

From what I can tell, it’s not possible to perform a nested query inside of a filter context with BodyBuilder:

bodybuilder().orFilter('nested', 'path', 'my_path', b => 
  b.query('term', 'my_path.my_field', 'my_value')
)

This doesn’t work because b.query does not exist, and instead only various filter functions are available. I believe this has to do with being in the “filter context” because of the outer orFilter().

If I try to use filter, however:

bodybuilder().orFilter('nested', 'path', 'my_path', b => 
  b.filter('term', 'my_path.my_field', 'my_value')
)

This results in a 400 Bad Request from Elasticsearch because filters inside of a nested field is not supported. I am running against Elasticsearch v6.2.

If I use an orQuery at the top-level this works fine:

bodybuilder().orQuery('nested', 'path', 'my_path', b => 
  b.query('term', 'my_path.my_field', 'my_value')
)

However this doesn’t work for me because I want my boolean condition to live in the filter context with my other conditions and it’s cheaper to perform.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
conradoramalhocommented, Jun 20, 2018

I solved the problem using:

bodybuilder()
        .filter('term', 'agency_id', '115') 
        .filter('nested', {
          path: 'brokers',
          ...bodybuilder()
            .query('term', 'brokers.broker_customer_service_type_id', 2)
            .query('term', 'brokers.broker_id', 167415)
            .build()
        }) 
  	.filter('range', 'profiles.beds', {gte: 2})   
  	.orFilter('term', 'profiles.rent', true)   		 
  	.build()
2reactions
quinnlangillecommented, Jun 26, 2018

@johannes-scharlach’s solve worked for me. If there’s no plans on adding nested filter functionality in the near future, this should probably be documented as a workaround :~)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Combine nested query with filter - elasticsearch - Stack Overflow
The following query should work. You cannot nest the range query inside the nested one, you need to keep it outside but at...
Read more >
Invalid sub-query filter. Either data cannot be retrieved (null) or ...
Either data cannot be retrieved (null) or no result objects are defined. (IES 00023) The query contains an invalid sub-query.
Read more >
Use Context Filters - Tableau Help
To create a context filter, select Add to Context from the context menu of an existing categorical filter. The context is computed once...
Read more >
Perform simple and compound queries in Cloud Firestore
Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group.
Read more >
ElasticSearch Nested Queries: How to Search for Embedded ...
Doing nested queries with ElasticSearch can be complicated. Get all the answers on how to search for embedded documents here.
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