|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.query; |
| 21 | + |
| 22 | +import org.apache.lucene.util.BytesRef; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 25 | +import org.elasticsearch.common.io.stream.Streamable; |
| 26 | +import org.elasticsearch.common.lucene.BytesRefs; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +public abstract class BaseTermQueryBuilder<QB extends BoostableQueryBuilder<QB>> extends QueryBuilder implements Streamable, BoostableQueryBuilder<QB> { |
| 33 | + |
| 34 | + /** Name of field to match against. */ |
| 35 | + protected String fieldName; |
| 36 | + |
| 37 | + /** Value to find matches for. */ |
| 38 | + protected Object value; |
| 39 | + |
| 40 | + /** Query boost. */ |
| 41 | + protected float boost = 1.0f; |
| 42 | + |
| 43 | + /** Name of the query. */ |
| 44 | + protected String queryName; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs a new base term query. |
| 48 | + * |
| 49 | + * @param fieldName The name of the field |
| 50 | + * @param value The value of the term |
| 51 | + */ |
| 52 | + public BaseTermQueryBuilder(String fieldName, String value) { |
| 53 | + this(fieldName, (Object) value); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs a new base term query. |
| 58 | + * |
| 59 | + * @param fieldName The name of the field |
| 60 | + * @param value The value of the term |
| 61 | + */ |
| 62 | + public BaseTermQueryBuilder(String fieldName, int value) { |
| 63 | + this(fieldName, (Object) value); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Constructs a new base term query. |
| 68 | + * |
| 69 | + * @param fieldName The name of the field |
| 70 | + * @param value The value of the term |
| 71 | + */ |
| 72 | + public BaseTermQueryBuilder(String fieldName, long value) { |
| 73 | + this(fieldName, (Object) value); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Constructs a new base term query. |
| 78 | + * |
| 79 | + * @param fieldName The name of the field |
| 80 | + * @param value The value of the term |
| 81 | + */ |
| 82 | + public BaseTermQueryBuilder(String fieldName, float value) { |
| 83 | + this(fieldName, (Object) value); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Constructs a new base term query. |
| 88 | + * |
| 89 | + * @param fieldName The name of the field |
| 90 | + * @param value The value of the term |
| 91 | + */ |
| 92 | + public BaseTermQueryBuilder(String fieldName, double value) { |
| 93 | + this(fieldName, (Object) value); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Constructs a new base term query. |
| 98 | + * |
| 99 | + * @param fieldName The name of the field |
| 100 | + * @param value The value of the term |
| 101 | + */ |
| 102 | + public BaseTermQueryBuilder(String fieldName, boolean value) { |
| 103 | + this(fieldName, (Object) value); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Constructs a new base term query. |
| 108 | + * |
| 109 | + * @param fieldName The name of the field |
| 110 | + * @param value The value of the term |
| 111 | + */ |
| 112 | + public BaseTermQueryBuilder(String fieldName, Object value) { |
| 113 | + this.fieldName = fieldName; |
| 114 | + if (value instanceof String) { |
| 115 | + this.value = BytesRefs.toBytesRef(value); |
| 116 | + } else { |
| 117 | + this.value = value; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + BaseTermQueryBuilder() { |
| 122 | + // for serialization only |
| 123 | + } |
| 124 | + |
| 125 | + /** Returns the field name used in this query. */ |
| 126 | + public String fieldName() { |
| 127 | + return this.fieldName; |
| 128 | + } |
| 129 | + |
| 130 | + /** Returns the value used in this query. */ |
| 131 | + public Object value() { |
| 132 | + return this.value; |
| 133 | + } |
| 134 | + |
| 135 | + /** Returns the query name for the query. */ |
| 136 | + public String queryName() { |
| 137 | + return this.queryName; |
| 138 | + } |
| 139 | + /** |
| 140 | + * Sets the query name for the query. |
| 141 | + */ |
| 142 | + @SuppressWarnings("unchecked") |
| 143 | + public QB queryName(String queryName) { |
| 144 | + this.queryName = queryName; |
| 145 | + return (QB) this; |
| 146 | + } |
| 147 | + |
| 148 | + /** Returns the boost for this query. */ |
| 149 | + public float boost() { |
| 150 | + return this.boost; |
| 151 | + } |
| 152 | + /** |
| 153 | + * Sets the boost for this query. Documents matching this query will (in addition to the normal |
| 154 | + * weightings) have their score multiplied by the boost provided. |
| 155 | + */ |
| 156 | + @SuppressWarnings("unchecked") |
| 157 | + @Override |
| 158 | + public QB boost(float boost) { |
| 159 | + this.boost = boost; |
| 160 | + return (QB) this; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + protected void doXContent(XContentBuilder builder, Params params) throws IOException { |
| 165 | + builder.startObject(parserName()); |
| 166 | + Object valueToWrite = value; |
| 167 | + if (value instanceof BytesRef) { |
| 168 | + valueToWrite = ((BytesRef) value).utf8ToString(); |
| 169 | + } |
| 170 | + |
| 171 | + if (boost == 1.0f && queryName == null) { |
| 172 | + builder.field(fieldName, valueToWrite); |
| 173 | + } else { |
| 174 | + builder.startObject(fieldName); |
| 175 | + builder.field("value", valueToWrite); |
| 176 | + if (boost != 1.0f) { |
| 177 | + builder.field("boost", boost); |
| 178 | + } |
| 179 | + if (queryName != null) { |
| 180 | + builder.field("_name", queryName); |
| 181 | + } |
| 182 | + builder.endObject(); |
| 183 | + } |
| 184 | + builder.endObject(); |
| 185 | + } |
| 186 | + |
| 187 | + /** Returns a {@link QueryValidationException} if fieldName is null or empty, or if value is null. */ |
| 188 | + @Override |
| 189 | + public QueryValidationException validate() { |
| 190 | + QueryValidationException validationException = null; |
| 191 | + if (fieldName == null || fieldName.isEmpty()) { |
| 192 | + validationException = QueryValidationException.addValidationError("field name cannot be null or empty.", validationException); |
| 193 | + } |
| 194 | + if (value == null) { |
| 195 | + validationException = QueryValidationException.addValidationError("value cannot be null.", validationException); |
| 196 | + } |
| 197 | + return validationException; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public int hashCode() { |
| 202 | + return Objects.hash(getClass(), fieldName, value, boost, queryName); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public boolean equals(Object obj) { |
| 207 | + if (this == obj) { |
| 208 | + return true; |
| 209 | + } |
| 210 | + if (obj == null || getClass() != obj.getClass()) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + @SuppressWarnings("rawtypes") |
| 214 | + BaseTermQueryBuilder other = (BaseTermQueryBuilder) obj; |
| 215 | + return Objects.equals(fieldName, other.fieldName) && |
| 216 | + Objects.equals(value, other.value) && |
| 217 | + Objects.equals(boost, other.boost) && |
| 218 | + Objects.equals(queryName, other.queryName); |
| 219 | + } |
| 220 | + |
| 221 | + /** Read the given parameters. */ |
| 222 | + @Override |
| 223 | + public void readFrom(StreamInput in) throws IOException { |
| 224 | + fieldName = in.readString(); |
| 225 | + value = in.readGenericValue(); |
| 226 | + boost = in.readFloat(); |
| 227 | + queryName = in.readOptionalString(); |
| 228 | + } |
| 229 | + |
| 230 | + /** Writes the given parameters. */ |
| 231 | + @Override |
| 232 | + public void writeTo(StreamOutput out) throws IOException { |
| 233 | + out.writeString(fieldName); |
| 234 | + out.writeGenericValue(value); |
| 235 | + out.writeFloat(boost); |
| 236 | + out.writeOptionalString(queryName); |
| 237 | + } |
| 238 | +} |
0 commit comments