Returns the most similar indexed documents to the query text.
Uses cosine similarity.
similarity_search(
self,
query: str,
k: int = 4,
filter: Optional[dict] = None,
search_strategy: SearchStrategy = SearchStrategy.VECTOR_ONLY,
filter_threshold: float = 0,
text_weight: float = 0.5,
vector_weight: float = 0.5,
vector_select_count_multiplier: int = 10,
**kwargs: Any = {}
) -> List[Document]Examples:
Basic Usage: .. code-block:: python
from langchain_community.vectorstores import SingleStoreDB
from langchain_openai import OpenAIEmbeddings
s2 = SingleStoreDB.from_documents(
docs,
OpenAIEmbeddings(),
host="username:password@localhost:3306/database"
)
results = s2.similarity_search("query text", 1,
{"metadata_field": "metadata_value"})
Different Search Strategies: .. code-block:: python
from langchain_community.vectorstores import SingleStoreDB
from langchain_openai import OpenAIEmbeddings
s2 = SingleStoreDB.from_documents(
docs,
OpenAIEmbeddings(),
host="username:password@localhost:3306/database",
use_full_text_search=True,
use_vector_index=True,
)
results = s2.similarity_search("query text", 1,
search_strategy=SingleStoreDB.SearchStrategy.FILTER_BY_TEXT,
filter_threshold=0.5)
Weighted Sum Search Strategy: .. code-block:: python
from langchain_community.vectorstores import SingleStoreDB
from langchain_openai import OpenAIEmbeddings
s2 = SingleStoreDB.from_documents(
docs,
OpenAIEmbeddings(),
host="username:password@localhost:3306/database",
use_full_text_search=True,
use_vector_index=True,
)
results = s2.similarity_search("query text", 1,
search_strategy=SingleStoreDB.SearchStrategy.WEIGHTED_SUM,
text_weight=0.3,
vector_weight=0.7)
| Name | Type | Description |
|---|---|---|
query* | str | The query text for which to find similar documents. |
k | int | Default: 4The number of documents to return. Default is 4. |
filter | dict | Default: NoneA dictionary of metadata fields and values to filter by. Default is None. |
search_strategy | SearchStrategy | Default: SearchStrategy.VECTOR_ONLYThe search strategy to use. Default is SearchStrategy.VECTOR_ONLY. Available options are:
|
filter_threshold | float | Default: 0The threshold for filtering by text or vector similarity. Default is 0. This option has effect only if search_strategy is SearchStrategy.FILTER_BY_TEXT or SearchStrategy.FILTER_BY_VECTOR. |
text_weight | float | Default: 0.5The weight of text similarity in the weighted sum search strategy. Default is 0.5. This option has effect only if search_strategy is SearchStrategy.WEIGHTED_SUM. |
vector_weight | float | Default: 0.5The weight of vector similarity in the weighted sum search strategy. Default is 0.5. This option has effect only if search_strategy is SearchStrategy.WEIGHTED_SUM. |
vector_select_count_multiplier | int | Default: 10The multiplier for the number of vectors to select when using the vector index. Default is 10. This parameter has effect only if use_vector_index is True and search_strategy is SearchStrategy.WEIGHTED_SUM or SearchStrategy.FILTER_BY_TEXT. The number of vectors selected will be k * vector_select_count_multiplier. This is needed due to the limitations of the vector index. |