分面搜索:利用人工智能改进搜索范围和结果
探索如何在 Elasticsearch 中使用面搜索来快速缩小类别内的选项范围。
在本文中,我们将探讨人工智能(AI),特别是使用 GPT-4 等高级语言模型,如何帮助创建更多的上下文切面,使它们对用户更加相关和有用。
切面搜索是电子商务平台的一个强大工具。它有助于根据显示项目的特征来组织和完善搜索结果。虽然滤镜经常被混淆,但切面的工作原理是不同的。筛选器是固定属性,由索引中始终存在的信息定义,如产品类别或格式。而面则是动态的,由执行搜索后返回的结果生成。
试想一下服装目录:"类别" (如 T 恤、裤子)或"性别" (如男、女)等字段是帮助缩小结果范围的过滤器。而面则反映了结果中出现的产品的具体特征,如常见颜色、可用尺寸或材料。这使得搜索体验更具适应性和情境性。
下面是一张图片,我们与一个切面进行交互,可以看到由切面筛选出的搜索结果。

人工智能如何改善面的生成
人工智能通常与语义搜索和嵌入相关联,那么面呢?如何利用人工智能,使面面俱到对每次搜索更有用,并针对具体情况?
一个引人入胜的可能性是利用人工智能创建新的分类,超越索引中的传统分类。通过分析内容的具体特征,这些新类别可以提供更丰富、更精确的上下文,使面更相关、更符合用户需求。与原始文件类别相比,这使得结果的细化更有意义。
人工智能如何完善电影分类以提高搜索效果
让我们来分析一下以下影片,它们目前都被归类为剧情片类型:
梦之安魂曲 简历:四个科尼岛人吸毒成瘾后,他们的乌托邦被打破了。
美国丽人 简历:一位在性方面受挫的郊区父亲在迷恋上女儿最好的朋友后,陷入了中年危机。
Good Will Hunting 简历:威尔-亨廷是麻省理工学院的看门人,他有数学天赋,但需要心理学家的帮助才能找到人生方向。
这种类型划分无法捕捉到每部影片的细微差别或独特背景。通过利用人工智能分析故事梗概和中心主题,我们可以创建新的类别,更好地反映每部电影的真实背景。例如
梦之安魂曲 - 新类别:"成瘾与依赖"
美国丽人》 - 新类别:"中年危机"
Good Will Hunting - 新类别:"智力斗争"
这些新类别使搜索更加精确,同时为用户提供了更有意义的筛选器来完善搜索结果。当原始分类过于笼统时,这种方法尤为有效,能让用户更轻松地准确找到他们要找的内容。
使用 GPT-4 创建新类别:面搜索示例
在这个例子中,我们将展示如何利用人工智能模型来创建新的电影类别,使其更加精确,并与每部作品的背景相一致。为了演示这一过程,我们将使用 Elastic 仿真管道和 OpenAI 推断服务。我们将创建一个由多个处理器组成的流水线,其中包括脚本处理器,它将负责创建提示语,并在推理处理器中执行,从而确定新的类别。其他处理器将用于处理流水线执行过程中产生的数据和辅助字段。值得一提的是,这一逻辑也可应用于其他类似的工具或模型。
首先,我们需要创建推理端点,将服务定义为 OpenAI、访问服务所需的令牌以及模型。在本例中,我使用的是 gpt-4-mini。有关 OpenAI 推断服务的更多详情,请点击此处。
PUT _inference/completion/generate_topics_ia
{
"service": "openai",
"service_settings": {
"api_key": "your-token",
"model_id": "gpt-4o-mini"
}
}端点创建完成后,我们就可以用它来创建新类别了。下面是一个处理文档数据操作和提示生成整个过程的管道。我将详细解释每个处理器的功能。
第一个处理器将负责建立提示。明确详细的说明非常重要,这样人工智能才能正确分析和识别主题。在本提示中,我要求根据对电影标题、描述和类型的分析来确定 2 个主题。
{
"script": {
"source": """
ctx.prompt = "You are an expert in semantic analysis and audiovisual content categorization. Your task is to generate only subcategories (max 2 topics) that describe specific aspects of movies based on their genres and descriptions. The output should be like: 'n1, n2, ...n'. Here is a movie info to analyze: Title: " + ctx.title + "Genres: " + ctx.genres + "Description: " + ctx.description;
"""
}下一个管道是推理管道,它将接收提示并将其发送到我们的generate_topics_ia端点。模型生成的响应将存储在结果字段中。
{
"inference": {
"model_id": "generate_topics_ia",
"input_output": {
"input_field": "prompt",
"output_field": "result"
}
}
}接下来,除了删除我创建的临时字段外,我们还有 3 个处理器用于处理响应并将其设置到主题字段中。
执行该流水线后,我们将得到以下结果:
{
"docs": [
{
"doc": {
"_index": "index",
"_version": "-3",
"_id": "1",
"_source": {
"description": "While Frodo and Sam edge closer to Mordor with the help of the shifty Gollum, the divided fellowship makes a stand against Sauron's new ally, Saruman, and his hordes of Isengard.",
"model_id": "generate_topics_ia",
"title": "The Lord of the Rings: The Fellowship of the Ring",
"genres": [
"Action",
"Adventure",
"Drama"
],
"topics": [
"Fantasy",
"Quest"
]
},
"_ingest": {
"timestamp": "2024-11-22T17:51:51.340010257Z"
}
}
},
{
"doc": {
"_index": "index",
"_version": "-3",
"_id": "2",
"_source": {
"description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
"model_id": "generate_topics_ia",
"title": "Interstellar",
"genres": [
"Adventure",
"Drama",
"Sci-Fi"
],
"topics": [
"space exploration",
"human survival"
]
},
"_ingest": {
"timestamp": "2024-11-22T17:51:51.340413173Z"
}
}
},
{
"doc": {
"_index": "index",
"_version": "-3",
"_id": "3",
"_source": {
"description": "An astronaut becomes stranded on Mars after his team assume him dead, and must rely on his ingenuity to find a way to signal to Earth that he is alive.",
"model_id": "generate_topics_ia",
"title": "The Martian",
"genres": [
"Adventure",
"Drama",
"Sci-Fi"
],
"topics": [
"survival",
"ingenuity"
]
},
"_ingest": {
"timestamp": "2024-11-22T17:51:51.340427965Z"
}
}
}
]
}请注意,尽管有些影片最初属于同一类型,但我们有了与影片背景更加相关的新类别。
现在,我们可以使用这些新类别,并将它们与文档一起编入索引。这样,在生成切面时,除了主类别外,我们还可以根据影片的背景情况,生成更具体的子类别。
此外,还可以将这些新类别矢量化,并将其用于矢量搜索。这意味着,新的类别不仅可以用作过滤器,还可以用来计算与搜索词的语义相似性,从而进一步提高搜索结果的相关性。
完整的管道:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"source": """
ctx.prompt = "You are an expert in semantic analysis and audiovisual content categorization. Your task is to generate only subcategories (max 2 topics) that describe specific aspects of movies based on their genres and descriptions. The output should be like string: 'n1, n2m ...n'. Here is a movies info to analyze: Title: " + ctx.title + "Genres: " + ctx.genres + "Description: " + ctx.description;
"""
}
},
{
"inference": {
"model_id": "generate_topics_ia",
"input_output": {
"input_field": "prompt",
"output_field": "result"
}
}
},
{
"split": {
"field": "result",
"target_field": "topics",
"separator": ", "
}
},
{
"remove": {
"field": "result"
}
},
{
"remove": {
"field": "prompt"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "1",
"_source": {
"title": "The Lord of the Rings: The Fellowship of the Ring",
"description": "While Frodo and Sam edge closer to Mordor with the help of the shifty Gollum, the divided fellowship makes a stand against Sauron's new ally, Saruman, and his hordes of Isengard.",
"genres": [
"Action",
"Adventure",
"Drama"
]
}
},
{
"_index": "index",
"_id": "2",
"_source": {
"title": "Interstellar",
"description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
"genres": [
"Adventure", "Drama", "Sci-Fi"
]
}
},
{
"_index": "index",
"_id": "3",
"_source": {
"title": "The Martian",
"description": "An astronaut becomes stranded on Mars after his team assume him dead, and must rely on his ingenuity to find a way to signal to Earth that he is alive.",
"genres": [
"Adventure", "Drama", "Sci-Fi"
]
}
}
]
}结论
利用人工智能改进面,可以使搜索结果更具体、更符合上下文,从而改变搜索体验。固定类别通常比较宽泛,而人工智能生成的类别则不同,它能更好地反映背景情况。例如,在对电影进行重新分类时,我们可以捕捉到主要类别所忽略的背景,从而提供更相关的分组。
将这些新的类别添加到索引中,不仅可以改进分面,还可以实现矢量搜索。其结果是搜索体验更加高效,筛选器更加符合上下文。
参考资料
https://www.elastic.co/guide/en/elasticsearch/reference/current/infer-service-openai.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/simulate-pipeline-api.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html
常见问题
什么是分面搜索?
分面搜索是电子商务平台的一种工具。它有助于根据显示项目的特征来组织和完善搜索结果。
人工智能如何改进分面搜索?
人工智能可以超越索引中的传统分类,创建新的分类,使搜索结果更具体、更符合上下文,从而改进搜索中的面生成。




