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

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

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

复制代码
POST search-national-parks/_search
{
  "query": {
    "multi_match": {
      "query": "Where can I see the Northern Lights?",
      "fields": ["title", "description"]
    }
  },
  "_source": ["title"]
}

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

复制代码
POST search-national-parks/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "text_expansion": {
            "ml.inference.title_expanded.predicted_value": {
              "model_id": ".elser_model_2",
              "model_text": "Where can I see the Northern Lights?"
            }
          }
        },
        {
          "text_expansion": {
            "ml.inference.description_expanded.predicted_value": {
              "model_id": ".elser_model_2",
              "model_text": "Where can I see the Northern Lights?"
            }
          }
        }
      ]
    }
  },
  "_source": [
    "title"
  ]
}

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

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

复制代码
POST search-national-parks/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "text_expansion": {
            "ml.inference.title_expanded.predicted_value": {
              "model_id": ".elser_model_2",
              "model_text": "Where can I see the Northern Lights?",
              "boost": 1
            }
          }
        },
        {
          "text_expansion": {
            "ml.inference.description_expanded.predicted_value": {
              "model_id": ".elser_model_2",
              "model_text": "Where can I see the Northern Lights?",
              "boost": 1
            }
          }
        },
        {
          "multi_match": {
            "query": "Where can I see the Northern Lights?",
            "fields": [
              "title",
              "description"
            ],
            "boost": 4
          }
        }
      ]
    }
  },
  "_source": [
    "title"
  ]
}

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

复制代码
POST search-national-parks/_search
{
  "sub_searches": [
    {
      "query": {
        "multi_match": {
          "query": "Where can I see the Northern Lights?",
          "fields": [
            "title",
            "description"
          ]
        }
      }
    },
    {
      "query": {
        "text_expansion": {
          "ml.inference.title_expanded.predicted_value": {
            "model_id": ".elser_model_2",
            "model_text": "Where can I see the Northern Lights?"
          }
        }
      }
    },
    {
      "query": {
        "text_expansion": {
          "ml.inference.description_expanded.predicted_value": {
            "model_id": ".elser_model_2",
            "model_text": "Where can I see the Northern Lights?"
          }
        }
      }
    }
  ],
  "rank": {
    "rrf": {
      "window_size": 10,
      "rank_constant": 20
    }
  },
  "_source": [
    "title", "states"
  ]
}

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

相关推荐
carpell35 分钟前
【语义分割专栏】3:Segnet实战篇(附上完整可运行的代码pytorch)
人工智能·python·深度学习·计算机视觉·语义分割
智能汽车人1 小时前
自动驾驶---SD图导航的规划策略
人工智能·机器学习·自动驾驶
mengyoufengyu1 小时前
DeepSeek11-Ollama + Open WebUI 搭建本地 RAG 知识库全流程指南
人工智能·深度学习·deepseek
Tianyanxiao1 小时前
华为×小鹏战略合作:破局智能驾驶深水区的商业逻辑深度解析
大数据·人工智能·经验分享·华为·金融·数据分析
rit84324991 小时前
基于BP神经网络的语音特征信号分类
人工智能·神经网络·分类
一点.点2 小时前
AlphaDrive:通过强化学习和推理释放自动驾驶中 VLM 的力量
人工智能·机器学习·自动驾驶
科技小E2 小时前
口罩佩戴检测算法AI智能分析网关V4工厂/工业等多场景守护公共卫生安全
网络·人工智能
说私域2 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的首屏组件优化策略研究
人工智能·小程序·开源·零售
vlln2 小时前
2025年与2030年AI及AI智能体 (Agent) 市场份额分析报告
人工智能·深度学习·神经网络·ai
栗克2 小时前
Halcon 图像预处理②
人工智能·计算机视觉·halcon