全部產品
Search
文件中心

Elasticsearch:FalconSeek向量索引使用指南

更新時間:Dec 11, 2025

本文檔詳細介紹FalconSeek在向量檢索情境中的進階用法,協助您根據需求(如大規模資料存放區成本敏感、極致效能最佳化等)選擇HNSW、RabitQGraph等演算法進行加速查詢。涵蓋演算法選型指南、參數配置、索引管理及動態調優技巧,並提供完整程式碼範例,協助實現高效向量檢索。

背景資訊

FalconSeek在阿里雲ES原有索引結構之上,增加了基於C++的向量引擎全新索引,FalconSeek向量索引是阿里巴巴自主研發,支援了阿里巴巴集團內淘寶、天貓搜尋、推薦、拍立淘等集團內的主要業務,您可以利用 FalconSeek 核心整合的高效能向量索引,構建高效的以圖搜圖、語義搜尋等 AI 應用。

FalconSeek 向量索引完全相容開源 ES 向量引擎功能(如kNN 搜尋等)。在阿里雲ES索引中使用FalconSeek僅需一步:在索引配置中將 index_options.type 設定為 havenask_native,更多進階參數配置請參見下文詳細說明。

使用樣本

基礎樣本

以下樣本建立一個儲存和檢索向量資料的索引,索引名為 my_falcon_seek_index ,其中包含一個名為 product_vector 的向量欄位,維度為 128,並使用HNSW 演算法。

  1. 建立索引

    PUT /my_falcon_seek_index
    {
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
      },
      "mappings": {
        "properties": {
          "product_vector": {
            "type": "dense_vector",
            "dims": 128,
            "index": true,
            "similarity": "l2_norm",
            "index_options": {
              "type": "havenask_native",
              "knn_type": "HNSW",
              "m": 32,
              "ef_construction": 400
            }
          },
          "category": {
            "type": "keyword" 
          }
        }
      }
    }

    核心參數:

    • dense_vector 欄位類型,是用於儲存稠密向量資料的專用欄位類型。在 mappings 中定義此類型欄位時,必須指定以下核心屬性:

      • dims:向量的維度。

      • similarity:向量間的相似性計算函數。

      • index_options:向量索引的詳細配置,包括演算法類型和相關參數。

    • similarity 相似性函數,用于衡量兩個向量之間的相似程度,選擇合適的函數對召回效果至關重要。

      函數

      說明

      適用情境

      l2_norm

      歐氏距離。計算兩個向量在多維空間中的直線距離。距離越小,越相似。

      通用情境,如Image Recognition、Face Service。

      cosine

      餘弦相似性。計算兩個向量方向的夾角餘弦值。值越接近 1,越相似。

      文本語義相似性分析,不受向量長度影響。

      dot_product

      內積。計算兩個向量的點積。值越大,越相似。

      推薦系統等需要考慮向量模長的情境。

      max_inner_product

      與dot_product功能一樣,但不要求向量歸一化。

      推薦系統等需要考慮向量模長的情境。

  2. 寫入資料:向索引中寫入包含向量和中繼資料(如商品類別)的文檔,product_vector 數組的長度必須與 dims 定義的維度(128)完全一致。

    POST /my_falcon_seek_index/_doc/1
    {
      "product_vector": [0.12, -0.05, 0.08, 0.24, -0.17, 0.31, 0.02, -0.19, 0.11, 0.28,
        -0.03, 0.15, 0.22, -0.11, 0.09, 0.33, -0.07, 0.14, 0.26, -0.21,
        0.18, 0.29, -0.13, 0.06, 0.35, -0.08, 0.16, 0.23, -0.15, 0.12, 
        0.27, -0.22, 0.19, 0.32, -0.14, 0.07, 0.25, -0.18, 0.13, 0.30,
        -0.09, 0.17, 0.24, -0.16, 0.10, 0.34, -0.10, 0.20, 0.31, -0.23,
        0.15, 0.28, -0.12, 0.11, 0.26, -0.19, 0.14, 0.29, -0.17, 0.08,
        0.22, -0.20, 0.16, 0.27, -0.15, 0.09, 0.25, -0.21, 0.18, 0.30,
        -0.13, 0.07, 0.24, -0.22, 0.19, 0.32, -0.16, 0.10, 0.26, -0.18,
        0.12, 0.28, -0.14, 0.06, 0.23, -0.19, 0.15, 0.29, -0.11, 0.05,
        0.21, -0.17, 0.13, 0.27, -0.10, 0.04, 0.20, -0.15, 0.11, 0.25,
        -0.09, 0.03, 0.19, -0.13, 0.10, 0.24, -0.08, 0.02, 0.18, -0.12,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22, 0.05, -0.06,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22],
      "category": "clothes"
    }
    POST /my_falcon_seek_index/_doc/2
    {"product_vector":[0.12, -0.05, 0.08, 0.24, -0.17, 0.31, 0.02, -0.19, 0.11, 0.28,
        -0.03, 0.15, 0.22, -0.11, 0.09, 0.33, -0.07, 0.14, 0.26, -0.21,
        0.18, 0.29, -0.13, 0.06, 0.35, -0.08, 0.16, 0.23, -0.15, 0.12, 
        0.27, -0.22, 0.19, 0.32, -0.14, 0.07, 0.25, -0.18, 0.13, 0.30,
        -0.09, 0.17, 0.24, -0.16, 0.10, 0.34, -0.10, 0.20, 0.31, -0.23,
        0.15, 0.28, -0.12, 0.11, 0.26, -0.19, 0.14, 0.29, -0.17, 0.08,
        0.22, -0.20, 0.16, 0.27, -0.15, 0.09, 0.25, -0.21, 0.18, 0.30,
        -0.13, 0.07, 0.24, -0.22, 0.19, 0.32, -0.16, 0.10, 0.26, -0.18,
        0.12, 0.28, -0.14, 0.06, 0.23, -0.19, 0.15, 0.29, -0.11, 0.05,
        0.21, -0.17, 0.13, 0.27, -0.10, 0.04, 0.20, -0.15, 0.11, 0.25,
        -0.09, 0.03, 0.19, -0.13, 0.10, 0.24, -0.08, 0.02, 0.18, -0.12,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22, 0.05, -0.06,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22],"category": "clothes"}
  3. 向量檢索(k-NN): 根據給定的查詢向量,尋找最相似的 5個文檔。

    GET /my_falcon_seek_index/_search
    {
      "knn": {
        "field": "product_vector",
        "query_vector": [0.12, -0.05, 0.01, 0.24, -0.17, 0.31, 0.02, -0.19, 0.11, 0.28,
        -0.03, 0.15, 0.22, -0.11, 0.09, 0.23, -0.07, 0.14, 0.26, -0.21,
        0.18, 0.29, -0.13, 0.06, 0.35, -0.18, 0.16, 0.23, -0.15, 0.12, 
        0.27, -0.22, 0.19, 0.32, -0.14, 0.87, 0.25, -0.18, 0.13, 0.30,
        -0.09, 0.17, 0.24, -0.16, 0.10, 0.64, -0.10, 0.20, 0.31, -0.23,
        0.15, 0.28, -0.12, 0.11, 0.26, -0.19, 0.14, 0.29, -0.17, 0.08,
        0.22, -0.20, 0.16, 0.27, -0.15, 0.09, 0.25, -0.21, 0.18, 0.30,
        -0.13, 0.07, 0.24, -0.22, 0.19, 0.52, -0.16, 0.10, 0.26, -0.18,
        0.12, 0.28, -0.14, 0.06, 0.23, -0.19, 0.14, 0.29, -0.11, 0.05,
        0.21, -0.17, 0.13, 0.27, -0.10, 0.04, 0.20, -0.15, 0.11, 0.25,
        -0.09, 0.03, 0.19, -0.13, 0.10, 0.24, -0.08, 0.02, 0.18, -0.12,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22, 0.05, -0.06,
        0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.12],
        "k": 5,
        "num_candidates": 100
      }
    }

    核心參數:

    knn 查詢文法,是執行 k-最近鄰(k-Nearest Neighbors)搜尋的專用查詢體。

    • field:要查詢的 dense_vector 欄位名。

    • query_vector:用於查詢的向量,其維度必須與欄位定義一致。

    • k:需要返回的最相似結果數量。

    • num_candidates:在每個分區上,演算法內部搜尋的候選集大小。該值大於 k,通常是 k 的數倍。值越大,召回率越高,但查詢延遲也相應增加。

  4. 過濾檢索:在執行向量檢索的同時,對結果進行過濾,以滿足更複雜的業務需求。以下樣本將在 category 為 "shoes" 的文檔中,尋找最相似的 5 個文檔。

    GET /my_falcon_seek_index/_search
    {
      "knn": {
        "field": "product_vector",
        "query_vector": [
          0.12, -0.05, 0.01, 0.24, -0.17, 0.31, 0.02, -0.19, 0.11, 0.28,
          -0.03, 0.15, 0.22, -0.11, 0.09, 0.23, -0.07, 0.14, 0.26, -0.21,
          0.18, 0.29, -0.13, 0.06, 0.35, -0.18, 0.16, 0.23, -0.15, 0.12,
          0.27, -0.22, 0.19, 0.32, -0.14, 0.87, 0.25, -0.18, 0.13, 0.30,
          -0.09, 0.17, 0.24, -0.16, 0.10, 0.64, -0.10, 0.20, 0.31, -0.23,
          0.15, 0.28, -0.12, 0.11, 0.26, -0.19, 0.14, 0.29, -0.17, 0.08,
          0.22, -0.20, 0.16, 0.27, -0.15, 0.09, 0.25, -0.21, 0.18, 0.30,
          -0.13, 0.07, 0.24, -0.22, 0.19, 0.52, -0.16, 0.10, 0.26, -0.18,
          0.12, 0.28, -0.14, 0.06, 0.23, -0.19, 0.14, 0.29, -0.11, 0.05,
          0.21, -0.17, 0.13, 0.27, -0.10, 0.04, 0.20, -0.15, 0.11, 0.25,
          -0.09, 0.03, 0.19, -0.13, 0.10, 0.24, -0.08, 0.02, 0.18, -0.12,
          0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.22, 0.05, -0.06,
          0.09, 0.23, -0.07, 0.01, 0.17, -0.11, 0.08, 0.12
        ],
        "k": 5,
        "num_candidates": 100,
        "filter": {
          "term": {
            "category": "shoes"
          }
        }
      }
    }

擴充功能:tags_filter過濾檢索

tags_filter 是一種專為 HNSW 和 QGraph 演算法最佳化的前置過濾機制。與標準的 bool filter 相比,它在圖遍曆的早期階段就排除不匹配的節點,因此效能更高。

  • 適用情境:當過濾欄位取值有限(如類目、品牌 ID),且對查詢效能要求極致時。

  • 如何啟用:

    1. 在 mappings 的 index_options 中,使用 tags 參數聲明要用於過濾的 keyword 類型欄位。

      PUT /my_vector_index_with_tags
      {
        "mappings": {
          "properties": {
            "product_vector": {
              "type": "dense_vector",
              "dims": 128,
              "index_options": {
                "type": "havenask_native",
                "knn_type": "HNSW",
                "tags": ["category"] // 聲明 category 欄位用於 tags_filter
              }
            },
            "category": {
              "type": "keyword" // 必須是 keyword 類型
            }
          }
        }
      }
    2. 在查詢的 knn 體中使用 tags_filter 參數,並採用 "field_name = value" 文法。支援 | (OR) 和 & (AND) 邏輯。

      GET /my_vector_index_with_tags/_search
      {
        "knn": {
          "field": "product_vector",
          "query_vector": [...],
          "k": 5,
          "tags_filter": "category = shoes | category = socks"
        }
      }
      

