Elasticsearch:结合 ELSER 和 BM25 文本查询的相关搜索

Elastic Learned Spare EncodeR (ELSER) 允许你执行语义搜索以获得更相关的搜索结果。 然而,有时,将语义搜索结果与常规关键字搜索结果相结合以获得最佳结果会更有用。 问题是,如何结合文本和语义搜索结果?

首先,让我们看一下对某些字段使用 multi_match 的花园品种文本查询。 这种搜索具有关键字搜索的典型陷阱,即关键字必须以某种形式存在于要返回的文档中,并且我们没有考虑用户搜索内容的上下文。

sql 复制代码
1.  POST search-national-parks/_search
2.  {
3.    "query": {
4.      "multi_match": {
5.        "query": "Where can I see the Northern Lights?",
6.        "fields": ["title", "description"]
7.      }
8.    },
9.    "_source": ["title"]
10.  }

现在,让我们看看 ELSER 查询本身:

sql 复制代码
1.  POST search-national-parks/_search
2.  {
3.    "query": {
4.      "bool": {
5.        "should": [
6.          {
7.            "text_expansion": {
8.              "ml.inference.title_expanded.predicted_value": {
9.                "model_id": ".elser_model_2",
10.                "model_text": "Where can I see the Northern Lights?"
11.              }
12.            }
13.          },
14.          {
15.            "text_expansion": {
16.              "ml.inference.description_expanded.predicted_value": {
17.                "model_id": ".elser_model_2",
18.                "model_text": "Where can I see the Northern Lights?"
19.              }
20.            }
21.          }
22.        ]
23.      }
24.    },
25.    "_source": [
26.      "title"
27.    ]
28.  }

在上面,我们使用 ELSER 来对文章进行语义搜索。如果你对 ELSER 还不是很熟的话,请参阅如下的文章:

组合这两个查询的第一种方法是使用称为线性提升的策略。 在此示例中,我们正在提升文本搜索结果,以便它们具有优先级。 根据你正在运行的查询,这可能是理想的,也可能不是理想的。

sql 复制代码
1.  POST search-national-parks/_search
2.  {
3.    "query": {
4.      "bool": {
5.        "should": [
6.          {
7.            "text_expansion": {
8.              "ml.inference.title_expanded.predicted_value": {
9.                "model_id": ".elser_model_2",
10.                "model_text": "Where can I see the Northern Lights?",
11.                "boost": 1
12.              }
13.            }
14.          },
15.          {
16.            "text_expansion": {
17.              "ml.inference.description_expanded.predicted_value": {
18.                "model_id": ".elser_model_2",
19.                "model_text": "Where can I see the Northern Lights?",
20.                "boost": 1
21.              }
22.            }
23.          },
24.          {
25.            "multi_match": {
26.              "query": "Where can I see the Northern Lights?",
27.              "fields": [
28.                "title",
29.                "description"
30.              ],
31.              "boost": 4
32.            }
33.          }
34.        ]
35.      }
36.    },
37.    "_source": [
38.      "title"
39.    ]
40.  }

最后,我们还可以使用倒数排名融合(RRF)将文本搜索结果与语义结果结合起来,并对返回的搜索结果重新评分:

sql 复制代码
1.  POST search-national-parks/_search
2.  {
3.    "sub_searches": [
4.      {
5.        "query": {
6.          "multi_match": {
7.            "query": "Where can I see the Northern Lights?",
8.            "fields": [
9.              "title",
10.              "description"
11.            ]
12.          }
13.        }
14.      },
15.      {
16.        "query": {
17.          "text_expansion": {
18.            "ml.inference.title_expanded.predicted_value": {
19.              "model_id": ".elser_model_2",
20.              "model_text": "Where can I see the Northern Lights?"
21.            }
22.          }
23.        }
24.      },
25.      {
26.        "query": {
27.          "text_expansion": {
28.            "ml.inference.description_expanded.predicted_value": {
29.              "model_id": ".elser_model_2",
30.              "model_text": "Where can I see the Northern Lights?"
31.            }
32.          }
33.        }
34.      }
35.    ],
36.    "rank": {
37.      "rrf": {
38.        "window_size": 10,
39.        "rank_constant": 20
40.      }
41.    },
42.    "_source": [
43.      "title", "states"
44.    ]
45.  }

这些示例应该可以帮助你开始为你的用例创建最相关的搜索结果的旅程!

相关推荐
Elastic 中国社区官方博客2 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
alfiy3 小时前
Elasticsearch学习笔记(四) Elasticsearch集群安全配置一
笔记·学习·elasticsearch
alfiy4 小时前
Elasticsearch学习笔记(五)Elastic stack安全配置二
笔记·学习·elasticsearch
丶213620 小时前
【大数据】Elasticsearch 实战应用总结
大数据·elasticsearch·搜索引擎
闲人编程21 小时前
elasticsearch实战应用
大数据·python·elasticsearch·实战应用
世俗ˊ1 天前
Elasticsearch学习笔记(3)
笔记·学习·elasticsearch
weixin_466286681 天前
ElasticSearch入门
大数据·elasticsearch·搜索引擎
Elasticsearch1 天前
使用模拟和真实的 Elasticsearch 来测试你的 Java 代码
elasticsearch
沐曦可期1 天前
Elasticsearch学习记录
大数据·学习·elasticsearch
alfiy2 天前
ElasticSearch学习笔记(三)Ubuntu 2204 server elasticsearch集群配置
笔记·学习·elasticsearch