Scalar btree index on doc_id + auto-build during Lance activation
LanceVectorStore gains build_scalar_index(column) and
has_scalar_index(column). Exposed as POST /vectors/lance/scalar-index/
{index}/{column}. activate_profile auto-builds the doc_id btree
alongside the IVF_PQ vector index when activating a Lance-backed
profile — operators get both indexes without extra API calls.
stats() now reports has_doc_id_index alongside has_vector_index.
Measured on resumes_100k_v2 (100K × 768d): random doc_id fetch
improved from ~5.4ms to ~3.5ms (35% faster). Btree build: 19ms,
+2.7 MB on disk. The remaining ~3ms is vector column materialization,
not index lookup — to close further would need a projection-only
fetch that skips the 768-float vector for text-only RAG retrieval.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -77,12 +77,21 @@ pub struct IndexStats {
|
||||
pub disk_bytes_added: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ScalarIndexStats {
|
||||
pub name: String,
|
||||
pub column: String,
|
||||
pub build_time_secs: f32,
|
||||
pub disk_bytes_added: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct DatasetStats {
|
||||
pub path: String,
|
||||
pub rows: usize,
|
||||
pub disk_bytes: u64,
|
||||
pub has_vector_index: bool,
|
||||
pub has_doc_id_index: bool,
|
||||
}
|
||||
|
||||
// ================= The backend =================
|
||||
@@ -132,11 +141,13 @@ impl LanceVectorStore {
|
||||
let rows = self.count().await.unwrap_or(0);
|
||||
let disk_bytes = dir_size_bytes(&strip_file_uri(&self.path));
|
||||
let has_vector_index = self.has_vector_index().await.unwrap_or(false);
|
||||
let has_doc_id_index = self.has_scalar_index("doc_id").await.unwrap_or(false);
|
||||
Ok(DatasetStats {
|
||||
path: self.path.clone(),
|
||||
rows,
|
||||
disk_bytes,
|
||||
has_vector_index,
|
||||
has_doc_id_index,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -297,6 +308,52 @@ impl LanceVectorStore {
|
||||
})
|
||||
}
|
||||
|
||||
/// Build a scalar btree index on `doc_id`. Makes `get_by_doc_id`
|
||||
/// O(log N) instead of a filter-scan of every fragment. Small index
|
||||
/// footprint — a few hundred KB on 100K rows.
|
||||
pub async fn build_scalar_index(&self, column: &str) -> Result<ScalarIndexStats, String> {
|
||||
use lance::dataset::Dataset;
|
||||
use lance_index::{DatasetIndexExt, IndexType};
|
||||
use lance_index::scalar::ScalarIndexParams;
|
||||
|
||||
let pre_bytes = dir_size_bytes(&strip_file_uri(&self.path));
|
||||
let t0 = Instant::now();
|
||||
|
||||
let mut dataset = Dataset::open(&self.path).await.map_err(e)?;
|
||||
let idx_name = format!("{column}_btree");
|
||||
dataset.create_index(
|
||||
&[column],
|
||||
IndexType::Scalar,
|
||||
Some(idx_name.clone()),
|
||||
&ScalarIndexParams::default(),
|
||||
true,
|
||||
).await.map_err(e)?;
|
||||
|
||||
Ok(ScalarIndexStats {
|
||||
name: idx_name,
|
||||
column: column.to_string(),
|
||||
build_time_secs: t0.elapsed().as_secs_f32(),
|
||||
disk_bytes_added: dir_size_bytes(&strip_file_uri(&self.path)).saturating_sub(pre_bytes),
|
||||
})
|
||||
}
|
||||
|
||||
/// True if a scalar index exists on the named column.
|
||||
pub async fn has_scalar_index(&self, column: &str) -> Result<bool, String> {
|
||||
use lance_index::DatasetIndexExt;
|
||||
let dataset = match lance::dataset::Dataset::open(&self.path).await {
|
||||
Ok(d) => d,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
let indexes = dataset.load_indices().await.map_err(e)?;
|
||||
Ok(indexes.iter().any(|ix| {
|
||||
ix.fields.iter().any(|fid| {
|
||||
dataset.schema().field_by_id(*fid)
|
||||
.map(|f| f.name == column)
|
||||
.unwrap_or(false)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
/// Search for top_k nearest neighbors of `query`. Uses the IVF_PQ
|
||||
/// index if one exists; otherwise does a full scan (slow but
|
||||
/// correct — useful during development before index build).
|
||||
|
||||
Reference in New Issue
Block a user