学习排序
学习排序是一种机器学习技术,允许您训练一个模型来对搜索结果进行排序。该模型使用一组特征和一组相关性判断进行训练。然后,该模型用于对搜索结果进行排序。
在本指南中,您将学习如何将学习排序与 Metarank 和 Searchkit 集成。您可以在 Metarank 文档 (在新标签页中打开) 中了解有关 Metarank 的学习排序的更多信息。
在本指南中,我们假设您已
- 安装 Metarank 和 Searchkit
- 使用 Metarank 训练了一个模型
- 为您的模型设置了特征和相关性判断
Searchkit UI 的更改
Metarank 要求在每次搜索请求中发送会话 ID 和用户 ID。一种方法是在 @searchkit/instantsearch-client
中,并通过标头提供会话 ID 和用户 ID
const searchClient = Client({
url: "/api/search",
headers: {
"x-session-id": "my-session-id",
"x-user-id": "my-user-id",
},
});
Searchkit API 的更改
为了将学习排序与 Searchkit 集成,我们将使用 beforeSearch
和 afterSearch
的钩子。这些钩子允许我们在查询和结果发送到 Elasticsearch 并返回到 UI 之前修改它们。
我们将使用 afterSearch
钩子将结果发送到 Metarank API 并使用新顺序更新结果。
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const headers = req.headers
const results = await client.handleRequest(req.body, {
hooks: {
afterSearch: async (searchRequests, searchResponses) => {
// Get the first search request
const [uiRequest, otherRequests] = searchRequests;
// Get the first search response
const [uiResponse, otherResponses] = searchResponses;
// Get the session id and user id from the headers
const sessionId = headers["x-session-id"];
const userId = headers["x-user-id"];
// Get the query from the first search request
const query = uiRequest.body.query;
// Get the results from the first search response
const results = uiResponse.body.hits.hits;
// Send the request to the Metarank API
const response = await fetch("https://127.0.0.1:8080/rank/xgboost", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session: sessionId,
user: userId,
fields: [
{ name: "query", value: query }
],
timestamp: (new Date()).getTime(),
items: results.map((item) => {
return {
id: item._id,
// an optional array of extra fields that you can use in your model
fields: [
{ name: "title", value: item._source.title },
{ name: "description", value: item._source.description },
{ name: "url", value: item._source.url },
],
};
}})
}),
});
// Get the response from the Metarank API
const metarankResponse = await response.json();
// Get the items order from the response
const items = metarankResponse.items;
// Update the order of the results for the first 20 results
const updatedResults = items.map((item) => {
const result = results.find((result) => result._id === item.id);
return result
});
// Update the results in the first search response
const updatedUiResponse = {
...uiResponse,
body: {
...uiResponse.body,
hits: {
...uiResponse.body.hits,
hits: updatedResults,
},
},
};
return [
updatedUiResponse,
...otherResponses
];
}
}
});
}