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

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

相关推荐
郝开10 小时前
ElasticSearch 分词器介绍及测试:Standard(标准分词器)、English(英文分词器)、Chinese(中文分词器)、IK(IK 分词器)
elasticsearch·中文分词·ik·ik analyzer
kngines12 小时前
【实战ES】实战 Elasticsearch:快速上手与深度实践-3.2.3 案例:新闻搜索引擎的相关性优化
大数据·elasticsearch·搜索引擎
天草二十六_简村人21 小时前
JPA编程,去重查询ES索引中的字段,对已有数据的去重过滤,而非全部字典数据
java·大数据·spring boot·elasticsearch·搜索引擎·微服务·架构
C182981825751 天前
ES Filter Query 区别
elasticsearch
Elastic 中国社区官方博客1 天前
Elasticsearch:使用 BigQuery 提取数据
大数据·数据库·elasticsearch·搜索引擎·全文检索
小诸葛IT课堂1 天前
MySQL数据实时同步至Elasticsearch的高效方案:Java实现+源码解析,一文搞定!
java·mysql·elasticsearch
山上春1 天前
常见的 Git 命令
大数据·git·elasticsearch
kngines1 天前
【实战ES】实战 Elasticsearch:快速上手与深度实践-3.1.3高亮与排序的性能陷阱
大数据·elasticsearch·搜索引擎
palomua2 天前
25.3.7#Git命令#merge和rebase的区别
大数据·git·elasticsearch
我的ID配享太庙呀2 天前
Centos的ElasticSearch安装教程
运维·计算机网络·elasticsearch·centos·智能路由器·jenkins·es