Search code examples
elasticsearchelasticsearch-6

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"
      }
    }
  }
}

Solution

  • 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" } } } }