← 中文资源

简体中文版本

语义检索

语义检索按含义而非精确词匹配,用嵌入检索即使用词不同但意图相近的结果,通常与关键词检索混合使用。

阅读英文原文

概念概述

语义检索(Semantic Search)指的是:语义检索按含义而非精确词匹配,用嵌入检索即使用词不同但意图相近的结果,通常与关键词检索混合使用。

从更严格的定义看,Semantic search is a retrieval approach that represents queries and documents as vectors and ranks documents by embedding similarity, capturing meaning instead of surface lexical overlap. 该概念在工程实践中的价值,在于它能帮助团队用统一的语言描述一个具体的质量属性或设计约束,从而在评审与协作中减少歧义。

为什么需要它

It is used where users describe what they want in their own words and keyword matching misses valid results. 它与 Text Embeddings、Tokenization and Subword Units、The Context Window 等概念经常一起出现,用于解决相近的工程问题。理解它的适用场景,有助于在多种备选方案之间做出有依据的取舍,而不是凭直觉选择技术方案。

常见误解

  • semantic search always beats keyword search
  • embeddings make exact identifiers and codes searchable

需要避免的反模式

  • dropping lexical search entirely and losing exact-match ability
  • using a general embedding model without domain evaluation

实践要点

  1. Because similarity is learned, synonymous phrasing retrieves the right document, but rare terms, codes, and names may embed poorly and need lexical support.
  2. Hybrid approaches combine embedding similarity with keyword scoring and fusion, capturing both semantic intent and exact tokens.
  3. Evaluation must use real queries with graded relevance, because approximate metrics can look good while users still fail to find results.

参考来源

https://en.wikipedia.org/wiki/Semantic_search

代码示例

export function binarySearch(sorted, target, compare = (a, b) => a - b) {
  let low = 0;
  let high = sorted.length;
  while (low < high) {
    const mid = (low + high) >>> 1;
    const cmp = compare(sorted[mid], target);
    if (cmp < 0) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  const found = low < sorted.length && compare(sorted[low], target) === 0;
  return { index: low, found };
}
<p>
  Search results for <mark>HTML</mark>:
  The <mark>HTML</mark> standard defines the structure of web pages.
  Learning <mark>HTML</mark> is the first step in web development.
</p>

<p>
  The <mark>critical threshold</mark> is reached when
  the temperature exceeds 100&deg;C.
</p>

<blockquote>
  <p>
    It was the best of times, it was the worst of times,
    it was the age of <mark>wisdom</mark>, it was the age
    of <mark>foolishness</mark>.
  </p>
</blockquote>

<p>
  Required fields are marked with <mark>*</mark>.
  Please review the <mark>terms and conditions</mark>
  before proceeding.
</p>

<style>
  mark {
    background: #ffeb3b;
    color: #000;
    padding: 0 2px;
    border-radius: 2px;
  }
  mark.search-result {
    background: #fff3cd;
    border-bottom: 2px solid #ffc107;
  }
  mark.important {
    background: #ffcdd2;
    font-weight: bold;
  }
  mark::before {
    content: "\1F50E";
    margin-right: 2px;
  }
</style>