All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder Maven / Gradle / Ivy

There is a newer version: 8.13.4
Show newest version
/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.index.query.xcontent;

import org.elasticsearch.util.trove.ExtTObjectFloatHashMap;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.util.collect.Lists.*;

/**
 * A query that parses a query string and runs it. There are two modes that this operates. The first,
 * when no field is added (using {@link #field(String)}, will run the query once and non prefixed fields
 * will use the {@link #defaultField(String)} set. The second, when one or more fields are added
 * (using {@link #field(String)}), will run the parsed query against the provided fields, and combine
 * them either using DisMax or a plain boolean query (see {@link #useDisMax(boolean)}).
 *
 * @author kimchy (shay.baon)
 */
public class QueryStringQueryBuilder extends BaseQueryBuilder {

    public static enum Operator {
        OR,
        AND
    }

    private final String queryString;

    private String defaultField;

    private Operator defaultOperator;

    private String analyzer;

    private Boolean allowLeadingWildcard;

    private Boolean lowercaseExpandedTerms;

    private Boolean enablePositionIncrements;

    private float fuzzyMinSim = -1;

    private float boost = -1;

    private int fuzzyPrefixLength = -1;

    private int phraseSlop = -1;

    private List fields;

    private ExtTObjectFloatHashMap fieldsBoosts;

    private Boolean useDisMax;

    private float tieBreaker = -1;

    public QueryStringQueryBuilder(String queryString) {
        this.queryString = queryString;
    }

    /**
     * The default field to run against when no prefix field is specified. Only relevant when
     * not explicitly adding fields the query string will run against.
     */
    public QueryStringQueryBuilder defaultField(String defaultField) {
        this.defaultField = defaultField;
        return this;
    }

    /**
     * Adds a field to run the query string against.
     */
    public QueryStringQueryBuilder field(String field) {
        if (fields == null) {
            fields = newArrayList();
        }
        fields.add(field);
        return this;
    }

    /**
     * Adds a field to run the query string against with a specific boost.
     */
    public QueryStringQueryBuilder field(String field, float boost) {
        if (fields == null) {
            fields = newArrayList();
        }
        fields.add(field);
        if (fieldsBoosts == null) {
            fieldsBoosts = new ExtTObjectFloatHashMap().defaultReturnValue(-1);
        }
        fieldsBoosts.put(field, boost);
        return this;
    }

    /**
     * When more than one field is used with the query string, should queries be combined using
     * dis max, or boolean query. Defaults to dis max (true).
     */
    public QueryStringQueryBuilder useDisMax(boolean useDisMax) {
        this.useDisMax = useDisMax;
        return this;
    }

    /**
     * When more than one field is used with the query string, and combined queries are using
     * dis max, control the tie breaker for it.
     */
    public QueryStringQueryBuilder tieBreaker(float tieBreaker) {
        this.tieBreaker = tieBreaker;
        return this;
    }

    /**
     * Sets the boolean operator of the query parser used to parse the query string.
     *
     * 

In default mode ({@link FieldQueryBuilder.Operator#OR}) terms without any modifiers * are considered optional: for example capital of Hungary is equal to * capital OR of OR Hungary. * *

In {@link FieldQueryBuilder.Operator#AND} mode terms are considered to be in conjunction: the * above mentioned query is parsed as capital AND of AND Hungary */ public QueryStringQueryBuilder defaultOperator(Operator defaultOperator) { this.defaultOperator = defaultOperator; return this; } /** * The optional analyzer used to analyze the query string. Note, if a field has search analyzer * defined for it, then it will be used automatically. Defaults to the smart search analyzer. */ public QueryStringQueryBuilder analyzer(String analyzer) { this.analyzer = analyzer; return this; } /** * Should leading wildcards be allowed or not. Defaults to true. */ public QueryStringQueryBuilder allowLeadingWildcard(boolean allowLeadingWildcard) { this.allowLeadingWildcard = allowLeadingWildcard; return this; } /** * Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically * lower-cased or not. Default is true. */ public QueryStringQueryBuilder lowercaseExpandedTerms(boolean lowercaseExpandedTerms) { this.lowercaseExpandedTerms = lowercaseExpandedTerms; return this; } /** * Set to true to enable position increments in result query. Defaults to * true. * *

When set, result phrase and multi-phrase queries will be aware of position increments. * Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token. */ public QueryStringQueryBuilder enablePositionIncrements(boolean enablePositionIncrements) { this.enablePositionIncrements = enablePositionIncrements; return this; } /** * Set the minimum similarity for fuzzy queries. Default is 0.5f. */ public QueryStringQueryBuilder fuzzyMinSim(float fuzzyMinSim) { this.fuzzyMinSim = fuzzyMinSim; return this; } /** * Set the minimum similarity for fuzzy queries. Default is 0.5f. */ public QueryStringQueryBuilder fuzzyPrefixLength(int fuzzyPrefixLength) { this.fuzzyPrefixLength = fuzzyPrefixLength; return this; } /** * Sets the default slop for phrases. If zero, then exact phrase matches * are required. Default value is zero. */ public QueryStringQueryBuilder phraseSlop(int phraseSlop) { this.phraseSlop = phraseSlop; return this; } /** * Sets the boost for this query. Documents matching this query will (in addition to the normal * weightings) have their score multiplied by the boost provided. */ public QueryStringQueryBuilder boost(float boost) { this.boost = boost; return this; } @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(QueryStringQueryParser.NAME); builder.field("query", queryString); if (defaultField != null) { builder.field("default_field", defaultField); } if (fields != null) { builder.startArray("fields"); for (String field : fields) { float boost = -1; if (fieldsBoosts != null) { boost = fieldsBoosts.get(field); } if (boost != -1) { field += "^" + boost; } builder.value(field); } builder.endArray(); } if (useDisMax != null) { builder.field("use_dis_max", useDisMax); } if (tieBreaker != -1) { builder.field("tie_breaker", tieBreaker); } if (defaultOperator != null) { builder.field("default_operator", defaultOperator.name().toLowerCase()); } if (analyzer != null) { builder.field("analyzer", analyzer); } if (allowLeadingWildcard != null) { builder.field("allow_leading_wildcard", allowLeadingWildcard); } if (lowercaseExpandedTerms != null) { builder.field("lowercase_expanded_terms", lowercaseExpandedTerms); } if (enablePositionIncrements != null) { builder.field("enable_position_increments", enablePositionIncrements); } if (fuzzyMinSim != -1) { builder.field("fuzzy_min_sim", fuzzyMinSim); } if (boost != -1) { builder.field("boost", boost); } if (fuzzyPrefixLength != -1) { builder.field("fuzzy_prefix_length", fuzzyPrefixLength); } if (phraseSlop != -1) { builder.field("phrase_slop", phraseSlop); } builder.endObject(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy