mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-16 16:52:34 +00:00
This PR is part of the documentation revamp. Once the full set of PRs is merged, we’ll publish the combined site to staging for review before updating docs.lancedb.com. Ingestion, search and integration examples are incomplete or inconsistent across languages, and some guidance misstates SDK behavior. This brings the guides and their example sources together. It includes complete Python, TypeScript and Rust workflows, isolated provider and reranker examples, the Superlinked corrections, and guidance on retries that can duplicate writes. Stacked on #4491. Each MDX file has one PR owner. Rebase onto main after #4491 is squash-merged. Local assembly, Mint validation and link checks pass. Example code is unchanged from the previously validated package. Publication stays held through integration.
229 lines
6.6 KiB
Python
229 lines
6.6 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
|
|
def test_vector_search():
|
|
# --8<-- [start:vector_search]
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import lancedb
|
|
|
|
with TemporaryDirectory(prefix="lancedb-search-") as uri:
|
|
db = lancedb.connect(uri)
|
|
table = db.create_table(
|
|
"documents",
|
|
[
|
|
{
|
|
"id": 1,
|
|
"text": "A puppy playing in the park",
|
|
"category": "animals",
|
|
"vector": [1.0, 0.0],
|
|
},
|
|
{
|
|
"id": 2,
|
|
"text": "A dog resting at home",
|
|
"category": "animals",
|
|
"vector": [0.9, 0.1],
|
|
},
|
|
{
|
|
"id": 3,
|
|
"text": "A guide to growing tomatoes",
|
|
"category": "gardening",
|
|
"vector": [0.0, 1.0],
|
|
},
|
|
],
|
|
)
|
|
rows = (
|
|
table.search([1.0, 0.0])
|
|
.distance_type("l2")
|
|
.select(["id", "text", "_distance"])
|
|
.limit(2)
|
|
.to_list()
|
|
)
|
|
print(rows)
|
|
# --8<-- [end:vector_search]
|
|
|
|
assert [row["id"] for row in rows] == [1, 2]
|
|
assert rows[0]["_distance"] == 0.0
|
|
|
|
|
|
def test_text_search():
|
|
# --8<-- [start:text_search]
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import lancedb
|
|
from lancedb.index import FTS
|
|
|
|
with TemporaryDirectory(prefix="lancedb-search-") as uri:
|
|
db = lancedb.connect(uri)
|
|
table = db.create_table(
|
|
"documents",
|
|
[
|
|
{
|
|
"id": 1,
|
|
"text": "A puppy playing in the park",
|
|
"category": "animals",
|
|
},
|
|
{
|
|
"id": 2,
|
|
"text": "A dog resting at home",
|
|
"category": "animals",
|
|
},
|
|
{
|
|
"id": 3,
|
|
"text": "A guide to growing tomatoes",
|
|
"category": "gardening",
|
|
},
|
|
],
|
|
)
|
|
table.create_index("text", config=FTS())
|
|
rows = (
|
|
table.search("puppy", query_type="fts", fts_columns="text")
|
|
.select(["id", "text", "_score"])
|
|
.limit(2)
|
|
.to_list()
|
|
)
|
|
print(rows)
|
|
# --8<-- [end:text_search]
|
|
|
|
assert [row["id"] for row in rows] == [1]
|
|
assert rows[0]["_score"] > 0
|
|
|
|
|
|
def test_hybrid_search():
|
|
# --8<-- [start:hybrid_search]
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import lancedb
|
|
from lancedb.index import FTS
|
|
|
|
with TemporaryDirectory(prefix="lancedb-search-") as uri:
|
|
db = lancedb.connect(uri)
|
|
table = db.create_table(
|
|
"documents",
|
|
[
|
|
{
|
|
"id": 1,
|
|
"text": "A puppy playing in the park",
|
|
"category": "animals",
|
|
"vector": [1.0, 0.0],
|
|
},
|
|
{
|
|
"id": 2,
|
|
"text": "A dog resting at home",
|
|
"category": "animals",
|
|
"vector": [0.9, 0.1],
|
|
},
|
|
{
|
|
"id": 3,
|
|
"text": "A guide to growing tomatoes",
|
|
"category": "gardening",
|
|
"vector": [0.0, 1.0],
|
|
},
|
|
],
|
|
)
|
|
from lancedb.rerankers import RRFReranker
|
|
|
|
table.create_index("text", config=FTS())
|
|
rows = (
|
|
table.search(
|
|
query_type="hybrid", vector_column_name="vector", fts_columns="text"
|
|
)
|
|
.vector([1.0, 0.0])
|
|
.text("puppy")
|
|
.rerank(RRFReranker())
|
|
.limit(2)
|
|
.to_list()
|
|
)
|
|
print(rows)
|
|
# --8<-- [end:hybrid_search]
|
|
|
|
assert [row["id"] for row in rows] == [1, 2]
|
|
assert rows[0]["_relevance_score"] > rows[1]["_relevance_score"]
|
|
|
|
|
|
def test_filter_search():
|
|
# --8<-- [start:filter_search]
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import lancedb
|
|
|
|
with TemporaryDirectory(prefix="lancedb-search-") as uri:
|
|
db = lancedb.connect(uri)
|
|
table = db.create_table(
|
|
"documents",
|
|
[
|
|
{
|
|
"id": 1,
|
|
"text": "A puppy playing in the park",
|
|
"category": "animals",
|
|
"vector": [1.0, 0.0],
|
|
},
|
|
{
|
|
"id": 2,
|
|
"text": "A dog resting at home",
|
|
"category": "animals",
|
|
"vector": [0.9, 0.1],
|
|
},
|
|
{
|
|
"id": 3,
|
|
"text": "A guide to growing tomatoes",
|
|
"category": "gardening",
|
|
"vector": [0.0, 1.0],
|
|
},
|
|
],
|
|
)
|
|
rows = (
|
|
table.search([0.0, 1.0])
|
|
.where("category = 'animals'", prefilter=True)
|
|
.select(["id", "text", "category", "_distance"])
|
|
.limit(2)
|
|
.to_list()
|
|
)
|
|
print(rows)
|
|
# --8<-- [end:filter_search]
|
|
|
|
assert {row["id"] for row in rows} == {1, 2}
|
|
assert all(row["category"] == "animals" for row in rows)
|
|
|
|
|
|
def test_query_plan():
|
|
# --8<-- [start:query_plan]
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import lancedb
|
|
|
|
with TemporaryDirectory(prefix="lancedb-search-") as uri:
|
|
db = lancedb.connect(uri)
|
|
table = db.create_table(
|
|
"documents",
|
|
[
|
|
{
|
|
"id": 1,
|
|
"text": "A puppy playing in the park",
|
|
"category": "animals",
|
|
"vector": [1.0, 0.0],
|
|
},
|
|
{
|
|
"id": 2,
|
|
"text": "A dog resting at home",
|
|
"category": "animals",
|
|
"vector": [0.9, 0.1],
|
|
},
|
|
{
|
|
"id": 3,
|
|
"text": "A guide to growing tomatoes",
|
|
"category": "gardening",
|
|
"vector": [0.0, 1.0],
|
|
},
|
|
],
|
|
)
|
|
query = table.search([1.0, 0.0]).select(["id", "text", "_distance"]).limit(2)
|
|
print("Plan:", query.explain_plan())
|
|
print("Measured execution:", query.analyze_plan())
|
|
rows = query.to_list()
|
|
print(rows)
|
|
# --8<-- [end:query_plan]
|
|
|
|
assert [row["id"] for row in rows] == [1, 2]
|