|
| 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.mapper; |
| 21 | + |
| 22 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 23 | +import org.apache.lucene.search.MatchNoDocsQuery; |
| 24 | +import org.apache.lucene.search.MultiTermQuery; |
| 25 | +import org.apache.lucene.search.Query; |
| 26 | +import org.apache.lucene.util.BytesRef; |
| 27 | +import org.elasticsearch.common.Nullable; |
| 28 | +import org.elasticsearch.common.lucene.search.Queries; |
| 29 | +import org.elasticsearch.common.regex.Regex; |
| 30 | +import org.elasticsearch.index.query.QueryShardContext; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +/** |
| 35 | + * A {@link MappedFieldType} that has the same value for all documents. |
| 36 | + * Factory methods for queries are called at rewrite time so they should be |
| 37 | + * cheap. In particular they should not read data from disk or perform a |
| 38 | + * network call. Furthermore they may only return a {@link MatchAllDocsQuery} |
| 39 | + * or a {@link MatchNoDocsQuery}. |
| 40 | + */ |
| 41 | +public abstract class ConstantFieldType extends MappedFieldType { |
| 42 | + |
| 43 | + public ConstantFieldType() { |
| 44 | + super(); |
| 45 | + } |
| 46 | + |
| 47 | + public ConstantFieldType(ConstantFieldType other) { |
| 48 | + super(other); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public final boolean isSearchable() { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public final boolean isAggregatable() { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public final Query existsQuery(QueryShardContext context) { |
| 63 | + return new MatchAllDocsQuery(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Return whether the constant value of this field matches the provided {@code pattern} |
| 68 | + * as documented in {@link Regex#simpleMatch}. |
| 69 | + */ |
| 70 | + protected abstract boolean matches(String pattern, QueryShardContext context); |
| 71 | + |
| 72 | + private static String valueToString(Object value) { |
| 73 | + return value instanceof BytesRef |
| 74 | + ? ((BytesRef) value).utf8ToString() |
| 75 | + : value.toString(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public final Query termQuery(Object value, QueryShardContext context) { |
| 80 | + String pattern = valueToString(value); |
| 81 | + if (matches(pattern, context)) { |
| 82 | + return Queries.newMatchAllQuery(); |
| 83 | + } else { |
| 84 | + return new MatchNoDocsQuery(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public final Query termsQuery(List<?> values, QueryShardContext context) { |
| 90 | + for (Object value : values) { |
| 91 | + String pattern = valueToString(value); |
| 92 | + if (matches(pattern, context)) { |
| 93 | + // `terms` queries are a disjunction, so one matching term is enough |
| 94 | + return Queries.newMatchAllQuery(); |
| 95 | + } |
| 96 | + } |
| 97 | + return new MatchNoDocsQuery(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public final Query prefixQuery(String prefix, |
| 102 | + @Nullable MultiTermQuery.RewriteMethod method, |
| 103 | + QueryShardContext context) { |
| 104 | + String pattern = prefix + "*"; |
| 105 | + if (matches(pattern, context)) { |
| 106 | + return Queries.newMatchAllQuery(); |
| 107 | + } else { |
| 108 | + return new MatchNoDocsQuery(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public final Query wildcardQuery(String value, |
| 114 | + @Nullable MultiTermQuery.RewriteMethod method, |
| 115 | + QueryShardContext context) { |
| 116 | + if (matches(value, context)) { |
| 117 | + return Queries.newMatchAllQuery(); |
| 118 | + } else { |
| 119 | + return new MatchNoDocsQuery(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments