
Meilisearch v1.1 推出了 /multi-search
端点。通过此端点,用户现在可以将多个搜索查询捆绑成一个 HTTP 请求,对一个或多个索引进行搜索。本质上,/multi-search
端点为联合搜索铺平了道路。
联合搜索 在 Meilisearch 1.10 及更高版本中可用。
使用 Meilisearch 进行多索引搜索
联合搜索以 277 票成为最受请求的功能之一。我们很高兴终于宣布该功能的第一阶段。
我们引入了 /multi-search
路由,允许通过单个 HTTP 请求在多个索引中进行搜索。假设我们有两个索引:movies
和 actors
。使用 /multi-search
端点,我们可以搜索女演员的名字,并在搜索结果中同时检索她的传记和电影。
例如,以下是多搜索请求:
curl -X POST 'https://: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
参数将返回的文档数量限制为仅一个。
多索引搜索演示
此演示使用两个数据集:电影和演员。每个数据集都存储在自己的索引中,拥有自己的设置,并且如上例所示,拥有自己的架构。
由于这两个索引具有不同的架构,因此它们具有不同的可搜索属性。在电影索引中,您可以按电影标题、角色、演员、制片人、导演或电影概述搜索电影。
//movies index searchableAttributes: [ 'title', 'crew.name', 'cast.name', 'cast.character', 'overview', ]
演员索引允许您按姓名、其最著名的电影或节目以及传记中找到的关键词搜索演员。
//actors index searchableAttributes: ['name', 'known_for', 'biography']
完整的设置列表、数据集和代码均可在 GitHub 上找到,欢迎您自行探索。请随意尝试这些设置以根据您的需求自定义行为。如果您有改进演示的想法,例如添加分面,请随时提交拉取请求。欢迎所有贡献!
结论
我们很高兴能推出联合搜索的第一个版本,我们将努力推动实现聚合搜索结果。如果没有我们开发者社区提供的宝贵反馈和见解,我们无法做到这一点。您的反馈对于帮助我们确定功能优先级并使其符合您的需求至关重要。
如果您有兴趣了解我们的进展,我们鼓励您查看我们的产品路线图,加入我们在 GitHub 上的讨论,或者在 Discord 上与我们联系。
一如既往,我们致力于为用户提供最佳的搜索体验,并感谢您为实现这一目标所做的帮助。