Alireza Bashiri
Alireza Bashiri

Reputation: 452

Multi-Index search (autocomplete)

I have four indices called, cities, regions, countries and hotels they have a in common filed called name I want to search through theses indices and get a result to use in autocompletion. Also I couldn't create multi-type with single index using JDBC input in Logstash and reindex API just to mention due to one document per index change in Elasticsearch 6.x Here's an example of single index search;

GET /hotels/hotels/_search
{
  "query": {
    "match": {
      "name": {
        "query": "term",
        "operator": "and"
      }
    }
  }
}

I want to do the same with multi-index situation. The below doesn't work:

GET hotels,cities,countries,regions/_search
{
  "query": {
    "match": {
      "name": {
        "query": "term",
        "operator": "and"
      }
    }
  }
}

Upvotes: 3

Views: 3391

Answers (1)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

You can use the Multi Search API. This way you can provide multiple queries on different indices. For example:

GET _msearch
{"index" : "hotels"}
{ "query": { "match": { "name": { "query": "term", "operator": "and" } } } }
{"index" : "cities"}
{ "query": { "match": { "name": { "query": "term", "operator": "and" } } } }
{"index" : "countries"}
{ "query": { "match": { "name": { "query": "term", "operator": "and" } } } }
{"index" : "regions"}
{ "query": { "match": { "name": { "query": "term", "operator": "and" } } } }

Upvotes: 4

Related Questions