DennisKip
DennisKip

Reputation: 29

Aggregate results from several indices in Elasticsearch

I have an Elasticsearch query as shown below.

  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "Netherlands"
          }
        }
      ]
    }
  }, 
  "sort": [
    {
      "file.created": {
        "order": "asc"
      }
    }
  ]
}

When I query several indices and sort my results as shown below(in ascending or descending order), my results are in order but of each individual index. So I get trial2 results in order then trial3 results in order.

    http://localhost:9200/trial2,trial3/_doc/_search?pretty

What I am looking for, since I am querying several indices and sorting by date, is to get the results of all the indices in ascending or descending order. If a document in trial3 is more recent then the one in trial2, it should appear higher regardless of the order of the indices in the query.

Kindly advice

Upvotes: 0

Views: 90

Answers (1)

oliver_t
oliver_t

Reputation: 1095

If you are working with multiple indices that have an equal structure, it would make sense to create an alias that contains all these indices together. You can then run your queries against this virtual big index. Also the sorted results are then in a correct order, while the original index is still referenced in each result document.

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "trial2",
        "alias": "my-alias"
      }
    },
    {
      "add": {
        "index": "trial3",
        "alias": "my-alias"
      }
    }
  ]
}

Upvotes: 1

Related Questions