user3812411
user3812411

Reputation: 352

opensearch/elasticsearch query second index based on results from first

is there a way to get results from a second index based on the results from the first?

For example, patients index and appointments index:

the first one would get me my patients, and the second one would count how many appointments a patient has.

How can I merge them to get from the first one the patients + their number of appointments in the same query?

Basically, a graphql kind of thing where the number of appointments would be resolved into the first query in parallel or something of the sort.

Thank you in advance!

Upvotes: 0

Views: 587

Answers (2)

Musab Dogan
Musab Dogan

Reputation: 3680

Also, if you want to see all patients and with all organizations, you can use the following aggregation.

POST patients/_search
{
  "aggs": {
    "organizations": {
      "terms": {
        "field": "organization.S",
        "size": 1000
      },
      "aggs": {
        "patients": {
          "terms": {
            "field": "patient.S",
            "size": 1000
          }
        }
      }
    }
  }
}

Upvotes: 0

Musab Dogan
Musab Dogan

Reputation: 3680

You can use the term aggregation.

POST patients/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "organization.S": "whatever"
          }
        }
      ]
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "patient.S",
        "size": 1000
      }
    }
  }
}

Note: the field type should be keyword for the aggs. If you get any error, try with your_field_name.keyword.

Upvotes: 0

Related Questions