org.elasticsearch.index.query.TermQueryBuilder 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.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ConstantFieldType;
import java.io.IOException;
/**
* A Query that matches documents containing a term.
*/
public class TermQueryBuilder extends BaseTermQueryBuilder {
public static final String NAME = "term";
private static final ParseField TERM_FIELD = new ParseField("term");
private static final ParseField VALUE_FIELD = new ParseField("value");
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, String) */
public TermQueryBuilder(String fieldName, String value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, int) */
public TermQueryBuilder(String fieldName, int value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, long) */
public TermQueryBuilder(String fieldName, long value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, float) */
public TermQueryBuilder(String fieldName, float value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, double) */
public TermQueryBuilder(String fieldName, double value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, boolean) */
public TermQueryBuilder(String fieldName, boolean value) {
super(fieldName, (Object) value);
}
/** @see BaseTermQueryBuilder#BaseTermQueryBuilder(String, Object) */
public TermQueryBuilder(String fieldName, Object value) {
super(fieldName, value);
}
/**
* Read from a stream.
*/
public TermQueryBuilder(StreamInput in) throws IOException {
super(in);
}
public static TermQueryBuilder fromXContent(XContentParser parser) throws IOException {
String queryName = null;
String fieldName = null;
Object value = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else {
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
value = maybeConvertToBytesRef(parser.objectBytes());
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
value = maybeConvertToBytesRef(parser.objectBytes());
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else {
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support [" + currentFieldName + "]");
}
}
}
} else if (token.isValue()) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = currentFieldName;
value = maybeConvertToBytesRef(parser.objectBytes());
} else if (token == XContentParser.Token.START_ARRAY) {
throw new ParsingException(parser.getTokenLocation(), "[term] query does not support array of values");
}
}
TermQueryBuilder termQuery = new TermQueryBuilder(fieldName, value);
termQuery.boost(boost);
if (queryName != null) {
termQuery.queryName(queryName);
}
return termQuery;
}
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
QueryShardContext context = queryRewriteContext.convertToShardContext();
if (context != null) {
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
// since rewrites might happen on a network thread.
Query query = fieldType.termQuery(value, context);
if (query instanceof MatchAllDocsQuery) {
return new MatchAllQueryBuilder();
} else if (query instanceof MatchNoDocsQuery) {
return new MatchNoneQueryBuilder();
} else {
assert false : "Constant fields must produce match-all or match-none queries, got " + query ;
}
}
}
return super.doRewrite(queryRewriteContext);
}
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType mapper = context.fieldMapper(this.fieldName);
if (mapper == null) {
throw new IllegalStateException("Rewrite first");
}
return mapper.termQuery(this.value, context);
}
@Override
public String getWriteableName() {
return NAME;
}
}