<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title><![CDATA[Jessica Garson - Elasticsearch Labs]]></title>
    <description><![CDATA[Articles and tutorials from the Search team at Elastic]]></description>
    <copyright><![CDATA[© 2026. Elasticsearch B.V. All Rights Reserved]]></copyright>
    <image>
      <title><![CDATA[Jessica Garson - Elasticsearch Labs]]></title>
      <url>https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt1121c0bf0e8a6e65/6a88da6340a1841030ef456f/search-labs-thumbnail.png</url>
      <link>https://www.elastic.co/cn/search-labs/author/jessica-garson</link>
    </image>
    <link>https://www.elastic.co/cn/search-labs/author/jessica-garson</link>
    <atom:link href="https://www.elastic.co/cn/search-labs/rss/author/jessica-garson.xml" rel="self" type="application/rss+xml"/>
    <language><![CDATA[cn]]></language>
    <lastBuildDate>Mon, 14 Sep 2026 21:31:43 GMT</lastBuildDate>
  <item>
    <title><![CDATA[如何在使用案例中实施更好的二进制量化 (BBQ)]]></title>
    <description><![CDATA[探讨为什么要在用例中实施更好的二进制量化 (BBQ) 以及如何实施。]]></description>
    <content:encoded><![CDATA[<p>矢量搜索为实现文本的语义搜索或图像、视频或音频的相似性搜索提供了基础。在矢量搜索中，矢量是数据的数学表示，可能非常庞大，有时也会比较迟钝。更好的二进制量化（以下简称 BBQ）是一种矢量压缩方法。它可以让你找到正确的匹配，同时缩小矢量，使搜索和处理速度更快。本文将介绍 BBQ 和 rescore_vector，这是一个仅适用于量化索引的字段，可自动对向量重新评分。</p><p>本文中提到的所有完整查询和输出都可以在我们的<a href="https://github.com/elastic/elasticsearch-labs/tree/main/supporting-blog-content/how-and-why-bbq">Elasticsearch Labs 代码库中</a>找到。</p><h2>为什么要在使用案例中实施更好的二进制量化 (BBQ)？</h2>注：要深入了解 BBQ 背后的数学原理，请查看下面的<a href="https://www.elastic.co/cn/search-labs/blog/bbq-implementation-into-use-case#further-learning">"进一步学习 "部分</a>。就本博客而言，重点是实施。<p>数学知识固然耐人寻味，但要想完全掌握矢量搜索保持精确的原因，这一点至关重要。归根结底，这一切都与压缩有关，因为事实证明，目前的矢量搜索算法受到数据读取速度的限制。因此，如果能将所有数据都存储到内存中，那么与从存储设备中读取数据相比，速度将得到显著提升 （内存的 读取<a href="https://sre.google/static/pdf/rule-of-thumb-latency-numbers-letter.pdf"> 速度约为固态硬盘的 200 倍</a> ）。</p><p>有几点需要注意：</p><ul><li><p>基于图形的索引，如<a href="https://arxiv.org/pdf/1603.09320">HNSW</a>（层次导航小世界），对于向量检索来说是最快的。</p><ul><li><p>HNSW：一种近似近邻搜索算法，可构建多层图结构，从而实现高效的高维相似性搜索。</p></li></ul></li></ul><img src="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt760bd95c206bfa8f/6a17e2ad505ac393f7ad8a95/590f3b3c72a76023a38a0436cd9ff90a9f80e936-1964x1262.png" alt="HNSW：一种近似近邻搜索算法，可构建多层图结构，从而实现高效的高维相似性搜索。" /><ul><li><p>从根本上说，HNSW 的速度受限于从内存读取数据的速度，或者在最糟糕的情况下，受限于从存储器读取数据的速度。</p><ul><li><p>理想情况下，您希望能够将所有存储的向量加载到内存中。</p></li></ul></li><li><p>嵌入模型通常以 float32 的精度生成向量，每个浮点数 4 个字节。</p></li><li><p>最后，根据向量和/或维数的多少，内存很快就会不够存放所有向量。</p></li></ul><p>如果把这看作是理所当然的，那么一旦你开始摄入数百万甚至数十亿的向量，每个向量都可能有数百甚至数千个维度，你就会发现问题很快就出现了。题为 "<a href="https://www.elastic.co/cn/search-labs/blog/bbq-implementation-into-use-case#approximate-numbers-on-the-compression-ratios">压缩比近似值</a>"的部分提供了一些粗略的数字。</p><h2>开始需要什么？</h2><p>要开始使用，您需要具备以下条件：</p><ul><li><p>如果使用 Elastic Cloud 或内部部署，则需要高于 8.18 的 Elasticsearch 版本。虽然 BBQ 是在 8.16 中引入的，但在本文中，您将使用<code>vector_rescore</code> ，它是在 8.18 中引入的。</p></li><li><p>此外，您还需要确保集群中有一个<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/ml-settings.html">机器学习（ML）节点</a>。(注意：加载模型需要至少 4GB 的 ML 节点，但如果要完成生产工作负载，可能需要更大的节点）。</p></li><li><p>如果使用的是无服务器，则需要选择针对向量进行了优化的实例。</p></li><li><p>您还需要具备矢量数据库方面的基础知识。如果您还不熟悉 Elastic 中的矢量搜索概念，可能需要先查看以下资源：</p><ul><li><p><a href="https://www.elastic.co/cn/search-labs/blog/elastic-vector-database-practical-example">导航弹性矢量数据库</a></p></li><li><p><a href="https://www.elastic.co/cn/blog/retrieval-augmented-generation-explained">检索增强生成背后的重大理念</a></p></li></ul></li></ul><h2>更好的二进制量化 (BBQ) 实现</h2><img src="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt18df00df95ff2ca7/6a17e2af414c6411989450df/4d388078495566f0527e931e0c2e38facdce83c6-1503x748.png" alt="Elasticsearch bbq 实现。" /><p>为了使本博客简单明了，您将在可用时使用内置函数。在这种情况下，<a href="https://www.elastic.co/cn/guide/en/machine-learning/8.17/ml-nlp-e5.html"><code>.multilingual-e5-small</code></a> 向量嵌入模型将直接在 Elasticsearch 内部的机器学习节点上运行。请注意，您可以用自己选择的嵌入器<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/infer-service-openai.html">（OpenAI</a>、<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/infer-service-google-ai-studio.html">Google AI Studio</a>、<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/infer-service-cohere.html">Cohere</a>等）替换<code>text_embedding</code> 模型。如果您喜欢的模型尚未集成，您也可以<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/bring-your-own-vectors.html">自带密集向量嵌入</a>模型）。</p><p>首先，您需要创建一个推理端点，为给定文本生成向量。您将从 Kibana<a href="https://www.elastic.co/cn/guide/en/kibana/8.18/console-kibana.html">Dev Tools 控制台</a>运行所有这些命令。该命令将下载<code>.multilingual-e5-small</code>.如果端点还不存在，它将为您设置端点；这可能需要一分钟的时间。你可以在 Outputs 文件夹中的<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/01-create-an-inference-endpoint-output.json">01-create-an-inference-endpoint-output.json</a>文件中看到预期输出。 </p>PUT _inference/text_embedding/my_e5_model
{
  "service": "elasticsearch",
  "service_settings": {
    "num_threads": 1,
    "model_id": ".multilingual-e5-small",
    "adaptive_allocations": {
      "enabled": true,
      "min_number_of_allocations": 1
    }
  }
}<p>返回后，模型就设置好了，您可以使用以下命令测试模型是否按预期运行。你可以在 Outputs 文件夹中的<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/02-embed-text-output.json">02-embed-text-output.json</a>文件中看到预期输出。</p>POST _inference/text_embedding/my_e5_model
{
  "input": "my awesome piece of text"
}<p>如果遇到训练好的模型没有分配到任何节点的问题，可能需要手动启动模型。</p>POST _ml/trained_models/.multilingual-e5-small/deployment/_start<p>现在，让我们创建一个带有 2 个属性的新映射，一个标准文本字段 (<code>my_field</code>) 和一个 384 维的密集矢量字段 (<code>my_vector</code>) ，以匹配嵌入模型的输出。您还可以覆盖<code>index_options.type to bbq_hnsw</code> 。你可以在 Outputs 文件夹中的文件<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/03-create-byte-qauntized-index-output.json">03-create-byte-qauntized-index-output.json</a>中看到预期输出。</p>PUT bbq-my-byte-quantized-index
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text"
      },
      "my_vector": {
        "type": "dense_vector",
        "dims": 384,
        "index_options": {
          "type": "bbq_hnsw"
        }
      }
    }
  }
}<p>要确保 Elasticsearch 生成向量，可以使用<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/ingest.html">Ingest Pipeline</a>。该管道需要三样东西：端点 (<code>model_id</code>)、要为其创建向量的<code>input_field</code> 以及用于存储这些向量的<code>output_field</code> 。下面的第一条命令将创建推理摄取管道，该管道使用<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/current/inference-apis.html">推理服务 </a>，第二条命令将测试管道是否正常工作。你可以在 Outputs 文件夹中的文件<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/04-create-and-simulate-ingest-pipeline-output.json">04-create and-simulate-ingest-pipeline-output.json</a>中看到预期输出。 </p>PUT _ingest/pipeline/my_inference_pipeline
{
  "processors": [
    {
      "inference": {
        "model_id": "my_e5_model",
        "input_output": [
          {
            "input_field": "my_field",
            "output_field": "my_vector"
          }
        ]
      }
    }
  ]
}

