forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This attempts to do to SpanTermQueryBuilder what has already been changed for TermQueryBuilder. The commit tries to avoid code duplication where possible by pulling what is the same for both QueryBuilders and tests into separate classes. Relates to elastic#10217
- Loading branch information
Isabel Drost-Fromm
committed
May 15, 2015
1 parent
d7884b6
commit 8c0f232
Showing
9 changed files
with
458 additions
and
273 deletions.
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/* | ||
* 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.util.BytesRef; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Streamable; | ||
import org.elasticsearch.common.lucene.BytesRefs; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public abstract class BaseTermQueryBuilder extends QueryBuilder implements Streamable { | ||
|
||
/** Name of field to match against. */ | ||
protected String fieldName; | ||
|
||
/** Value to find matches for. */ | ||
protected Object value; | ||
|
||
/** Query boost. */ | ||
protected float boost = 1.0f; | ||
|
||
/** Name of the query. */ | ||
protected String queryName; | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, String value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, int value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, long value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, float value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, double value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, boolean value) { | ||
this(fieldName, (Object) value); | ||
} | ||
|
||
/** | ||
* Constructs a new base term query. | ||
* | ||
* @param fieldName The name of the field | ||
* @param value The value of the term | ||
*/ | ||
public BaseTermQueryBuilder(String fieldName, Object value) { | ||
this.fieldName = fieldName; | ||
if (value instanceof String) { | ||
this.value = BytesRefs.toBytesRef(value); | ||
} else { | ||
this.value = value; | ||
} | ||
} | ||
|
||
BaseTermQueryBuilder() { | ||
// for serialization only | ||
} | ||
|
||
/** Returns the field name used in this query @see org.apache.lucene.search.spans.SpanTermQuery#getField() */ | ||
public String fieldName() { | ||
return this.fieldName; | ||
} | ||
|
||
/** Returns the value used in this query @see org.apache.lucene.index.Term#text() */ | ||
public Object value() { | ||
return this.value; | ||
} | ||
|
||
/** Returns the query name for the query. */ | ||
public String queryName() { | ||
return this.queryName; | ||
} | ||
/** Returns the boost for this query. @see org.apache.lucene.search.Query#setBoost() */ | ||
public float boost() { | ||
return this.boost; | ||
} | ||
|
||
@Override | ||
protected void doXContent(XContentBuilder builder, Params params) throws IOException { | ||
Object valueToWrite = value; | ||
if (value instanceof BytesRef) { | ||
valueToWrite = ((BytesRef) value).utf8ToString(); | ||
} | ||
|
||
if (boost == 1.0f && queryName == null) { | ||
builder.field(fieldName, valueToWrite); | ||
} else { | ||
builder.startObject(fieldName); | ||
builder.field("value", valueToWrite); | ||
if (boost != 1.0f) { | ||
builder.field("boost", boost); | ||
} | ||
if (queryName != null) { | ||
builder.field("_name", queryName); | ||
} | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
} | ||
|
||
/** Returns a {@link QueryValidationException} if fieldName is null or empty, or if value is null. */ | ||
@Override | ||
public QueryValidationException validate() { | ||
QueryValidationException validationException = null; | ||
if (fieldName == null || fieldName.isEmpty()) { | ||
validationException = QueryValidationException.addValidationError("field name cannot be null or empty.", validationException); | ||
} | ||
if (value == null) { | ||
validationException = QueryValidationException.addValidationError("value cannot be null.", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
|
||
@Override | ||
public int hashCode() { | ||
/* | ||
* Added class name to the hashcode so a {@link SpanTermQueryBuilder} and a {@link TermQueryBuilder} aren't considered the same | ||
* if they have the same parameters set. | ||
*/ | ||
return Objects.hash(getClass(), fieldName, value, boost, queryName); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
BaseTermQueryBuilder other = (BaseTermQueryBuilder) obj; | ||
return Objects.equals(fieldName, other.fieldName) && | ||
Objects.equals(value, other.value) && | ||
Objects.equals(boost, other.boost) && | ||
Objects.equals(queryName, other.queryName); | ||
} | ||
|
||
/** Read the given parameters. */ | ||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
fieldName = in.readString(); | ||
value = in.readGenericValue(); | ||
boost = in.readFloat(); | ||
queryName = in.readOptionalString(); | ||
} | ||
|
||
/** Writes the given parameters. */ | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(fieldName); | ||
out.writeGenericValue(value); | ||
out.writeFloat(boost); | ||
out.writeOptionalString(queryName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.