演算法選擇與效能指南

選擇合適的 knn_type 演算法對於實現效能、成本和精度的最佳平衡至關重要。

演算法對比與決策建議

演算法

召回率

查詢速度

記憶體佔用

適用情境與決策建議

核心限制

HNSW

通用首選。

適用於絕大多數情境,在召回率、效能和資源消耗之間取得了最佳平衡。資料規模在 10 萬到 1000 萬時表現優異。

無特殊限制。

RabitQGraph

最快

最低

極致效能與成本敏感情境。

適用於對查詢延遲有毫秒級響應要求,或記憶體成本極其敏感的超大規模(千萬級以上)資料集。

僅支援 l2_norm 相似性;向量維度必須是 64 的正整數倍。

QGraph

大規模資料且儲存成本敏感情境。

通過向量量化技術顯著降低記憶體和儲存佔用,適用於 500 萬以上規模的資料集。

無特殊限制。

QC

大規模資料且記憶體受限情境。當構建時間不敏感,但運行時記憶體資源非常緊張時可以考慮。適用於 100 萬以上規模的資料集。

構建時間較長。

Linear

最高 (100%)

小資料集或要求絕對精確情境。

適用於資料總量小於 brute_force_threshold(預設 1000)或 linear_build_threshold 的情境,系統會自動切換。查詢時間隨資料量線性增長。

僅適用於小資料集。

情境化選型路徑

  • 剛開始或不確定時:直接選擇 HNSW

  • 資料量增長,效能下降:

    • 如果記憶體充足,追求更高 QPS:從 HNSW 遷移到 RabitQGraph (需滿足其限制)。

    • 如果記憶體/儲存成本過高:從 HNSW 遷移到 QGraph,並配置 quantizer

  • 超大規模資料集(千萬級以上):

    • 對延遲要求極高:直接選用 RabitQGraph

    • 對成本敏感,延遲要求稍低:選用 QGraph

索引管理 (Mappings)

在建立索引時,所有向量相關的配置都在 mappings 的 index_options 塊內完成。

PUT /<your_index_name>
{
  "mappings": {
    "properties": {
      "<your_vector_field>": {
        "type": "dense_vector",
        "dims": 768,
        "similarity": "l2_norm",
        "index_options": {
          // --- 通用參數 ---
          "type": "havenask_native",
          "knn_type": "HNSW",
          // --- 演算法構建參數 ---
          "m": 32,
          "ef_construction": 400,
          // --- 進階參數 ---
          "thread_count": 8
        }
      }
    }
  }
}

通用參數

參數

描述

類型

是否必需

預設值

type

指定使用 FalconSeek 的向量索引引擎。必須設定為 "havenask_native"

String

knn_type

指定使用的向量索引演算法。可選值為 "HNSW""RabitQGraph""QGraph""QC""Linear"

String

"HNSW"

演算法構建參數

以下參數在索引構建時生效,決定了索引的結構和品質。

HNSW / QGraph 共有參數

參數

描述

類型

預設值

取值建議與影響

m

圖中每個節點的最大鄰居數。

Integer

16

影響:直接影響召回率和記憶體佔用。
建議:16 (低記憶體)、32 (平衡)、64-128 (高召回率)。值越大,召回率越高,但記憶體佔用和構建時間也越長。取值範圍:4-128。

ef_construction

構建圖時搜尋候選集的寬度。

Integer

200

影響:決定索引構建的品質和時間。
建議:200 (快速構建)、400-500 (平衡)、800+ (高品質構建)。值越大,索引品質越高(最終召回率也越高),但構建時間越長。取值範圍:10-2000。

QGraph 專用參數

參數

描述

類型

預設值

取值建議與影響

quantizer

向量量化方式,用於壓縮向量以減少記憶體和儲存佔用。

String

影響:顯著降低資源佔用,但可能帶來輕微精度損失。
建議:"int8" (推薦,8倍壓縮)、"int4" (更高壓縮比)、"fp16" (高精度,2倍壓縮)、"2bit" (極致壓縮)。

進階參數

參數

描述

類型

預設值

取值建議與影響

thread_count

索引構建時使用的並行線程數。

Integer

1

影響:加快構建速度。
建議:設定為 0 可自動使用機器的所有 CPU 核心數,或根據資源情況指定具體數值(1-32)。

tags

聲明用於 tags_filter 高效能過濾的欄位列表。

Array

[]

影響:啟用高效能前置過濾。
建議:將取值基數小、過濾頻繁的 keyword 欄位加入列表,如 ["category", "brand_id"]

linear_build_threshold

當索引的文檔總數低於此閾值時,自動切換為 Linear(暴力)搜尋,以最佳化小資料集情境的資源消耗和查詢效率。

Integer

0

影響:避免為小資料集構建複雜索引結構。
建議:對於文檔數可能很少的索引,可設定為 1000 或 5000

index_params

底層引擎的參數配置介面,用於進階調優。

Object

{}

影響:可以精細控制構建和查詢的每一個細節。
重要:index_params 內的參數會覆蓋頂層同名參數(如 mef_construction)。這是為進階使用者提供的介面,普通使用者推薦使用頂層參數。

index_params 提供了對底層 proxima.* 和 param.* 參數的直接存取能力。例如,頂層的 m 參數對應底層的 proxima.hnsw.builder.max_neighbor_count。僅當您需要調整頂層參數未暴露的細節時,才需要使用此配置。

json

