想要更好地控制您的搜索设置?了解我们的灵活的基础设施定价

回到主页Meilisearch 的标志
返回文章

如何使用第二个索引实现文档提升

了解如何使用第二个索引(用于固定文档)通过Meilisearch实现推广搜索结果。

2023年8月9日3分钟阅读
Laurent Cazanove
Laurent Cazanove开发者体验工程师@StriftCodes
How to implement document boosting with a second index

本指南将引导您使用 Meilisearch 实现推广搜索结果。我们的目标是在用户查询匹配某些关键词时,搜索结果能优先显示特定文档。这些被提升的文档应显示在搜索结果的顶部。

本指南解释了如何在后端实现推广文档。对于前端优先的实现,请参阅使用 React InstantSearch 实现推广搜索结果

概述

以下是使用第二个“置顶文档”索引和多重搜索功能实现文档提升的简化流程。

  1. 创建索引: 设置两个索引:一个用于常规搜索,一个用于提升结果。提升索引将有一个特殊属性keywords来触发提升。
  2. 填充“games”索引: 使用提供的 JSON 文件将数据集填充到 games 索引中。此索引将作为我们提升文档的来源。
  3. 配置“pinned_games”索引: 配置pinned_games索引以显示属性而不暴露关键词。相应地调整可搜索属性和显示属性。
  4. 提升文档: 识别您想要提升的文档并为其分配相关的关键词。例如,您可以将关键词fpsshooter分配给游戏Counter-Strike
  5. 实现多重搜索: 利用 Meilisearch 的多重搜索功能,在常规索引和提升索引上执行搜索查询。这样,匹配关键词的提升文档将首先出现。
  6. 显示结果: 以用户友好的格式呈现搜索结果,并用视觉指示器突出显示推广文档。

实施

安装

在深入了解之前,请确保您已成功运行 Meilisearch。如果您尚未安装,请按照以下步骤操作

  1. 启动 Meilisearch 实例——您可以在本地运行 Meilisearch,也可以通过Meilisearch 云运行。
  2. 确保您已安装了自己喜欢的语言 SDK(或框架集成)。

本指南使用 Python SDK,但它与其他 Meilisearch 集成的工作方式相同。🎉

初始化索引

在我们的示例中,我们将使用 Steam 游戏数据集。您可以将此过程适应您自己的数据。

  1. 下载steam-games.jsonsettings.json文件,用于我们的Steam 游戏数据集
  2. 通过添加文档steam-games.json文件将数据集加载到您的 Meilisearch 实例中。

games 索引

import meilisearch
import json
from typing import Callable

client = meilisearch.Client(url="https://:7700")
games = client.index("games")

# helper to wait for Meilisearch tasks
def wait_with_progress(client: meilisearch.Client, task_uid: int):
    while True:
        try:
            client.wait_for_task(task_uid, timeout_in_ms=1000)
            break
        except meilisearch.errors.MeilisearchTimeoutError:
            print(".", end="")
    task = client.get_task(task_uid)
    print(f" {task.status}")
    if task.error is not None:
        print(f"{task.error}")
            
print("Adding settings...", end="")
with open("settings.json") as settings_file:
    settings = json.load(settings_file)
    task = games.update_settings(settings)
    wait_with_progress(client, task.task_uid)


with open("steam-games.json") as documents_file:
    documents = json.load(documents_file)
    task = games.add_documents_json(documents)
    print("Adding documents...", end="")
    wait_with_progress(client, task.task_uid)

pinned_games 索引

此索引将包含推广文档。pinned_games索引的设置与games索引相同,但有以下不同之处

  • 唯一的searchableAttributeskeywords属性,其中包含触发固定该文档的词语。
  • displayedAttributes是文档的所有属性,除了keywords(我们不想向最终用户显示关键词)
pinned = client.index("pinned_games")

print("Adding settings...", end="")
with open("settings.json") as settings_file:
    settings = json.load(settings_file)
    settings["searchableAttributes"] = ["keywords"]
    # all but "keywords"
    settings["displayedAttributes"] = ["name", "description", "id", "price", "image", "releaseDate", "recommendationCount", "platforms", "players", "genres", "misc"]
    task = pinned.update_settings(settings)
    # see `wait_with_progress` implementation in previous code sample
    wait_with_progress(client, task.task_uid) 

