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

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

相关推荐
chen<>6 小时前
Git原理与应用
大数据·git·elasticsearch·svn
liuyunshengsir11 小时前
Elasticsearch 高级查询must 多个条件同时满足
linux·服务器·elasticsearch
Cx330❀1 天前
Git 多人协作全攻略:从入门到高效协同
大数据·elasticsearch·搜索引擎·gitee·github·全文检索·gitcode
bigHead-1 天前
Git合并操作详解:安全高效地合并远程分支
git·安全·elasticsearch
奇树谦1 天前
FastDDS阿里云DDSRouter安装和使用(失败)
elasticsearch·阿里云·云计算
色空大师1 天前
服务打包包名设置
java·elasticsearch·maven·打包
码农很忙1 天前
从0到1搭建实时日志监控系统:基于WebSocket + Elasticsearch的实战方案
websocket·网络协议·elasticsearch
Elastic 中国社区官方博客1 天前
在 ES|QL 中的混合搜索和多阶段检索
大数据·人工智能·sql·elasticsearch·搜索引擎·ai·全文检索
Elasticsearch2 天前
在 ES|QL 中的混合搜索和多阶段检索
elasticsearch
AC赳赳老秦2 天前
量化交易脚本开发:DeepSeek生成技术指标计算与信号触发代码
数据库·elasticsearch·信息可视化·流程图·数据库架构·memcached·deepseek