23

Can you nest queries logically in ElasticSearch, so the output of one query is the input to another query. Another, way to ask is how can I chain or pipe queries together?

This should be analogous to the IN operator or subqueries in SQL

i.e.:- select au_lname, au_fname, title from (select au_lname, au_fname, au_id from pubs.dbo.authors where state = 'CA') or

SELECT Name FROM AdventureWorks2008R2.Production.Product WHERE ListPrice = (SELECT ListPrice FROM AdventureWorks2008R2.Production.Product WHERE Name = 'Chainring Bolts' );

user48545
  • 3,291
  • 9
  • 31
  • 42
  • Maybe (to be tested) subqueries are now supported, considering the [Elasticsearch 6.3 release (June 2018) comes with native SQL support](https://stackoverflow.com/a/50889538/6309). – VonC Jun 16 '18 at 16:12

3 Answers3

27

Elasticsearch doesn't support subqueries; you would need to perform your first query, then construct a second query using the results of the first query as an input.

Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • 1
    weeps, you're right. http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/application-joins.html – user48545 Feb 26 '15 at 04:37
0

this is not supported in elastic-search you must normalize your data and have all field you need in one setting

reihaneh
  • 225
  • 4
  • 18
-3

That is totally correct, you must programm a subquery in your favorite programming language. An example can be found here:

https://sebastianviereck.de/elasticsearch-subquery-scoring-optimization/

Sebastian Viereck
  • 5,455
  • 53
  • 53