Teradata Package for LangChain Function Reference - similarity_search - Teradata® Package for LangChain - Look here for syntax, methods and examples for the functions included in the Teradata langchain-teradata package.

Teradata® Package for LangChain Function Reference

Deployment
VantageCloud
Edition
Enterprise
Product
Teradata® Package for LangChain
Release Number
20.00.00.01
Published
December 2025
ft:locale
en-US
ft:lastEdition
2025-12-19
dita:id
Langchain-Teradata_FxRef_Lake
Product Category
Teradata Vantage
libs.teradata.langchain_teradata.TeradataVectorStore.similarity_search = similarity_search(self, question=None, **kwargs)
DESCRIPTION:
    Performs similarity search in the Vector Store for the input question.
    The algorithm specified in "search_algorithm" is used to perform
    the search against the vector store.
    The result contains "top_k" rows along with similarity score
    found by the "search_algorithm".
 
PARAMETERS:
    question:
        Optional Argument.
        Specifies a string of text for which similarity search
        needs to be performed.
        Types: str
 
    column:
        Optional Argument.
        Specifies the column name which contains the
        question in text format.
        Types: str
 
    data:
        Optional Argument.
        Specifies the table name/DataFrame which contains
        the question in text format.
        Types: str or DataFrame
 
    batch_query_column:
        Required for batch mode.
        Specifies the query column to be indexed for batch mode.
        Note:
            Applicable only for AWS.
        Types: str
 
    top_k:
        Optional Argument.
        Specifies the number of similarity matches to be generated.
        Default Value: 10
        Permitted Values: [1 - 1024]
        Types: int
 
    search_threshold:
        Optional Argument.
        Threshold value to consider matching tables/views while searching.
        A higher threshold value limits responses to the top matches only.
        Note:
            Only applicable when "search_algorithm" is 'VECTOR_DISTANCE' AND 'KMEANS'.
        Types: float
 
    search_numcluster:
        Optional Argument.
        Number of clusters or fraction of train_numcluster to be considered while searching.
        Notes:
            Applicable when "search_algorithm" is 'KMEANS'.
            If you want to pass a fraction of train_numcluster to be used for searching,
            the supported range is (0, 1.0].
            If you want to pass the exact number of clusters to be used for searching,
            the supported range is [1, train_numcluster].
        Types: int or float
 
    ef_search:
        Optional Argument.
        Specify the number of neighbors to consider during search in HNSW graph.
        Note:
            Applicable when "search_algorithm" is 'HNSW'.
        Permitted Values: [1 - 1024]
        Types: int
 
    filter:
        Optional Argument.
        Specifies the filter expression to be used for filtering the results.
        Supports logical operators (AND, OR, NOT), comparison operators
        (=, !=, <, <=, >, >=), and IN clauses with parentheses grouping.
        Notes:
            Examples:
            * Simple condition: "age > 25"
            * Complex condition: "age >= 18 AND status = 'active'"
            * IN clause: "category IN ('A', 'B', 'C')"
            * Grouped conditions: "(age > 18 AND status = 'active') OR priority = 'high'"
            * String matching: "name != 'test' AND description LIKE '%important%'"
        Types: str
 
    filter_style:
        Optional Argument.
        Specifies whether to apply filtering before or after the similarity_search.
        Default Value: PRE-FILTERING
        Permitted Values: PRE-FILTERING, POST-FILTERING
        Types: str
 
    maximal_marginal_relevance:
        Optional Argument.
        Specifies whether to use Maximal Marginal Relevance (MMR) for retrieving documents.
        Types: bool
 
    lambda_multiplier:
        Optional Argument.
        Lambda multiplier to control the trade-off between relevance and diversity when selecting documents.
        Permitted Values: 0.0 to 1.0
        Types: float
 
    batch_data:
        Required for batch mode.
        Specifies the table name or teradataml DataFrame to be indexed for batch mode.
        Note:
            Applicable only for AWS.
        Types: str, teradataml DataFrame
 
    batch_id_column:
        Required for batch mode.
        Specifies the ID column to be indexed for batch mode.
        Note:
            Applicable only for AWS.
        Types: str
 
    return_type:
        Optional Argument.
        Specifies the return type of similarity_search.
        Default Value: teradataml
        Permitted Values: teradataml, pandas, json
        Types: str
 
RETURNS:
    list.
 
RAISES:
    TeradataMlException.
 
EXAMPLES:
    >>> from langchain_teradata import TeradataVectorStore
    >>> from teradatagenai import load_data
 
    # Load data into the vector store.
    >>> load_data("amazon", "amazon_reviews_25")
 
    # Note this step is not needed if vector store already exists.
    >>> vs = TeradataVectorStore.from_datasets(name = "tdvs_example",
                                               data="amazon_reviews_25",
                                               key_columns=['rev_id', 'aid'],
                                               data_columns=['rev_text'],
                                               embedding = "amazon.titan-embed-text-v1",
                                               )
    # Example 1: Perform similarity search in the Vector Store for
    #            the input question.
    >>> question = 'Are there any reviews about books?'
    >>> response = vs.similarity_search(question=question)
 
    # Example 2: Perform similarity search with SQL string filter.
    >>> question = 'Which book are all the reviews talking about?'
    >>> response = vs.similarity_search(question=question, 
                                        top_k=5,
                                        filter="rev_name LIKE 'A%' and rev_name NOT LIKE 'Antiquarian'",
                                        return_type='json' 
                                        )
 
    # Example 3: Perform batch similarity search in the Vector Store.
    # Create an instance of the VectorStore class.
    # Note: This step is not needed if the vector store already exists.
    >>> vs = TeradataVectorStore.from_datasets(name="vs",
                                               data = "valid_passages",
                                               data_columns = "passage",
                                               key_columns = "pid",
                                               embedding = "amazon.titan-embed-text-v1",
                                               top_k=10,
                                               search_algorithm="HNSW",
                                               vector_column="VectorIndex")
 
    # Perform batch similarity search in the Vector Store.
    >>> response = vs.similarity_search(batch_data="valid_passages",
                                        batch_id_column="pid",
                                        batch_query_column="passage")
 
    # Retrieve the batch similarity results.
    from teradatagenai import VSApi
    >>> similarity_results = vs.get_batch_result(api_name=VSApi.SimilaritySearch)