001package com.avaje.ebean.search; 002 003/** 004 * Text query string options. 005 * <p> 006 * This maps to an ElasticSearch "query string query". 007 * </p> 008 * 009 * <pre>{@code 010 * 011 * TextQueryString options = new TextQueryString() 012 * .analyzeWildcard(true) 013 * .fields("name") 014 * .lenient(true) 015 * .opAnd(); 016 * 017 * List<Customer> customers = server.find(Customer.class) 018 * .text() 019 * .textSimple("quick brown", options) 020 * .findList(); 021 * 022 * }</pre> 023 * 024 * 025 * <pre>{@code 026 * 027 * // just use default options 028 * TextQueryString options = new TextQueryString(); 029 * 030 * List<Customer> customers = server.find(Customer.class) 031 * .text() 032 * .textSimple("quick brown", options) 033 * .findList(); 034 * 035 * }</pre> 036 * 037 */ 038public class TextQueryString { 039 040 public static final int DEFAULT_FUZZY_MAX_EXPANSIONS = 50; 041 public static final int DEFAULT_MAX_DETERMINIZED_STATES = 10000; 042 043 protected final String[] fields; 044 045 /** 046 * Only used when multiple fields set. 047 */ 048 protected boolean useDisMax = true; 049 050 /** 051 * Only used when multiple fields set. 052 */ 053 protected double tieBreaker; 054 055 protected String defaultField; 056 057 protected boolean operatorAnd; 058 059 protected String analyzer; 060 061 protected boolean allowLeadingWildcard = true; 062 063 protected boolean lowercaseExpandedTerms = true; 064 065 protected boolean enablePositionIncrements = true; 066 067 protected int fuzzyMaxExpansions = DEFAULT_FUZZY_MAX_EXPANSIONS; 068 069 protected String fuzziness; 070 071 protected int fuzzyPrefixLength; 072 073 protected double phraseSlop; 074 075 protected double boost; 076 077 protected boolean analyzeWildcard; 078 079 protected boolean autoGeneratePhraseQueries; 080 081 protected int maxDeterminizedStates = DEFAULT_MAX_DETERMINIZED_STATES; 082 083 protected String minShouldMatch; 084 085 protected boolean lenient; 086 087 protected String locale; 088 089 protected String timeZone; 090 091 protected String rewrite; 092 093 /** 094 * Create with given fields. 095 */ 096 public static TextQueryString fields(String... fields) { 097 return new TextQueryString(fields); 098 } 099 100 /** 101 * Construct with the fields to use. 102 */ 103 public TextQueryString(String... fields) { 104 this.fields = fields; 105 } 106 107 /** 108 * Use the AND operator (rather than OR). 109 */ 110 public TextQueryString opAnd() { 111 this.operatorAnd = true; 112 return this; 113 } 114 115 /** 116 * Use the OR operator (rather than AND). 117 */ 118 public TextQueryString opOr() { 119 this.operatorAnd = false; 120 return this; 121 } 122 123 /** 124 * Set the locale. 125 */ 126 public TextQueryString locale(String locale) { 127 this.locale = locale; 128 return this; 129 } 130 131 /** 132 * Set lenient mode. 133 */ 134 public TextQueryString lenient(boolean lenient) { 135 this.lenient = lenient; 136 return this; 137 } 138 139 /** 140 * Set the minimum should match. 141 */ 142 public TextQueryString minShouldMatch(String minShouldMatch) { 143 this.minShouldMatch = minShouldMatch; 144 return this; 145 } 146 147 /** 148 * Set the analyzer. 149 */ 150 public TextQueryString analyzer(String analyzer) { 151 this.analyzer = analyzer; 152 return this; 153 } 154 155 /** 156 * Set useDisMax option (when multiple fields only). 157 */ 158 public TextQueryString useDisMax(boolean useDisMax) { 159 this.useDisMax = useDisMax; 160 return this; 161 } 162 163 /** 164 * Set tieBreaker option (when multiple fields only). 165 */ 166 public TextQueryString tieBreaker(double tieBreaker) { 167 this.tieBreaker = tieBreaker; 168 return this; 169 } 170 171 /** 172 * Set the default field. 173 */ 174 public TextQueryString defaultField(String defaultField) { 175 this.defaultField = defaultField; 176 return this; 177 } 178 179 /** 180 * Set allow leading wildcard mode. 181 */ 182 public TextQueryString allowLeadingWildcard(boolean allowLeadingWildcard) { 183 this.allowLeadingWildcard = allowLeadingWildcard; 184 return this; 185 } 186 187 /** 188 * Set lowercase expanded terms mode. 189 */ 190 public TextQueryString lowercaseExpandedTerms(boolean lowercaseExpandedTerms) { 191 this.lowercaseExpandedTerms = lowercaseExpandedTerms; 192 return this; 193 } 194 195 /** 196 * Set enable position increments mode. 197 */ 198 public TextQueryString enablePositionIncrements(boolean enablePositionIncrements) { 199 this.enablePositionIncrements = enablePositionIncrements; 200 return this; 201 } 202 203 /** 204 * Set fuzzy max expansions. 205 */ 206 public TextQueryString fuzzyMaxExpansions(int fuzzyMaxExpansions) { 207 this.fuzzyMaxExpansions = fuzzyMaxExpansions; 208 return this; 209 } 210 211 /** 212 * Set fuzziness. 213 */ 214 public TextQueryString fuzziness(String fuzziness) { 215 this.fuzziness = fuzziness; 216 return this; 217 } 218 219 /** 220 * Set the fuzzy prefix length. 221 */ 222 public TextQueryString fuzzyPrefixLength(int fuzzyPrefixLength) { 223 this.fuzzyPrefixLength = fuzzyPrefixLength; 224 return this; 225 } 226 227 /** 228 * Set the phrase slop. 229 */ 230 public TextQueryString phraseSlop(double phraseSlop) { 231 this.phraseSlop = phraseSlop; 232 return this; 233 } 234 235 /** 236 * Set the boost. 237 */ 238 public TextQueryString boost(double boost) { 239 this.boost = boost; 240 return this; 241 } 242 243 /** 244 * Set the analyze wildcard mode. 245 */ 246 public TextQueryString analyzeWildcard(boolean analyzeWildcard) { 247 this.analyzeWildcard = analyzeWildcard; 248 return this; 249 } 250 /** 251 * Set the auto generate phrase queries mode. 252 */ 253 public TextQueryString autoGeneratePhraseQueries(boolean autoGeneratePhraseQueries) { 254 this.autoGeneratePhraseQueries = autoGeneratePhraseQueries; 255 return this; 256 } 257 258 /** 259 * Set the max determinized states. 260 */ 261 public TextQueryString maxDeterminizedStates(int maxDeterminizedStates) { 262 this.maxDeterminizedStates = maxDeterminizedStates; 263 return this; 264 } 265 266 /** 267 * Set the time zone. 268 */ 269 public TextQueryString timeZone(String timeZone) { 270 this.timeZone = timeZone; 271 return this; 272 } 273 274 /** 275 * Set the rewrite option. 276 */ 277 public TextQueryString rewrite(String rewrite) { 278 this.rewrite = rewrite; 279 return this; 280 } 281 282 /** 283 * Return the rewrite option. 284 */ 285 public String getRewrite() { 286 return rewrite; 287 } 288 289 /** 290 * Return the fields. 291 */ 292 public String[] getFields() { 293 return fields; 294 } 295 296 /** 297 * Return true if AND is the default operator. 298 */ 299 public boolean isOperatorAnd() { 300 return operatorAnd; 301 } 302 303 /** 304 * Return the analyzer. 305 */ 306 public String getAnalyzer() { 307 return analyzer; 308 } 309 310 /** 311 * Return the locale. 312 */ 313 public String getLocale() { 314 return locale; 315 } 316 317 /** 318 * Return lenient mode. 319 */ 320 public boolean isLenient() { 321 return lenient; 322 } 323 324 /** 325 * Return the minimum should match. 326 */ 327 public String getMinShouldMatch() { 328 return minShouldMatch; 329 } 330 331 /** 332 * Return the useDixMax mode. 333 */ 334 public boolean isUseDisMax() { 335 return useDisMax; 336 } 337 338 /** 339 * Return the tie breaker. 340 */ 341 public double getTieBreaker() { 342 return tieBreaker; 343 } 344 345 /** 346 * Return the default field. 347 */ 348 public String getDefaultField() { 349 return defaultField; 350 } 351 352 /** 353 * Return the allow leading wildcard mode. 354 */ 355 public boolean isAllowLeadingWildcard() { 356 return allowLeadingWildcard; 357 } 358 359 /** 360 * Return the lowercase expanded terms mode. 361 */ 362 public boolean isLowercaseExpandedTerms() { 363 return lowercaseExpandedTerms; 364 } 365 366 /** 367 * Return the enable position increments mode. 368 */ 369 public boolean isEnablePositionIncrements() { 370 return enablePositionIncrements; 371 } 372 373 /** 374 * Return the fuzzy max expansions. 375 */ 376 public int getFuzzyMaxExpansions() { 377 return fuzzyMaxExpansions; 378 } 379 380 /** 381 * Return the fuzziness. 382 */ 383 public String getFuzziness() { 384 return fuzziness; 385 } 386 387 /** 388 * Return the fuzzy prefix length. 389 */ 390 public int getFuzzyPrefixLength() { 391 return fuzzyPrefixLength; 392 } 393 394 /** 395 * Return the phrase slop. 396 */ 397 public double getPhraseSlop() { 398 return phraseSlop; 399 } 400 401 /** 402 * Return the analyze wildcard mode. 403 */ 404 public boolean isAnalyzeWildcard() { 405 return analyzeWildcard; 406 } 407 408 /** 409 * Return the boost. 410 */ 411 public double getBoost() { 412 return boost; 413 } 414 415 /** 416 * Return the auto generate phase queries mode. 417 */ 418 public boolean isAutoGeneratePhraseQueries() { 419 return autoGeneratePhraseQueries; 420 } 421 422 /** 423 * Return the max determinized states. 424 */ 425 public int getMaxDeterminizedStates() { 426 return maxDeterminizedStates; 427 } 428 429 /** 430 * Return the time zone. 431 */ 432 public String getTimeZone() { 433 return timeZone; 434 } 435 436}