"index_options": {  "knn_type": "HNSW",  "m": 32, // 會被下面的 index_params 覆蓋  "index_params": {    "proxima.hnsw.builder.max_neighbor_count": 48 // 最終生效的是 48  }}

knn查詢體

參數

描述

類型

是否必需

field

要執行 k-NN 搜尋的 dense_vector 欄位名。

String

query_vector

用於查詢的向量。

Array

k

返回最相似結果的數量。

Integer

num_candidates

每個分區上的搜尋候選集大小。值越大,召回率越高,查詢越慢。

Integer

否 (推薦設定)

tags_filter

(可選)用於 HNSW 和 QGraph 的高效能前置過濾。

String

search_params

(可選)在查詢時動態調整部分搜尋參數,用於臨時調優。

Object

參數說明

HNSW

HNSW (Hierarchical Navigable Small Worlds) 演算法的參數使用 proxima.hnsw. 命名空間,可通過 index_params 設定。

HNSW Builder

參數名

類型

預設值

說明

proxima.hnsw.builder.max_neighbor_count

uint32

100

圖的鄰居數,值越大圖越精確,但計算和儲存開銷越大,一般不超過特徵維度,最大65535個

proxima.hnsw.builder.efconstruction

uint32

500

用於控製圖的構建精度,值越大構建的圖越精確但構建更耗時

proxima.hnsw.builder.thread_count

uint32

0

構建時開啟線程數量,設定為0時為CPU核心數

proxima.hnsw.builder.memory_quota

uint64

0

限制最大構建記憶體,暫不支援磁碟構建,當超過此值時會構建失敗

proxima.hnsw.builder.scaling_factor

uint32

50

每層圖之間節點比例,一般無須修改,取值範圍 [5,1000]

proxima.hnsw.builder.neighbor_prune_ratio

float

0.5

用於控制鄰居表開始裁邊鄰居數,一般無須修改

proxima.hnsw.builder.upper_neighbor_ratio

float

0.5

圖的上層鄰居數相對0層圖鄰居比例,一般無須修改

proxima.hnsw.builder.enable_adsampling

bool

false

預設不開啟,目前只支援fp32資料集下的歐式距離計算,256維以下不建議開啟

proxima.hnsw.builder.slack_pruning_factor

float

1.0

預設為1.0,建議在 [1.1, 1.2] 之間,gist960、sift128建議 1.1

HNSW Searcher

參數名

類型

預設值

說明

proxima.hnsw.searcher.ef

uint32

500

用於檢索時考察精度,值越大掃描doc數越多,召回率越高

proxima.hnsw.searcher.max_scan_ratio

float

0.1

用在檢索時控制最多掃描文檔的比例,如ef值提前收斂則不會掃描到此比例

proxima.hnsw.searcher.neighbors_in_memory_enable

bool

false

開啟時將鄰居表儲存在記憶體,提升效能但消耗較多記憶體

proxima.hnsw.searcher.check_crc_enable

bool

false

是否對索引進行CRC校正,開啟時載入時間會延長

proxima.hnsw.searcher.visit_bloomfilter_enable

bool

false

使用bloomfilter作為graph節點訪問去重容器,記憶體最佳化但效能稍差

proxima.hnsw.searcher.visit_bloomfilter_negative_prob

float

0.001

bloomfilter的準確度,越小越精確但記憶體佔用越大

proxima.hnsw.searcher.brute_force_threshold

int

1000

當總doc數量小於此值時,走線性檢索

HNSW配置樣本

// 基礎 HNSW 配置
PUT /hnsw_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 768,
        "index": true,
        "similarity": "cosine",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "HNSW"
        }
      }
    }
  }
}

// 高效能 HNSW 配置
PUT /hnsw_performance
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 1024,
        "index": true,
        "similarity": "dot_product",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "HNSW",
          "m": 48,
          "ef_construction": 500,
          "thread_count": 8,
          "linear_build_threshold": 1000,
          "is_embedding_saved": true,
          "embedding_load_strategy": "ANN_INDEX_FILE",
          "index_load_strategy": "MEM"
        }
      }
    }
  }
}

RabitQGraph

RabitQGraph Builder

參數名

類型

預設值

說明

param.rabitQGraph.builder.neighbor_cnt

uint32

128

每個節點的鄰居數量,影響圖的連通性和搜尋精度

param.rabitQGraph.builder.ef_construction

uint32

512

構建時的 EF 參數,控制構建過程中的候選節點數量

param.rabitQGraph.builder.prune_ratio

float

0.5

鄰居剪枝比例,用於最佳化圖結構

param.rabitQGraph.builder.cluster_count

uint32

64

聚類中心數量,用於向量量化

param.rabitQGraph.builder.quantized_bit_count

uint32

1

量化的位元數,只能設定為 1, 4, 5, 8, 9

param.rabitQGraph.builder.slack_prune_factor

float

1.0

鬆弛剪枝因子,用於控制剪枝策略

param.rabitQGraph.builder.repair_connectivity

bool

true

是否修複圖連通性

param.rabitQGraph.builder.thread_count

uint32

0

構建時使用的線程數,設定為0時為CPU核心數

param.rabitQGraph.builder.ckpt_count

uint32

0

檢查點數量,用於增量構建

param.rabitQGraph.builder.ckpt_threshold

uint32

2000000

檢查點閾值

RabitQGraph Searcher

參數名

類型

預設值

說明

param.rabitQGraph.searcher.ef

uint32

250

搜尋時的 EF 參數,影響搜尋精度和效能

param.rabitQGraph.searcher.max_scan_ratio

double

0.05

最大掃描比例,限制搜尋的節點百分比

param.rabitQGraph.searcher.check_crc_enable

bool

false

是否啟用 CRC 校正

param.rabitQGraph.searcher.thread_count

uint32

1

搜尋時使用的線程數

param.rabitQGraph.searcher.thread_safe_filter

bool

false

是否啟用安全執行緒過濾

RabitQGraph配置樣本

// 高效能配置
{
  "index_params": {
    "param.rabitQGraph.builder.neighbor_cnt": 256,
    "param.rabitQGraph.builder.ef_construction": 512,
    "param.rabitQGraph.builder.quantized_bit_count": 8,
    "param.rabitQGraph.builder.cluster_count": 128,
    "param.rabitQGraph.builder.thread_count": 8,
    "param.rabitQGraph.searcher.ef": 300,
    "param.rabitQGraph.searcher.max_scan_ratio": 0.1
  }
}

// 記憶體最佳化配置
{
  "index_params": {
    "param.rabitQGraph.builder.neighbor_cnt": 64,
    "param.rabitQGraph.builder.ef_construction": 200,
    "param.rabitQGraph.builder.quantized_bit_count": 1,
    "param.rabitQGraph.builder.cluster_count": 32,
    "param.rabitQGraph.searcher.ef": 150,
    "param.rabitQGraph.searcher.max_scan_ratio": 0.03
  }
}

// 平衡配置
{
  "index_params": {
    "param.rabitQGraph.builder.neighbor_cnt": 128,
    "param.rabitQGraph.builder.ef_construction": 400,
    "param.rabitQGraph.builder.quantized_bit_count": 4,
    "param.rabitQGraph.builder.cluster_count": 64,
    "param.rabitQGraph.searcher.ef": 250
  }
}

Linear

Linear 演算法使用線性暴力搜尋,參數相對簡單,使用 proxima.linear. 命名空間。

Linear Builder/Searcher

參數名

類型

預設值

說明

proxima.linear.builder.column_major_order

string

false

構建時特徵用行排(false)或列排(true)

proxima.linear.searcher.read_block_size

uint32

1048576

search階段一次性讀取到記憶體的大小,推薦值1M

Linear配置樣本

// 基礎配置
{
  "index_params": {
    "proxima.linear.builder.column_major_order": "false",
    "proxima.linear.searcher.read_block_size": 1048576
  }
}

// 大記憶體配置
{
  "index_params": {
    "proxima.linear.builder.column_major_order": "true",
    "proxima.linear.searcher.read_block_size": 2097152
  }
}

QC

QC (Quantization Clustering) 演算法使用量化聚類索引,參數使用 proxima.qc. 命名空間。

QC Builder

參數名

類型

預設值

說明

proxima.qc.builder.train_sample_count

uint32

0

指定訓練資料量,如果為0則使用全部資料

proxima.qc.builder.thread_count

uint32

0

構建時開啟線程數量,設定為0時為CPU核心數

proxima.qc.builder.centroid_count

string

-

聚類中心點參數,支援層次聚類,層之間用"*"分隔

proxima.qc.builder.cluster_class

string

OptKmeansCluster

指定聚類方法

proxima.qc.builder.cluster_auto_tuning

bool

false

指定是否開啟中心點數目自適應

proxima.qc.builder.optimizer_class

string

HcBuilder

針對中心點部分的最佳化器,用於提升分類時的精度

proxima.qc.builder.optimizer_params

IndexParams

-

optimize方法對應的構建和檢索參數

proxima.qc.builder.converter_class

string

-

如果Measure是InnerProduct,會自動進行Mips轉換操作

proxima.qc.builder.converter_params

IndexParams

-

converter_class 初始化參數

proxima.qc.builder.quantizer_class

string

-

配置量化器,可選 Int8QuantizerConverter, Int4QuantizerConverter 等

proxima.qc.builder.quantizer_params

IndexParams

-

量化器相關參數

proxima.qc.builder.quantize_by_centroid

bool

false

使用quantizer_class時,是否按中心點進行量化

proxima.qc.builder.store_original_features

bool

false

是否保留原始特徵

QC Searcher

參數名

類型

預設值

說明

proxima.qc.searcher.scan_ratio

float

0.01

用於計算max_scan_num數量,總doc數量 * scan_ratio

proxima.qc.searcher.optimizer_params

IndexParams

-

指定Build時optimizer對應的線上檢索參數

proxima.qc.searcher.brute_force_threshold

int

1000

如果總doc數少於此值,則走線性檢索

QC配置樣本

// 基礎配置
{
  "index_params": {
    "proxima.qc.builder.thread_count": 4,
    "proxima.qc.builder.centroid_count": "1000",
    "proxima.qc.builder.cluster_class": "OptKmeansCluster",
    "proxima.qc.searcher.scan_ratio": 0.02
  }
}

// 層次聚類配置
{
  "index_params": {
    "proxima.qc.builder.thread_count": 8,
    "proxima.qc.builder.centroid_count": "100*100",
    "proxima.qc.builder.optimizer_class": "HnswBuilder",
    "proxima.qc.builder.quantizer_class": "Int8QuantizerConverter",
    "proxima.qc.searcher.scan_ratio": 0.01
  }
}

