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.  }

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

相关推荐
万山y2 小时前
es快速上手(从MySQL角度)
mysql·elasticsearch·jenkins
你的坚持终将美好,4 小时前
谷歌es插件elasticsearch-head.crx
大数据·elasticsearch·搜索引擎
小白的码BUG之路4 小时前
Elasticsearch-kibana索引操作
大数据·数据库·elasticsearch
万山y4 小时前
## Docker 中 Elasticsearch 启动失败:日志文件权限问题排查与解决
elasticsearch·docker·jenkins
Yweir12 小时前
ElasticSearch 8.x 快速上手并了解核心概念
elasticsearch·搜索引擎
weixin_4723394612 小时前
基于Elasticsearch的搜索引擎简介
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客12 小时前
JavaScript 中使用 Elasticsearch 的正确方式,第一部分
大数据·开发语言·javascript·数据库·elasticsearch·搜索引擎·全文检索
ThomasChan12312 小时前
Win10 安装单机版ES(elasticsearch),整合IK分词器和安装Kibana
java·大数据·elasticsearch·搜索引擎·全文检索·jenkins·es
令狐少侠201113 小时前
elasticsearch之记录es7.17升级8.17 springboot2.7.0 程序改造坑
大数据·elasticsearch·jenkins
Elastic 中国社区官方博客1 天前
Elasticsearch 索引副本数
大数据·数据库·elasticsearch·搜索引擎·全文检索