POST _ingest/pipeline/my_inference_pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "my_field": "my awesome text field"
      }
    }
  ]
}<p>现在，您可以使用下面的前 2 个命令添加一些文档，并使用第 3 个命令测试搜索是否有效。你可以在 Outputs 文件夹中的<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/05-bbq-index-output.json">05-bbq-index-output.json</a>文件中查看预期输出。 </p>PUT bbq-my-byte-quantized-index/_doc/1?pipeline=my_inference_pipeline
{
    "my_field": "my awesome text field"
}

PUT bbq-my-byte-quantized-index/_doc/2?pipeline=my_inference_pipeline
{
    "my_field": "some other sentence"
}

GET bbq-my-byte-quantized-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "knn": {
            "field": "my_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "my_e5_model",
                "model_text": "my awesome search field"
              }
            },
            "k": 10,
            "num_candidates": 100
          }
        }
      ]
    }
  },
  "_source": [
    "my_field"
  ]
}<p>正如<a href="https://www.elastic.co/cn/search-labs/blog/better-binary-quantization-lucene-elasticsearch#lucene-benchmarking">本文章</a>所建议的，当您扩展到非数量级的数据时，建议使用重采样和超采样，因为它们有助于在受益于压缩优势的同时保持较高的召回准确率。从 Elasticsearch 8.18 版开始，您可以使用<a href="https://www.elastic.co/cn/guide/en/elasticsearch/reference/8.18/knn-search.html#dense-vector-knn-search-rescoring">rescore_vector</a> 这样做。预期输出在 Outputs 文件夹中的<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/06-bbq-search-8-18-output.json">06-bbq-search-8-18-output.json</a>文件中。</p>GET bbq-my-byte-quantized-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "knn": {
            "field": "my_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "my_e5_model",
                "model_text": "my awesome search field"
              }
            },
            "rescore_vector": {
              "oversample": 3
            },
            "k": 10,
            "num_candidates": 100
          }
        }
      ]
    }
  },
  "_source": [
    "my_field"
  ]
}<p>这些分数与原始数据的分数相比如何？如果您再次进行上述操作，但使用<code>index_options.type: hnsw</code> ，您会发现得分非常接近。你可以在 Outputs 文件夹中的<a href="https://github.com/elastic/elasticsearch-labs/blob/main/supporting-blog-content/how-and-why-bbq/Outputs/07-raw-vector-output.json">07-raw-vector-output.json</a>文件中看到预期输出。</p>PUT my-raw-vector-index
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text"
      },
      "my_vector": {
        "type": "dense_vector",
        "dims": 384,
        "index_options": {
          "type": "hnsw"
        }
      }
    }
  }
}

