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"
  ]
}

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

相关推荐
六月的可乐1 分钟前
AI助理前端UI组件-悬浮球组件
前端·人工智能
R-G-B7 分钟前
OpenCV 实战篇——如何测算出任一副图片中的物体的实际尺寸?传感器尺寸与像元尺寸的关系?
人工智能·opencv·工业相机·传感器尺寸·像元·测算图片中的物体尺寸·像元与物体尺寸
Hello123网站14 分钟前
Ferret:苹果发布的多模态大语言模型
人工智能·语言模型·自然语言处理·ai工具
MobotStone16 分钟前
比对手快10倍?更强更精准?谷歌"纳米香蕉"到底藏着什么黑科技
人工智能
爱写代码的小朋友20 分钟前
STEM背景下人工智能素养框架的研究
人工智能
deepwater_zone21 分钟前
大数据(非结构化数据,Spark,MongoDB)
大数据
DreamNotOver21 分钟前
基于Spark的中文文本情感分析系统研究
大数据·分布式·spark·情感分析
self_myth33 分钟前
【考研/面试必备】操作系统核心原理与IPO机制深度解析
大数据·算法
DS小龙哥1 小时前
基于华为云的STM32F103C8T6智能停车场管理系统
大数据·stm32·华为云
大学生毕业题目1 小时前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别