Class AggregateHybridQuery
Ported from Python: redisvl/query/aggregate.py:57-329 (AggregateHybridQuery 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 using the formula:
hybrid_score = (1 - alpha) * text_score + alpha * vector_similarity
Where text_score is the BM25 score from the text search and vector_similarity
is the normalized cosine similarity from the vector search.
Redis Version Requirements: This query uses the ADDSCORES option in FT.AGGREGATE to expose the internal text search score (@__score). This feature requires Redis 7.4.0 or later. On older Redis versions, the query will fail.
Note on Runtime Parameters: AggregateHybridQuery uses Redis FT.AGGREGATE for
aggregation-based hybrid search. As of Redis Stack 7.2+, runtime parameters (efRuntime, epsilon,
etc.) are NOT supported in FT.AGGREGATE queries. If you need runtime parameter support, use
VectorQuery or VectorRangeQuery instead.
For native FT.HYBRID support with built-in score fusion (RRF, LINEAR), use HybridQuery
instead. This requires Redis 8.4+.
Python equivalent:
query = AggregateHybridQuery(
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:
AggregateHybridQuery query = AggregateHybridQuery.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(FullTextQueryHelper.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 classBuilder for creating AggregateHybridQuery instances with fluent API. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Create a new builder for AggregateHybridQuery.Build the base query string for the aggregation.Build the Redis AggregationBuilder for this query.floatgetAlpha()intgetDtype()intGet the parameters for the aggregation query.getText()float[]loadDefaultStopwords(String language) Load default stopwords for a given language.
-
Method Details
-
builder
Create a new builder for AggregateHybridQuery.- Returns:
- A new AggregateHybridQueryBuilder instance
-
loadDefaultStopwords
Load default stopwords for a given language.- Parameters:
language- the language (e.g., "english", "german")- Returns:
- set of stopwords
-
getText
-
getTextFieldName
-
getVector
public float[] getVector() -
getVectorFieldName
-
getTextScorer
-
getFilterExpression
-
getAlpha
public float getAlpha() -
getDtype
-
getNumResults
public int getNumResults() -
getReturnFields
-
getStopwords
-
getDialect
public int getDialect() -
buildQueryString
Description copied from class:AggregationQueryBuild the base query string for the aggregation.- Specified by:
buildQueryStringin classAggregationQuery- Returns:
- the query string
-
buildRedisAggregation
Description copied from class:AggregationQueryBuild the Redis AggregationBuilder for this query.- Specified by:
buildRedisAggregationin classAggregationQuery- Returns:
- the Jedis AggregationBuilder configured for this query
-
getParams
Description copied from class:AggregationQueryGet the parameters for the aggregation query.Used for parameterized queries (e.g., vector parameter in HybridQuery).
- Specified by:
getParamsin classAggregationQuery- Returns:
- a map of parameter names to values
-