Meilisearch v1.1 引入了 /multi-search 端点。使用此端点,用户现在可以通过将多个搜索查询捆绑到单个 HTTP 请求中,在多个索引上发送多个搜索查询。本质上,/multi-search 端点为 联合搜索 铺平了道路。

💡
联合搜索 可在 Meilisearch 1.10 及更高版本中使用。

使用 Meilisearch 进行多索引搜索

在 277 票中,联合搜索是 最受欢迎的功能之一。我们很高兴终于宣布该功能的第一阶段。

我们引入了一个 /multi-search 路由,它允许使用单个 HTTP 请求在多个索引中进行搜索。假设我们有两个索引:moviesactors。使用 /multi-search 端点,我们可以搜索女演员的名字,并在搜索结果中检索她的传记和电影。

例如,以下多搜索请求

curl \
-X POST 'http://localhost:7700/multi-search' \
-H 'Content-Type: application/json' \
--data-binary '{
  "queries": [
     {
       "indexUid": "movies",
       "q": "Kate Winslet",
       "limit": 1
     },
    {
       "indexUid": "actors",
       "q": "Kate Winslet",
       "limit": 1
    }
  ]
}'

响应将包含每个已搜索索引的一组搜索结果

{
   "results": [
     {
       "indexUid": "movies",
       "hits": [
         {
           "title": "The Reader",
           "overview": "The story of Michael Berg, a German lawyer who, as a teenager in the late 1950s, had an affair with an older woman, Hanna, who then disappeared only to resurface years later as one of the defendants in a war crimes trial stemming from her actions as a concentration camp guard late in the war. He alone realizes that Hanna is illiterate and may be concealing that fact at the expense of her freedom.",
           "crew": [...],
           "cast": [
		...
             {
               "character": "Hanna Schmitz",
               "name": "Kate Winslet",
               "profile_path": "/e3tdop3WhseRnn8KwMVLAV25Ybv.jpg"
             },
		...
           ]
         }
       ],
// other search results fields: processingTimeMs, limit, ...
     },
     {
       "indexUid": "actors",
       "hits": [
     {
       "name": "Kate Winslet",
       "known_for": [
           "Titanic",
           "Eternal Sunshine of the Spotless Mind",
           "The Reader"
       ],
       "birthday": "1975-10-05",
       "deathday": null,
       "biography": "Kate Elizabeth Winslet (born 5 October 1975) is an English actress. Known for her work in independent films..."
     }
       ],
	// other search results fields: processingTimeMs, limit, ...
     }
   ]
 }

如您所见,在结果数组中,索引按查询中的相同顺序排序。响应包含 通常的字段,传统的搜索返回以及索引 UID。请注意,在此示例中,我们使用 limit 参数将返回的文档数量限制为一个。

多索引搜索演示

Searching for 'Kate Winslet' returns both her biography and movies in which she starred, while searching for 'Titanic' returns the movie itself as well as actors who starred in it.

试试吧

此演示使用两个数据集:电影和演员。每个数据集都存储在自己的索引中,具有自己的设置,并且如您在上面的示例中看到的,具有自己的模式。

由于这两个索引具有不同的模式,因此它们具有不同的 可搜索属性。在电影索引中,您可以通过电影的标题、角色、演员、制片人、导演或电影概述来搜索电影。

//movies index 
searchableAttributes: [
   'title',
   'crew.name',
   'cast.name',
   'cast.character',
   'overview',
 ]

演员索引允许您通过姓名搜索演员,以及他们最著名的电影或节目,以及在他们的传记中找到的关键字。

//actors index 
searchableAttributes: ['name', 'known_for', 'biography']

完整的设置、数据集和代码都在 GitHub 上提供,因此请随时自行探索。如果您有改进演示的想法,例如添加筛选器,请随时提交拉取请求。所有贡献都受欢迎!

结论

我们很高兴交付联合搜索的第一个迭代,我们将努力推动聚合搜索结果。如果没有开发人员社区提供的宝贵反馈和见解,我们不可能实现这一点。您的反馈对我们帮助我们优先考虑和塑造功能以满足您的需求至关重要。

如果您有兴趣了解我们的进展,我们鼓励您查看我们的 产品路线图,加入我们在 GitHub 上的 讨论,或在 Discord 上与我们联系。

与往常一样,我们致力于为用户提供最佳的搜索体验,并感谢您在实现这一目标方面提供的帮助。