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