Elasticsearch Suggestions Multi Index and Multi Fields

Viewed 380

I have different indexes that contain different fields. And I try to figure out how to get suggests from all indexes and all fields. I know that with GET /_all/_search I can search for results through all indexes. But how can I get all suggestions from all indexes and all fields? Because I want to have a feature like Google "Did you mean: suggests" So, I tried this out:

GET /_all/_search
{
   "query" : {
      "multi_match" : {
         "query" : "berlin"
      }
    },
    "suggest" : {
       "text" : "berlin",
       "my-suggest-1" : {
          "term" : {
             "field" : "street"
          }
       },
       "my-suggest-2" : {
          "term" : {
             "field" : "city"
          }
       },
       "my-suggest-3" : {
          "term" : {
             "field" : "description"
          }
       }
    }
}

"my-suggest-1" and "-2" belongs to Index address (see below) and "my-suggest-3" belongs to Index product. I get the following error:

"error" : {
   "root_cause" : [
      {
         "type" : "illegal_argument_exception",
         "reason" : "no mapping found for field [street]"
      },
      {
         "type" : "illegal_argument_exception",
         "reason" : "no mapping found for field [city]"
      },
      {
         "type" : "illegal_argument_exception",
         "reason" : "no mapping found for field [description]"
      }
   ]
}

But if I use only the fields of 1 index I get suggestions, see:

GET /_all/_search
{
   "query" : {
      "multi_match" : {
         "query" : "berlin"
      }
    },
    "suggest" : {
       "text" : "berlin",
       "my-suggest-1" : {
          "term" : {
             "field" : "street"
          }
       },
       "my-suggest-2" : {
          "term" : {
             "field" : "city"
          }
       }
    }
}

Response

...
"failures" : {
...
},
"hits" : {
...
}
"suggest" : {
   "my-suggest-1" : [
      {
         "text" : "berlin",
         "offset" : 0,
         "length" : 10,
         "options" : [
            {
               "text" : "berliner",
               "score" : 0.9,
               "freq" : 12
            },
            {
               "text" : "berlinger",
               "score" : 0.9,
               "freq" : 1
            }
         ]
      }
   ],
   "my-suggest-2" : [
      {
         "text" : "berlin",
         "offset" : 0,
         "length" : 10,
         "options" : []
      }
   ]
...

I don't know how I can get suggests from index address and product? I would be happy if someone can help me.

Index 1 - Address:

"address" : {
  "aliases" : {
     ....
   },
   "mappings" : {
      "dynamic" : "strict",
      "properties" : {
         "_entity_type" : {
            "type" : "keyword",
            "index" : false
         },
         "street" : {
            "type" : "text"
         },
         "city" : {
            "type" : "text"
         }
      }
   },
   "settings" : {
      ...
   }
}

Index 2 - Product:

"product" : {
  "aliases" : {
     ...
   },
   "mappings" : {
      "dynamic" : "strict",
      "properties" : {
         "_entity_type" : {
            "type" : "keyword",
            "index" : false
         },             
         "description" : {
            "type" : "text"
         }
      }
   },
   "settings" : {
      ...
   }
}
1 Answers

You can add multiple indices to your search. In this case, you need to search over the fields that exist on all indices. So In your case, you need to define all three fields in both of the indices. The fields "street" and "city" are filed in the first index and the field "description" is filled only in the second index. This will be your mapping for the "Address" index. In this index, the "description" field exists but has no data. In the second index, "street" and "city" exist but have no data.

"address" : {
  "aliases" : {
     ....
   },
   "mappings" : {
      "dynamic" : "strict",
      "properties" : {
         "_entity_type" : {
            "type" : "keyword",
            "index" : false
         },
         "street" : {
            "type" : "text"
         },
         "city" : {
            "type" : "text"
         },
         "description" : {
            "type" : "text"
         }
      }
   },
   "settings" : {
      ...
   }
}
Related