PUT my-raw-vector-index/_doc/1?pipeline=my_inference_pipeline
{
    "my_field": "my awesome text field"
}

PUT my-raw-vector-index/_doc/2?pipeline=my_inference_pipeline
{
    "my_field": "some other sentence"
}

GET my-raw-vector-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "knn": {
            "field": "my_vector",
            "query_vector_builder": {
              "text_embedding": {
                "model_id": "my_e5_model",
                "model_text": "my awesome search field"
              }
            },
            "k": 10,
            "num_candidates": 100
          }
        }
      ]
    }
  },
  "_source": [
    "my_field"
  ]
}<h2>压缩比的近似值</h2><p>在使用矢量搜索时，存储和内存需求很快就会成为一项重大挑战。下面的细目说明了不同的量化技术如何显著减少矢量数据的内存占用。</p><p>向量 (V)</p><p>尺寸（D）</p><p>未加工（V x D x 4）</p><p>int8 (V x (D x 1 + 4))</p><p>int4 (V x (D x 0.5 + 4))</p><p>bbq (V x (D x 0.125 + 4))</p><p>10,000,000</p><p>384</p><p>14.31GB</p><p>3.61GB</p><p>1.83GB</p><p>0.58GB</p><p>50,000,000</p><p>384</p><p>71.53GB</p><p>18.07GB</p><p>9.13GB</p><p>2.89GB</p><p>100,000,000</p><p>384</p><p>143.05GB</p><p>36.14GB</p><p>18.25GB</p><p>5.77GB</p><h2>结论</h2><p>BBQ 是一种优化方法，可用于压缩矢量数据而不影响精度。它的工作原理是将向量转换为比特，让您能够有效地搜索数据，并使您能够扩展人工智能工作流程，加快搜索速度并优化数据存储。</p><h2>进一步学习</h2><p>如果您想了解有关烧烤的更多信息，请务必查看以下资源：</p><ul><li><p><a href="https://www.elastic.co/cn/search-labs/blog/better-binary-quantization-lucene-elasticsearch">Lucene 和 Elasticsearch 中的二进制量化 (BBQ)</a></p></li><li><p><a href="https://www.elastic.co/cn/search-labs/blog/bit-vectors-elasticsearch-bbq-vs-pq">更好的二进制量化（BBQ）与乘积量化比较</a></p></li><li><p><a href="https://www.elastic.co/cn/search-labs/blog/optimized-scalar-quantization-elasticsearch">优化的标量量化更好的二进制量化</a></p></li><li><p><a href="https://www.youtube.com/watch?v=04NzMt2Nigc">更好的二进制量化 (BBQ)：从字节到烧烤，更好的矢量搜索的秘密》，本-特伦特著</a></p></li></ul>]]></content:encoded>
    <link>https://www.elastic.co/search-labs/blog/bbq-implementation-into-use-case</link>
    <guid isPermaLink="true">https://www.elastic.co/search-labs/blog/bbq-implementation-into-use-case</guid>
    <category><![CDATA[向量数据库]]></category>
    <category><![CDATA[基础功能]]></category>
    <dc:creator><![CDATA[Sachin Frayne,Jessica Garson]]></dc:creator>
    <enclosure url="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt3dd0495b536b2615/6a17e2b0414c6488459450e3/66842055367cdd795532b01c167f2a4b03dc65e3-1200x628.png" length="0" type="image/png"/>
    <pubDate>Wed, 23 Apr 2025 00:00:00 GMT</pubDate>
  </item>
  </channel>
</rss>