// 高精度配置
{
  "index_params": {
    "proxima.qc.builder.thread_count": 12,
    "proxima.qc.builder.centroid_count": "2000",
    "proxima.qc.builder.train_sample_count": 100000,
    "proxima.qc.builder.store_original_features": true,
    "proxima.qc.searcher.scan_ratio": 0.05
  }
}

QGraph

QGraph (Quantized Graph) 演算法是量化圖索引,繼承了HNSW的大部分參數,並增加了量化相關參數。

QGraph Builder

QGraph繼承了所有 HNSW Builder 參數,並添加:

參數名

類型

預設值

說明

proxima.qgraph.builder.quantizer_class

string

-

配置量化器,可選 Int8QuantizerConverter, Int4QuantizerConverter, HalfFloatConverter, DoubleBitConverter

proxima.qgraph.builder.quantizer_params

IndexParams

-

配置量化器相關參數

所有 proxima.hnsw.builder.* 參數同樣適用於 QGraph。

QGraph Searcher

QGraph 繼承了所有 HNSW Searcher 參數。

QGraph配置樣本

// Int8 量化配置
{
  "index_params": {
    "proxima.hnsw.builder.max_neighbor_count": 32,
    "proxima.hnsw.builder.efconstruction": 400,
    "proxima.hnsw.builder.thread_count": 4,
    "proxima.qgraph.builder.quantizer_class": "Int8QuantizerConverter",
    "proxima.qgraph.builder.quantizer_params": {},
    "proxima.hnsw.searcher.ef": 300
  }
}

// Int4 量化配置(更省記憶體)
{
  "index_params": {
    "proxima.hnsw.builder.max_neighbor_count": 48,
    "proxima.hnsw.builder.efconstruction": 500,
    "proxima.hnsw.builder.thread_count": 6,
    "proxima.qgraph.builder.quantizer_class": "Int4QuantizerConverter",
    "proxima.qgraph.builder.quantizer_params": {},
    "proxima.hnsw.searcher.ef": 400,
    "proxima.hnsw.searcher.max_scan_ratio": 0.1
  }
}

// HalfFloat 量化配置(高精度)
{
  "index_params": {
    "proxima.hnsw.builder.max_neighbor_count": 64,
    "proxima.hnsw.builder.efconstruction": 600,
    "proxima.hnsw.builder.thread_count": 8,
    "proxima.qgraph.builder.quantizer_class": "HalfFloatConverter",
    "proxima.qgraph.builder.quantizer_params": {},
    "proxima.hnsw.searcher.ef": 500
  }
}

search_params 動態參數調整

在不重建索引的情況下,臨時調整某些搜尋參數,以在不同情境下平衡召回率和查詢延遲。

例如,對於普通線上查詢,使用預設或較低的 ef 值以保證低延遲;對於離線分析或高精度要求的任務,通過 search_params 臨時調高 ef 值以擷取更高的召回率。

HNSW

{
  "search_params": {
    "proxima.hnsw.searcher.ef": "400",
    "proxima.hnsw.searcher.max_scan_ratio": "0.15"
  }
}

QGraph

{
  "search_params": {
    "proxima.hnsw.searcher.ef": "400",
    "proxima.hnsw.searcher.max_scan_ratio": "0.15"
  }
}

QC

{
  "search_params": {
    "proxima.qc.searcher.scan_ratio": "0.02"
  }
}

RabitQGraph

{
  "search_params": {
    "param.rabitQGraph.searcher.ef": "300",
    "param.rabitQGraph.searcher.max_scan_ratio": "0.08"
  }
}

search_params配置樣本

// HNSW 動態調整精度和效能
GET vector_index/_search
{
  "knn": {
    "field": "vector",
    "query_vector": [0.1, 0.2, 0.3],
    "k": 10,
    "num_candidates": 100,
    "search_params": {
      "proxima.hnsw.searcher.ef": "500",
      "proxima.hnsw.searcher.max_scan_ratio": "0.2"
    }
  }
}

// QC 動態調整掃描比例
GET vector_index/_search
{
  "knn": {
    "field": "vector",
    "query_vector": [0.1, 0.2, 0.3],
    "k": 10,
    "num_candidates": 100,
    "search_params": {
      "proxima.qc.searcher.scan_ratio": "0.05"
    }
  }
}

// RabitQGraph 動態調整搜尋精度
GET vector_index/_search
{
  "knn": {
    "field": "vector",
    "query_vector": [0.1, 0.2, 0.3],
    "k": 10,
    "num_candidates": 100,
    "search_params": {
      "param.rabitQGraph.searcher.ef": "400",
      "param.rabitQGraph.searcher.max_scan_ratio": "0.1"
    }
  }
}

linear_build_threshold

類型為integer,屬於非必須參數,預設值為0。當文檔數量小於此閾值時,使用線性搜尋而不構建複雜索引

