-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from lancedb/addFts
Add Full text search query and query with filter
- Loading branch information
Showing
3 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "lancedb-cloud-benchmarks" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
description = "" | ||
authors = [{ name = "LanceDB Devs", email = "[email protected]" }] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import Enum | ||
import random | ||
from typing import Any, List | ||
|
||
from lancedb.remote.table import RemoteTable | ||
import numpy as np | ||
|
||
QUERY_WORDS = [ | ||
# Common nouns | ||
"University", | ||
"Institute", | ||
"School", | ||
"Museum", | ||
"Library", | ||
"History", | ||
"Science", | ||
"Art", | ||
"Literature", | ||
"Philosophy", | ||
# Locations | ||
"America", | ||
"Europe", | ||
"Asia", | ||
"China", | ||
"India", | ||
"Japan", | ||
"Russia", | ||
"Germany", | ||
"France", | ||
"England", | ||
# Organizations | ||
"Company", | ||
"Corporation", | ||
"Association", | ||
"Society", | ||
"Foundation", | ||
# Fields | ||
"Technology", | ||
"Engineering", | ||
"Medicine", | ||
"Economics", | ||
"Politics", | ||
] | ||
|
||
|
||
class QueryType(Enum): | ||
VECTOR = "vector" | ||
VECTOR_WITH_FILTER = "vector_with_filter" | ||
FTS = "fts" | ||
HYBRID = "hybrid" | ||
|
||
|
||
class Query(ABC): | ||
"""Abstract base class for different query types""" | ||
|
||
@abstractmethod | ||
def query(self, table: RemoteTable, **kwargs) -> Any: | ||
"""Execute the query on the given table""" | ||
pass | ||
|
||
|
||
class VectorQuery(Query): | ||
def __init__( | ||
self, | ||
words: List[str] = None, | ||
dim: int = 1536, | ||
metric: str = "cosine", | ||
nprobes: int = 1, | ||
selected_columns: List[str] = None, | ||
limit: int = 1, | ||
filter: bool = False, | ||
): | ||
self.words = words or QUERY_WORDS | ||
self.dim = dim | ||
self.metric = metric | ||
self.nprobes = nprobes | ||
self.selected_columns = selected_columns or ["openai", "title"] | ||
self.limit = limit | ||
self.filter = filter | ||
|
||
def query(self, table: RemoteTable, **kwargs) -> Any: | ||
query = table.search(np.random.standard_normal(self.dim)) | ||
|
||
if self.filter: | ||
filter_text = random.choice(self.words) | ||
query = query.where(f"text LIKE '%{filter_text}%'", prefilter=True) | ||
|
||
return ( | ||
query.metric(self.metric) | ||
.nprobes(self.nprobes) | ||
.select(self.selected_columns) | ||
.limit(self.limit) | ||
.to_arrow() | ||
) | ||
|
||
|
||
class FTSQuery(Query): | ||
"""Simple full-text search implementation""" | ||
|
||
def __init__( | ||
self, | ||
words: List[str] = None, | ||
column: str = "title", | ||
selected_columns: List[str] = None, | ||
limit: int = 1, | ||
): | ||
self.words = words or QUERY_WORDS | ||
self.column = column | ||
self.selected_columns = selected_columns or ["title"] | ||
self.limit = limit | ||
|
||
def query(self, table: RemoteTable, **kwargs) -> Any: | ||
query_text = random.choice(self.words) | ||
return ( | ||
table.search(query_text, query_type="fts") | ||
.select(self.selected_columns) | ||
.limit(self.limit) | ||
.to_arrow() | ||
) |