Home > Mobile >  How to perform nested queries on Elasticsearch?
How to perform nested queries on Elasticsearch?

Time:07-27

I was trying to perform nested query on elastic-search that is, I have 2 queries in which the output of the first query must be used as an input in the second query, was going through the documentation of elastic-search but couldn't find any alternative.

The first query is:

    GET index1/_search
    {
      "query": {
        "query_string": {
          "query": "(imageName: xyz.jpg)"
        }
      }
    }

The output of this query would be of JSON format,

For example:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.2682955,
    "hits" : [
      {
        "_index" : "index1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.2682955,
        "_source" : {
          "assetId" : "0",
          "descriptor" : "randomString",
          "bucketId" : [randomArray],
          "imageName" : "xyz.jpg"
        }
      }
    ]
  }
}

The second query is:

GET index2/_search
{
  "query": {
    "function_score": {
      "boost_mode": "replace", 
      "query": {
        "constant_score": {
          "filter": {
            "terms": {
              "bucketId": [randomArray that came as an output of the first query]
            }
          }
        }
      },
      "pqcode_score": {
        
             "descriptors": [
              {
            "descriptor": "randomString that came as an output of the first query"
              }
        ]
      }
    }
  }
}

How can we use the output of the first query inside the second query?

Can anyone help me in this regard?

CodePudding user response:

It is not possible in Elasticsearch. You need to implement this at your application side.

You can call first query and get result then you can call the second query by passing the output of first query that is the only option.

  • Related