|
1 | 1 | package com.redis.vl.query; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
| 3 | +import com.redis.vl.utils.TokenEscaper; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import lombok.Getter; |
5 | 7 |
|
6 | | -/** Full-text search query */ |
| 8 | +/** |
| 9 | + * Full-text search query with support for field weights. |
| 10 | + * |
| 11 | + * <p>Python port: Implements text_field_name with Union[str, Dict[str, float]] for weighted text |
| 12 | + * search across multiple fields. |
| 13 | + * |
| 14 | + * <p>Example usage: |
| 15 | + * |
| 16 | + * <pre>{@code |
| 17 | + * // Single field (backward compatible) |
| 18 | + * TextQuery query = TextQuery.builder() |
| 19 | + * .text("search terms") |
| 20 | + * .textField("description") |
| 21 | + * .build(); |
| 22 | + * |
| 23 | + * // Multiple fields with weights |
| 24 | + * TextQuery query = TextQuery.builder() |
| 25 | + * .text("search terms") |
| 26 | + * .textFieldWeights(Map.of("title", 5.0, "content", 2.0, "tags", 1.0)) |
| 27 | + * .build(); |
| 28 | + * }</pre> |
| 29 | + */ |
| 30 | +@Getter |
7 | 31 | public class TextQuery { |
8 | 32 |
|
9 | 33 | private final String text; |
10 | | - private final String textField; |
11 | 34 | private final String scorer; |
12 | 35 | private final Filter filterExpression; |
13 | | - private final List<String> returnFields; |
| 36 | + private final Integer numResults; |
14 | 37 |
|
15 | | - /** |
16 | | - * Create a text query without a filter expression. |
17 | | - * |
18 | | - * @param text The text to search for |
19 | | - * @param textField The field to search in |
20 | | - * @param scorer The scoring algorithm (e.g., "BM25", "TF IDF") |
21 | | - * @param returnFields List of fields to return in results |
22 | | - */ |
23 | | - public TextQuery(String text, String textField, String scorer, List<String> returnFields) { |
24 | | - this(text, textField, scorer, null, returnFields); |
25 | | - } |
| 38 | + /** Field names mapped to their search weights */ |
| 39 | + private Map<String, Double> fieldWeights; |
26 | 40 |
|
27 | | - /** |
28 | | - * Create a text query with all parameters. |
29 | | - * |
30 | | - * @param text The text to search for |
31 | | - * @param textField The field to search in |
32 | | - * @param scorer The scoring algorithm |
33 | | - * @param filterExpression Optional filter to apply |
34 | | - * @param returnFields List of fields to return in results |
35 | | - */ |
36 | | - public TextQuery( |
37 | | - String text, |
38 | | - String textField, |
39 | | - String scorer, |
40 | | - Filter filterExpression, |
41 | | - List<String> returnFields) { |
42 | | - this.text = text; |
43 | | - this.textField = textField; |
44 | | - this.scorer = scorer; |
45 | | - this.filterExpression = filterExpression; |
46 | | - this.returnFields = returnFields != null ? new ArrayList<>(returnFields) : null; |
| 41 | + private TextQuery(Builder builder) { |
| 42 | + this.text = builder.text; |
| 43 | + this.scorer = builder.scorer; |
| 44 | + this.filterExpression = builder.filterExpression; |
| 45 | + this.numResults = builder.numResults; |
| 46 | + this.fieldWeights = new HashMap<>(builder.fieldWeights); |
47 | 47 | } |
48 | 48 |
|
49 | 49 | /** |
50 | | - * Get the search text |
| 50 | + * Update the field weights dynamically. |
51 | 51 | * |
52 | | - * @return Search text |
| 52 | + * @param fieldWeights Map of field names to weights |
53 | 53 | */ |
54 | | - public String getText() { |
55 | | - return text; |
| 54 | + public void setFieldWeights(Map<String, Double> fieldWeights) { |
| 55 | + validateFieldWeights(fieldWeights); |
| 56 | + this.fieldWeights = new HashMap<>(fieldWeights); |
56 | 57 | } |
57 | 58 |
|
58 | 59 | /** |
59 | | - * Get the text field to search in |
| 60 | + * Get a copy of the field weights. |
60 | 61 | * |
61 | | - * @return Text field name |
| 62 | + * @return Map of field names to weights |
62 | 63 | */ |
63 | | - public String getTextField() { |
64 | | - return textField; |
| 64 | + public Map<String, Double> getFieldWeights() { |
| 65 | + return new HashMap<>(fieldWeights); |
65 | 66 | } |
66 | 67 |
|
67 | 68 | /** |
68 | | - * Get the scoring algorithm |
| 69 | + * Build the Redis query string for text search with weighted fields. |
| 70 | + * |
| 71 | + * <p>Format: |
69 | 72 | * |
70 | | - * @return Scorer name |
| 73 | + * <ul> |
| 74 | + * <li>Single field default weight: {@code @field:(term1 | term2)} |
| 75 | + * <li>Single field with weight: {@code @field:(term1 | term2) => { $weight: 5.0 }} |
| 76 | + * <li>Multiple fields: {@code (@field1:(terms) => { $weight: 3.0 } | @field2:(terms) => { |
| 77 | + * $weight: 2.0 })} |
| 78 | + * </ul> |
| 79 | + * |
| 80 | + * @return Redis query string |
71 | 81 | */ |
72 | | - public String getScorer() { |
73 | | - return scorer; |
| 82 | + public String toQueryString() { |
| 83 | + TokenEscaper escaper = new TokenEscaper(); |
| 84 | + |
| 85 | + // Tokenize and escape the query text |
| 86 | + String[] tokens = text.split("\\s+"); |
| 87 | + StringBuilder escapedQuery = new StringBuilder(); |
| 88 | + |
| 89 | + for (int i = 0; i < tokens.length; i++) { |
| 90 | + if (i > 0) { |
| 91 | + escapedQuery.append(" | "); |
| 92 | + } |
| 93 | + String cleanToken = |
| 94 | + tokens[i].strip().stripLeading().stripTrailing().replace(",", "").toLowerCase(); |
| 95 | + escapedQuery.append(escaper.escape(cleanToken)); |
| 96 | + } |
| 97 | + |
| 98 | + String escapedText = escapedQuery.toString(); |
| 99 | + |
| 100 | + // Build query parts for each field with its weight |
| 101 | + StringBuilder queryBuilder = new StringBuilder(); |
| 102 | + int fieldCount = 0; |
| 103 | + |
| 104 | + for (Map.Entry<String, Double> entry : fieldWeights.entrySet()) { |
| 105 | + String field = entry.getKey(); |
| 106 | + Double weight = entry.getValue(); |
| 107 | + |
| 108 | + if (fieldCount > 0) { |
| 109 | + queryBuilder.append(" | "); |
| 110 | + } |
| 111 | + |
| 112 | + queryBuilder.append("@").append(field).append(":(").append(escapedText).append(")"); |
| 113 | + |
| 114 | + // Add weight modifier if not default |
| 115 | + if (weight != 1.0) { |
| 116 | + queryBuilder.append(" => { $weight: ").append(weight).append(" }"); |
| 117 | + } |
| 118 | + |
| 119 | + fieldCount++; |
| 120 | + } |
| 121 | + |
| 122 | + // Wrap multiple fields in parentheses |
| 123 | + String textQuery; |
| 124 | + if (fieldWeights.size() > 1) { |
| 125 | + textQuery = "(" + queryBuilder.toString() + ")"; |
| 126 | + } else { |
| 127 | + textQuery = queryBuilder.toString(); |
| 128 | + } |
| 129 | + |
| 130 | + // Add filter expression if present |
| 131 | + if (filterExpression != null) { |
| 132 | + return textQuery + " AND " + filterExpression.build(); |
| 133 | + } |
| 134 | + |
| 135 | + return textQuery; |
74 | 136 | } |
75 | 137 |
|
76 | | - /** |
77 | | - * Get the filter expression |
78 | | - * |
79 | | - * @return Filter expression or null |
80 | | - */ |
81 | | - public Filter getFilterExpression() { |
82 | | - return filterExpression; |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + return toQueryString(); |
83 | 141 | } |
84 | 142 |
|
85 | 143 | /** |
86 | | - * Get the return fields |
| 144 | + * Create a new Builder for TextQuery. |
87 | 145 | * |
88 | | - * @return List of fields to return or null |
| 146 | + * @return Builder instance |
89 | 147 | */ |
90 | | - public List<String> getReturnFields() { |
91 | | - return returnFields != null ? new ArrayList<>(returnFields) : null; |
| 148 | + public static Builder builder() { |
| 149 | + return new Builder(); |
92 | 150 | } |
93 | 151 |
|
94 | | - /** |
95 | | - * Build the query string for Redis text search |
96 | | - * |
97 | | - * @return Query string |
98 | | - */ |
99 | | - public String toQueryString() { |
100 | | - StringBuilder query = new StringBuilder(); |
| 152 | + /** Builder for TextQuery with support for field weights. */ |
| 153 | + public static class Builder { |
| 154 | + private String text; |
| 155 | + private String scorer = "BM25STD"; |
| 156 | + private Filter filterExpression; |
| 157 | + private Integer numResults = 10; |
| 158 | + private Map<String, Double> fieldWeights = new HashMap<>(); |
101 | 159 |
|
102 | | - // Add filter expression if present |
103 | | - if (filterExpression != null) { |
104 | | - query.append(filterExpression.build()).append(" "); |
| 160 | + /** |
| 161 | + * Set the text to search for. |
| 162 | + * |
| 163 | + * @param text Search text |
| 164 | + * @return Builder |
| 165 | + */ |
| 166 | + public Builder text(String text) { |
| 167 | + this.text = text; |
| 168 | + return this; |
105 | 169 | } |
106 | 170 |
|
107 | | - // Add text search |
108 | | - if (textField != null && !textField.isEmpty()) { |
109 | | - query.append("@").append(textField).append(":(").append(text).append(")"); |
110 | | - } else { |
111 | | - // Search all text fields |
112 | | - query.append(text); |
| 171 | + /** |
| 172 | + * Set a single text field to search (backward compatible). |
| 173 | + * |
| 174 | + * @param fieldName Field name |
| 175 | + * @return Builder |
| 176 | + */ |
| 177 | + public Builder textField(String fieldName) { |
| 178 | + this.fieldWeights = Map.of(fieldName, 1.0); |
| 179 | + return this; |
113 | 180 | } |
114 | 181 |
|
115 | | - return query.toString(); |
| 182 | + /** |
| 183 | + * Set multiple text fields with weights. |
| 184 | + * |
| 185 | + * @param fieldWeights Map of field names to weights |
| 186 | + * @return Builder |
| 187 | + */ |
| 188 | + public Builder textFieldWeights(Map<String, Double> fieldWeights) { |
| 189 | + validateFieldWeights(fieldWeights); |
| 190 | + this.fieldWeights = new HashMap<>(fieldWeights); |
| 191 | + return this; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Set the scoring algorithm. |
| 196 | + * |
| 197 | + * @param scorer Scorer name (e.g., BM25STD, TFIDF) |
| 198 | + * @return Builder |
| 199 | + */ |
| 200 | + public Builder scorer(String scorer) { |
| 201 | + this.scorer = scorer; |
| 202 | + return this; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Set the filter expression. |
| 207 | + * |
| 208 | + * @param filterExpression Filter to apply |
| 209 | + * @return Builder |
| 210 | + */ |
| 211 | + public Builder filterExpression(Filter filterExpression) { |
| 212 | + this.filterExpression = filterExpression; |
| 213 | + return this; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Set the number of results to return. |
| 218 | + * |
| 219 | + * @param numResults Number of results |
| 220 | + * @return Builder |
| 221 | + */ |
| 222 | + public Builder numResults(int numResults) { |
| 223 | + this.numResults = numResults; |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Build the TextQuery instance. |
| 229 | + * |
| 230 | + * @return TextQuery |
| 231 | + * @throws IllegalArgumentException if text is null or field weights are empty |
| 232 | + */ |
| 233 | + public TextQuery build() { |
| 234 | + if (text == null || text.trim().isEmpty()) { |
| 235 | + throw new IllegalArgumentException("Text cannot be null or empty"); |
| 236 | + } |
| 237 | + if (fieldWeights.isEmpty()) { |
| 238 | + throw new IllegalArgumentException("At least one text field must be specified"); |
| 239 | + } |
| 240 | + return new TextQuery(this); |
| 241 | + } |
116 | 242 | } |
117 | 243 |
|
118 | | - @Override |
119 | | - public String toString() { |
120 | | - return toQueryString(); |
| 244 | + /** |
| 245 | + * Validate field weights. |
| 246 | + * |
| 247 | + * @param fieldWeights Map to validate |
| 248 | + * @throws IllegalArgumentException if weights are invalid |
| 249 | + */ |
| 250 | + private static void validateFieldWeights(Map<String, Double> fieldWeights) { |
| 251 | + for (Map.Entry<String, Double> entry : fieldWeights.entrySet()) { |
| 252 | + String field = entry.getKey(); |
| 253 | + Double weight = entry.getValue(); |
| 254 | + |
| 255 | + if (weight == null) { |
| 256 | + throw new IllegalArgumentException("Weight for field '" + field + "' cannot be null"); |
| 257 | + } |
| 258 | + if (weight <= 0) { |
| 259 | + throw new IllegalArgumentException( |
| 260 | + "Weight for field '" + field + "' must be positive, got " + weight); |
| 261 | + } |
| 262 | + } |
121 | 263 | } |
122 | 264 | } |
0 commit comments