Hyperspace Docs
Hyperspace Homepage
  • Getting started
    • Overview
      • Hyperspace Advantages
      • Hyperspace Search
    • Quick Start
  • flows
    • Setting Up
      • Installing the Hyperspace API Client
      • Connecting to the Hyperspace Server
      • Creating a Database Schema Configuration File
        • Vector Similarity Metrics
        • Index Type Methods
      • Creating a Collection
      • Uploading Data to a Collection
      • Building and Running Queries
        • Building a Lexical Search Query
        • Building a Vector Search Query
        • Building a Hybrid Search Query
      • Retrieving Results
    • Data Collections
      • Uploading Data
      • Accessing Data
      • Supported Data Types
    • Queries
      • DSL Query interface
        • Aggregations
        • Bool Query
        • Candidate Generation and Metadata Filtering
        • Scoring and Ranking
  • Reference
    • Hyperspace Query Flow
    • Features and Benefits
    • Search Processing Unit (SPU)
    • Hyperspace Document Prototype
  • API Documentation
    • Hyperspace Client
      • add_batch
      • add_document
      • async_req
      • clear_collection
      • collections_info
      • commit
      • create_collection
      • delete_collection
      • delete_by_query
      • dsl_search
      • get_schema
      • get_document
      • reset_password
      • search
      • update_by_query
      • update_document
    • DSL Query Framework
      • Aggregations
        • Cardinality Aggregation
        • Date Histogram
        • Metric Aggregations
        • Terms Aggregation
      • Bool Queries
        • Free Text Search
        • 'match' Clause
        • 'filter' Clause
        • 'must' Clause
        • 'must_not' Clause
        • 'should' Clause
        • 'should_not' Clause
      • Candidate Generation and Metadata Filtering
        • Geo Coordinates Match
        • Range Match
        • Term Match
      • Scoring and Ranking
        • Boost
        • 'dis_max'
        • Function Score
        • Rarity Score (TF-IDF)
  • Releases
    • 2024 Releases
Powered by GitBook
On this page
  • Defining the Vector Query Schema
  • Running the Vector Query
  1. flows
  2. Setting Up
  3. Building and Running Queries

Building a Vector Search Query

PreviousBuilding a Lexical Search QueryNextBuilding a Hybrid Search Query

Last updated 11 months ago

The following describes how to build and run a Vector Search query. This type of search matches the similarity of documents in a Collection to a document that you provide by measuring the proximity (or similarity) between vectors rather than relying on traditional keyword matching or exact matches. This Vector search assigns a numerical score to each document according to the type of vector scoring method that you specified in the metric field, such as Hamming. This provides the identifiers of the documents with the top highest scores for retrieval. A multiplier (weight) option can be assigned to the score values.

Note – While uploading data into a Collection, 'dense_vector' value assigned to the 'type' attribute signifies that the data is suitable for a Vector Search. Therefore, only data that has been uploaded in this manner will be matched in a Vector search, as described in .

Defining the Vector Query Schema

Define the Vector Search query schema by specifying the following –

vector_query_schema = {paramsument,
                         'knn': {
                         'vector_field_name_1': {'boost': 0.5},
                         'vector_field_name_2': {'boost': 0.5}      
                         }                   
                       }
JsonObject params = new JsonObject();
params.add("vector_field_name_1", new Gson().toJsonTree(vector1).getAsJsonArray());
params.add("vector_field_name_2", new Gson().toJsonTree(vector).getAsJsonArray());

JsonObject knn_vector = new JsonObject();
knn_vector.add("boost", new JsonPrimitive(0.5));
            
JsonObject knn_query = new JsonObject();
knn_query.add("boost", new JsonPrimitive(0.5));
            
JsonObject knn = new JsonObject();
knn.add("query", knn_query);
knn.add("vector", knn_vector);
            
JsonObject hybrid_search = new JsonObject();
vector_search.add("params", params);
vector_search.add("knn", knn);
let vectorQuerySchema= {params,
                         'knn': {
                                'vector_field_name_1': {'boost': 0.5},
                                'vector_field_name_2': {'boost': 0.5}      
                          }                   
                         };

Where –

  • 'document'– specifies the document for which the query is searching for a match.

  • 'vector_field_name_1'– the name of the first vector field to be given a weight

  • 'vector_field_name_2'– the name of the second vector field to be given a weight

  • 'boost' - the key states the value of the given weight per score type, which is 0.5 in the example. Default value is 1.0.

In the above example, the query will perform two KNN calculations and return the weighted sum of the score,

score = 0.5 * score(vector_field_name_1) + 0.5 * score(vector_field_name_2)

Multi vector query can be integrated with lexical search to create a multi hybrid query.

Running the Vector Query

Copy the following code snippet to run the Vector Search query –

results = hyperspace_client.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name)
Object results = hyperspaceClient.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name);
const results = hyperspaceClient.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name);

Where –

  • vector_query_schema – Specifies the document for similarity search and the multiplier of the return score, as described above, defining the Vector Query Schema.

  • size – Specifies the quantity of results to retrieve.

  • collection_name – Specifies the Collection in which to search.

Using multiple vectors in the same query requires all of the included vectors to be indexed in the same manner, for example HNSW for all fields in the

Creating a Database Schema Configuration File
database schema configuration file.