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

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

相关推荐
极小狐1 小时前
极狐GitLab 功能标志详解
linux·运维·服务器·elasticsearch·gitlab·极狐gitlab
bulucc3 小时前
回归,git 分支开发操作命令
大数据·git·elasticsearch
极小狐5 小时前
极狐GitLab 安全文件管理功能介绍
linux·运维·数据库·安全·elasticsearch·gitlab
Java程序之猿7 小时前
Spring Boot 集成spring-boot-starter-data-elasticsearch
spring boot·elasticsearch·jenkins
Elasticsearch7 小时前
如何增加 Elasticsearch 中的 primary shard 数量
elasticsearch
阿里云大数据AI技术8 小时前
阿里云 Elasticsearch Serverless 检索增强型 8.17 版来袭!
大数据·elasticsearch·serverless
星释10 小时前
GitLab-CI集成FTP自动发布
elasticsearch·ci/cd·gitlab
-曾牛16 小时前
Git完全指南:从入门到精通版本控制 ------- Git仓库创建 (5)
大数据·网络·git·学习·elasticsearch·个人开发
twj_one19 小时前
[ElasticSearch]Suggest查询建议(自动补全&纠错)
大数据·elasticsearch·搜索引擎
Willliam_william19 小时前
QEMU学习之路(8)— ARM32通过u-boot 启动Linux
linux·学习·elasticsearch