Search code examples
elasticsearchautocompleteelasticsearch-5elasticsearch-aggregation

Elasticsearch completion suggester across multiple indices with different fields


I have 2 huge indices in the Elastic-search ,I am implementing search auto completion using completion suggester,I need to have suggeters from 2 different indices with different fields but it is not that efficient

POST index1,index2/_search
 {
 "_source": "name",     
 "suggest": {
 "my-suggest": {
  "prefix": "milk",
  "completion": {
    "field": "name",     ----------> it should be name in index 1
    "size": 5 ,
    "skip_duplicates": true
      
  }
},
"my-suggest2": {
  "prefix": "milk",
  "completion": {
    "field": "product_name", ----------> this field is only existed in index2
    "size": 5 ,
    "skip_duplicates": true 
    
    }
    },
        "my-suggest3": {
  "prefix": "milk",
  "completion": {
    "field": "name", ----------> it should be name in index 2
    "size": 5 ,
    "skip_duplicates": true 
  }
 }.  

The question is that how can I get 5 suggestion of these different fields in a single query,currently I only get suggestions from one index and since the "Product_name" is not available in the index1.it gives some error

illegal_argument_exception",
      "reason": "no mapping found for field [brand_name]"

can somebody help me to be able to generate suggester from multiple indices?


Solution

  • I would use multi search for this so you'll get suggestions from index 1 separate from suggestions from index2:

    POST /_msearch
    {"index": "index1"}
    {"_source":"name","suggest":{"my-suggest":{"prefix":"milk","completion":{"field":"name","size":5,"skip_duplicates":true}}}}
    {"index": "index2"}
    {"_source":"name","suggest":{"my-suggest2":{"prefix":"milk","completion":{"field":"product_name","size":5,"skip_duplicates":true}},"my-suggest3":{"prefix":"milk","completion":{"field":"name","size":5,"skip_duplicates":true}}}}