org.elasticsearch.index.query.TermsSetQueryBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.CoveringQuery;
import org.apache.lucene.search.DoubleValues;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.LongValues;
import org.apache.lucene.search.LongValuesSource;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.SearchScript;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class TermsSetQueryBuilder extends AbstractQueryBuilder {
public static final String NAME = "terms_set";
static final ParseField TERMS_FIELD = new ParseField("terms");
static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match_field");
static final ParseField MINIMUM_SHOULD_MATCH_SCRIPT = new ParseField("minimum_should_match_script");
private final String fieldName;
private final List values;
private String minimumShouldMatchField;
private Script minimumShouldMatchScript;
public TermsSetQueryBuilder(String fieldName, List values) {
this.fieldName = Objects.requireNonNull(fieldName);
this.values = TermsQueryBuilder.convert(Objects.requireNonNull(values));
}
public TermsSetQueryBuilder(StreamInput in) throws IOException {
super(in);
this.fieldName = in.readString();
this.values = (List) in.readGenericValue();
this.minimumShouldMatchField = in.readOptionalString();
this.minimumShouldMatchScript = in.readOptionalWriteable(Script::new);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeGenericValue(values);
out.writeOptionalString(minimumShouldMatchField);
out.writeOptionalWriteable(minimumShouldMatchScript);
}
public List getValues() {
return values;
}
public String getMinimumShouldMatchField() {
return minimumShouldMatchField;
}
public TermsSetQueryBuilder setMinimumShouldMatchField(String minimumShouldMatchField) {
if (minimumShouldMatchScript != null) {
throw new IllegalArgumentException("A script has already been specified. Cannot specify both a field and script");
}
this.minimumShouldMatchField = minimumShouldMatchField;
return this;
}
public Script getMinimumShouldMatchScript() {
return minimumShouldMatchScript;
}
public TermsSetQueryBuilder setMinimumShouldMatchScript(Script minimumShouldMatchScript) {
if (minimumShouldMatchField != null) {
throw new IllegalArgumentException("A field has already been specified. Cannot specify both a field and script");
}
this.minimumShouldMatchScript = minimumShouldMatchScript;
return this;
}
@Override
protected boolean doEquals(TermsSetQueryBuilder other) {
return Objects.equals(fieldName, this.fieldName) && Objects.equals(values, this.values) &&
Objects.equals(minimumShouldMatchField, this.minimumShouldMatchField) &&
Objects.equals(minimumShouldMatchScript, this.minimumShouldMatchScript);
}
@Override
protected int doHashCode() {
return Objects.hash(fieldName, values, minimumShouldMatchField, minimumShouldMatchScript);
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(fieldName);
builder.field(TERMS_FIELD.getPreferredName(), TermsQueryBuilder.convertBack(values));
if (minimumShouldMatchField != null) {
builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatchField);
}
if (minimumShouldMatchScript != null) {
builder.field(MINIMUM_SHOULD_MATCH_SCRIPT.getPreferredName(), minimumShouldMatchScript);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}
public static TermsSetQueryBuilder fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "]");
}
String currentFieldName = parser.currentName();
String fieldName = currentFieldName;
token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "]");
}
List