// 禁用線性閾值
{
  "linear_build_threshold": 0
}

// 小資料集使用線性搜尋
{
  "linear_build_threshold": 1000
}

// 較大閾值,適用於測試環境
{
  "linear_build_threshold": 5000
}

附錄:完整樣本

HNSW

// 基礎 HNSW 配置
PUT /hnsw_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 768,
        "index": true,
        "similarity": "cosine",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "HNSW"
        }
      }
    }
  }
}

// 高效能 HNSW 配置
PUT /hnsw_performance
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 1024,
        "index": true,
        "similarity": "dot_product",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "HNSW",
          "m": 48,
          "ef_construction": 500,
          "thread_count": 8,
          "linear_build_threshold": 1000,
          "is_embedding_saved": true,
          "embedding_load_strategy": "ANN_INDEX_FILE",
          "index_load_strategy": "MEM"
        }
      }
    }
  }
}

Linear

// 基礎 Linear 配置
PUT /linear_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 384,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "Linear"
        }
      }
    }
  }
}

QC

// 基礎 QC 配置
PUT /qc_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 768,
        "index": true,
        "similarity": "cosine",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "QC"
        }
      }
    }
  }
}

// 自訂 QC 配置
PUT /qc_custom
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 1024,
        "index": true,
        "similarity": "dot_product",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "QC",
          "thread_count": 8,
          "linear_build_threshold": 5000,
          "index_params": "{\"proxima.qc.builder.thread_count\": 8, \"proxima.qc.builder.centroid_count\": \"2000\"}"
        }
      }
    }
  }
}

QGraph

// 基礎 QGraph 配置
PUT /qgraph_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 768,
        "index": true,
        "similarity": "cosine",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "QGraph",
          "quantizer": "int8"
        }
      }
    }
  }
}

// 高精度 QGraph 配置
PUT /qgraph_high_precision
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 1536,
        "index": true,
        "similarity": "cosine",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "QGraph",
          "m": 40,
          "ef_construction": 600,
          "thread_count": 8,
          "quantizer": "fp16",
          "is_embedding_saved": true,
          "index_load_strategy": "MEM"
        }
      }
    }
  }
}

// 記憶體最佳化 QGraph 配置
PUT /qgraph_memory_optimized
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 1024,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "QGraph",
          "m": 24,
          "ef_construction": 300,
          "thread_count": 6,
          "quantizer": "int4",
          "is_embedding_saved": false,
          "index_load_strategy": "BUFFER"
        }
      }
    }
  }
}

RabitQGraph

// 基礎 RabitQGraph 配置(僅支援 l2_norm 相似性)
PUT /rabitqgraph_basic
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 64,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "RabitQGraph"
        }
      }
    }
  }
}

// 高效能 RabitQGraph 配置 - 使用 Map 格式 index_params
PUT /rabitqgraph_performance
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 128,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "RabitQGraph",
          "thread_count": 8,
          "index_params": {
            "param.rabitQGraph.builder.neighbor_cnt": 256,
            "param.rabitQGraph.builder.ef_construction": 512,
            "param.rabitQGraph.builder.quantized_bit_count": 4,
            "param.rabitQGraph.builder.cluster_count": 128,
            "param.rabitQGraph.searcher.ef": 300
          }
        }
      }
    }
  }
}

// 記憶體最佳化 RabitQGraph 配置
PUT /rabitqgraph_memory_optimized
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 192,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "RabitQGraph",
          "thread_count": 4,
          "linear_build_threshold": 1000,
          "index_params": {
            "param.rabitQGraph.builder.neighbor_cnt": 64,
            "param.rabitQGraph.builder.ef_construction": 200,
            "param.rabitQGraph.builder.quantized_bit_count": 1,
            "param.rabitQGraph.builder.cluster_count": 32,
            "param.rabitQGraph.searcher.ef": 150,
            "param.rabitQGraph.searcher.max_scan_ratio": 0.05
          }
        }
      }
    }
  }
}

// RabitQGraph 帶標籤過濾配置
PUT /rabitqgraph_with_tags
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 256,
        "index": true,
        "similarity": "l2_norm",
        "index_options": {
          "type": "havenask_native",
          "knn_type": "RabitQGraph",
          "tags": ["category", "region"],
          "index_params": {
            "param.rabitQGraph.builder.neighbor_cnt": 128,
            "param.rabitQGraph.builder.ef_construction": 400,
            "param.rabitQGraph.builder.quantized_bit_count": 8,
            "param.rabitQGraph.searcher.ef": 250
          }
        }
      },
      "category": {
        "type": "keyword"
      },
      "region": {
        "type": "keyword"
      }
    }
  }
}