更新推广文档索引

现在,我们将从games索引中挑选要推广的文档来填充索引。

举例来说,假设我们想将游戏"Counter-Strike"固定到关键词"fps""first", "person", "shooter"

counter_strike = games.get_document(document_id=10)
counter_strike.keywords = ["fps", "first", "person", "shooter"]

print("Adding pinned document...", end="")
task = pinned.add_documents(dict(counter_strike))
wait_with_progress(client, task.task_uid)

自定义搜索结果

现在,让我们创建一个函数,返回包含置顶文档的搜索结果。

from copy import deepcopy
from typing import Any, Dict, List
from dataclasses import dataclass

@dataclass
class SearchResults:
    pinned: List[Dict[str, Any]]
    regular: List[Dict[str, Any]]

def search_with_pinned(client: meilisearch.Client, query: Dict[str, Any]) -> SearchResults:
    pinned_query = deepcopy(query)
    pinned_query["indexUid"] = "pinned_games"
    regular_query = deepcopy(query)
    regular_query["indexUid"] = "games"
    results = client.multi_search([pinned_query, regular_query])
    # fetch the limit that was passed to each query so that we can respect that value when getting the results from each source
    limit = results["results"][0]["limit"]
    # fetch as many results from the pinned source as possible
    pinned_results = results["results"][0]["hits"]
    # only fetch results from the regular source up to limit
    regular_results = results["results"][1]["hits"][:(limit-len(pinned_results))]
    return SearchResults(pinned=pinned_results, regular=regular_results)

我们可以使用此函数检索带有推广文档的搜索结果

results = search_with_pinned(client, {"q": "first person shoot", "attributesToRetrieve": ["name"]})

results 对象应如下所示

SearchResults(pinned=[{'name': 'Counter-Strike'}], regular=[{'name': 'Rogue Shooter: The FPS Roguelike'}, {'name': 'Rocket Shooter'}, {'name': 'Masked Shooters 2'}, {'name': 'Alpha Decay'}, {'name': 'Red Trigger'}, {'name': 'RAGE'}, {'name': 'BRINK'}, {'name': 'Voice of Pripyat'}, {'name': 'HAWKEN'}, {'name': 'Ziggurat'}, {'name': 'Dirty Bomb'}, {'name': 'Gunscape'}, {'name': 'Descent: Underground'}, {'name': 'Putrefaction'}, {'name': 'Killing Room'}, {'name': 'Hard Reset Redux'}, {'name': 'Bunny Hop League'}, {'name': 'Kimulator : Fight for your destiny'}, {'name': 'Intrude'}])

瞧!您的搜索结果对象现在包含两个数组:推广结果和“常规”结果。

遇到困难了吗?请随时在我们的Discord 社区寻求帮助。

进一步学习

本教程探讨了一种实现推广结果的方法。另一种替代技术是在前端实现文档固定;请参阅我们的React 实现指南。这种不同的方法具有与 InstantSearch 兼容的优点。

两种技术都能达到类似的效果。我们还计划将推广文档集成到 Meilisearch 引擎中。请在上面的链接中留下您的反馈,以帮助我们确定其优先级。

有关更多 Meilisearch 的内容,您可以订阅我们的新闻通讯。您可以通过查看路线图并参与我们的产品讨论来了解更多关于我们产品的信息。

如有其他任何疑问,请加入我们的开发者社区 Discord

干杯!

Meilisearch indexes embeddings 7x faster with binary quantization

Meilisearch 使用二值量化将嵌入索引速度提高7倍

通过在向量存储 Arroy 中实现二值量化,显著减少了大型嵌入的磁盘空间使用和索引时间,同时保持了搜索相关性和效率。

Tamo
Tamo2024年11月29日
How to add AI-powered search to a React app

如何向 React 应用添加 AI 驱动的搜索

使用 Meilisearch 的 AI 驱动搜索构建 React 电影搜索和推荐应用。

Carolina Ferreira
卡罗莱纳·费雷拉2024年9月24日
Meilisearch is too slow

Meilisearch 太慢了

在这篇博文中,我们探讨了 Meilisearch 文档索引器所需的增强功能。我们将讨论当前的索引引擎、其缺点以及优化性能的新技术。

Clément Renault
克莱门特·雷诺2024年8月20日
© . This site is unofficial and not affiliated with Meilisearch.