1

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" : {
      ...
   }
}
Veronika89
  • 41
  • 3

1 Answers1

0

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" : {
      ...
   }
}
Saeed Nasehi
  • 940
  • 1
  • 11
  • 27
  • It works this way. Thank you. But I'm looking for another solution. All my indexes should get a **field** called **"suggest"**. And this field should contain all other fields of this index (e.g. Address{street, city, suggest: {street, city}}), and no fields from other indexes. How is this possible? – Veronika89 Jul 22 '21 at 13:05
  • When you want to use a general query to work on multiple indices, you need to have same fields on all indices. The solution that comes to my mind is that you define suggest field in all of your indices. In all of the indices make fields to have same name. As an example for your Address index you add suggest:{field1,field2}. in this case you can make query over field1 and field2 which exists on all indices. – Saeed Nasehi Jul 24 '21 at 05:33