Package com.redis.vl.query
Class HybridQuery
java.lang.Object
com.redis.vl.query.AggregationQuery
com.redis.vl.query.HybridQuery
HybridQuery combines text and vector search in Redis using aggregation.
Ported from Python: redisvl/query/aggregate.py:23-230 (HybridQuery class)
It allows you to perform a hybrid search using both text and vector similarity. It scores documents based on a weighted combination of text and vector similarity.
Python equivalent:
query = HybridQuery( text="example text", text_field_name="text_field", vector=[0.1, 0.2, 0.3], vector_field_name="vector_field", text_scorer="BM25STD", filter_expression=None, alpha=0.7, dtype="float32", num_results=10, return_fields=["field1", "field2"], stopwords="english", dialect=2, ) results = index.query(query)
Java equivalent:
HybridQuery query = HybridQuery.builder() .text("example text") .textFieldName("text_field") .vector(new float[]{0.1f, 0.2f, 0.3f}) .vectorFieldName("vector_field") .textScorer("BM25STD") .filterExpression(null) .alpha(0.7f) .dtype("float32") .numResults(10) .returnFields(List.of("field1", "field2")) .stopwords(loadDefaultStopwords("english")) .dialect(2) .build(); List<Map<String, Object>> results = index.query(query);
This class is final to prevent finalizer attacks, as it throws exceptions in constructors for input validation (SEI CERT OBJ11-J).
- Since:
- 0.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder for creating HybridQuery instances with fluent API. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Create a new builder for HybridQuery.Build the full query string for text search with optional filtering.Build the Redis AggregationBuilder for this hybrid query.float
getAlpha()
Get the alpha weighting factor.int
Get the query dialect version.getDtype()
Get the data type for vector storage.Get the filter expression.int
Get the maximum number of results.Get the parameters for the aggregation query.Get an unmodifiable view of the return fields.Get an unmodifiable view of the stopwords.getText()
Get the text query string.Get the text field name.Get the text scoring algorithm.float[]
Get a copy of the query vector.Get the vector field name.loadDefaultStopwords
(String language) Load default stopwords for a given language.tokenizeAndEscapeQuery
(String userQuery) Tokenize and escape the user query.
-
Method Details
-
builder
Create a new builder for HybridQuery.- Returns:
- A new HybridQueryBuilder instance
-
getText
Get the text query string.- Returns:
- The text to search for
-
getTextFieldName
Get the text field name.- Returns:
- The field name containing text data
-
getVector
public float[] getVector()Get a copy of the query vector.- Returns:
- A defensive copy of the vector array
-
getVectorFieldName
Get the vector field name.- Returns:
- The field name containing vector data
-
getTextScorer
Get the text scoring algorithm.- Returns:
- The text scorer (e.g., "BM25", "TFIDF")
-
getFilterExpression
Get the filter expression.- Returns:
- The filter to apply, or null if no filter
-
getAlpha
public float getAlpha()Get the alpha weighting factor.- Returns:
- Weight between 0.0 (vector only) and 1.0 (text only)
-
getDtype
Get the data type for vector storage.- Returns:
- The data type (e.g., "float32", "float64")
-
getNumResults
public int getNumResults()Get the maximum number of results.- Returns:
- The result limit
-
getReturnFields
Get an unmodifiable view of the return fields.- Returns:
- Unmodifiable list of return fields
-
getStopwords
Get an unmodifiable view of the stopwords.- Returns:
- Unmodifiable set of stopwords
-
getDialect
public int getDialect()Get the query dialect version.- Returns:
- The dialect version (default 2)
-
loadDefaultStopwords
Load default stopwords for a given language.Python uses nltk, we'll use a simple file-based approach.
- Parameters:
language
- the language (e.g., "english", "german")- Returns:
- set of stopwords
-
tokenizeAndEscapeQuery
Tokenize and escape the user query.Ported from Python: _tokenize_and_escape_query (line 185-209)
- Parameters:
userQuery
- the user query to tokenize- Returns:
- tokenized and escaped query string joined by OR
-
buildQueryString
Build the full query string for text search with optional filtering.Ported from Python: _build_query_string (line 211-225)
- Specified by:
buildQueryString
in classAggregationQuery
- Returns:
- the query string
-
buildRedisAggregation
Build the Redis AggregationBuilder for this hybrid query.Ported from Python __init__ method (line 103-129)
- Specified by:
buildRedisAggregation
in classAggregationQuery
- Returns:
- the AggregationBuilder
-
getParams
Get the parameters for the aggregation query.Ported from Python: params property (line 132-145)
- Specified by:
getParams
in classAggregationQuery
- Returns:
- parameter map with vector
-