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

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

相关推荐
Jackson@ML21 分钟前
一分钟了解大语言模型(LLMs)
人工智能·语言模型·自然语言处理
让学习成为一种生活方式24 分钟前
大麦(Hordeum vulgare)中 BAHD 超家族酰基转移酶-文献精读129
人工智能
思茂信息27 分钟前
CST软件对OPERA&CST软件联合仿真汽车无线充电站对人体的影响
c语言·开发语言·人工智能·matlab·汽车·软件构建
墨绿色的摆渡人30 分钟前
pytorch小记(二十):深入解析 PyTorch 的 `torch.randn_like`:原理、参数与实战示例
人工智能·pytorch·python
lqjun082733 分钟前
Pytorch实现常用代码笔记
人工智能·pytorch·笔记
qyhua34 分钟前
用 PyTorch 从零实现简易GPT(Transformer 模型)
人工智能·pytorch·transformer
markuszhang1 小时前
Elasticsearch 官网阅读之 Term-level Queries
大数据·elasticsearch·搜索引擎
帝锦_li1 小时前
ElasticSearch
elasticsearch·搜索引擎
白熊1881 小时前
【计算机视觉】OpenCV项目实战:基于face_recognition库的实时人脸识别系统深度解析
人工智能·opencv·计算机视觉
桃花键神1 小时前
华为云Flexus+DeepSeek征文|基于Dify平台tiktok音乐领域热门短视频分析Ai agent
人工智能·华为云