| 
22 | 22 | import org.apache.lucene.document.Document;  | 
23 | 23 | import org.apache.lucene.document.Field;  | 
24 | 24 | import org.apache.lucene.index.IndexOptions;  | 
 | 25 | +import org.apache.lucene.search.Query;  | 
 | 26 | +import org.apache.lucene.util.BytesRef;  | 
25 | 27 | import org.elasticsearch.Version;  | 
26 | 28 | import org.elasticsearch.common.Nullable;  | 
27 | 29 | import org.elasticsearch.common.Strings;  | 
28 | 30 | import org.elasticsearch.common.lucene.Lucene;  | 
 | 31 | +import org.elasticsearch.common.lucene.search.Queries;  | 
29 | 32 | import org.elasticsearch.common.settings.Settings;  | 
30 | 33 | import org.elasticsearch.common.xcontent.XContentBuilder;  | 
31 | 34 | import org.elasticsearch.index.fielddata.FieldDataType;  | 
 | 
34 | 37 | import org.elasticsearch.index.mapper.MapperParsingException;  | 
35 | 38 | import org.elasticsearch.index.mapper.MergeMappingException;  | 
36 | 39 | import org.elasticsearch.index.mapper.MergeResult;  | 
37 |  | -import org.elasticsearch.index.mapper.ParseContext;  | 
38 | 40 | import org.elasticsearch.index.mapper.MetadataFieldMapper;  | 
 | 41 | +import org.elasticsearch.index.mapper.ParseContext;  | 
39 | 42 | import org.elasticsearch.index.mapper.core.AbstractFieldMapper;  | 
 | 43 | +import org.elasticsearch.index.query.QueryParseContext;  | 
40 | 44 | 
 
  | 
41 | 45 | import java.io.IOException;  | 
42 | 46 | import java.util.Iterator;  | 
@@ -135,6 +139,62 @@ public String typeName() {  | 
135 | 139 |             return CONTENT_TYPE;  | 
136 | 140 |         }  | 
137 | 141 | 
 
  | 
 | 142 | +        @Override  | 
 | 143 | +        public boolean useTermQueryWithQueryString() {  | 
 | 144 | +            // As we spoof the presence of an indexed field we have to override  | 
 | 145 | +            // the default of returning false which otherwise leads MatchQuery  | 
 | 146 | +            // et al to run an analyzer over the query string and then try to  | 
 | 147 | +            // hit the search index. We need them to use our termQuery(..)  | 
 | 148 | +            // method which checks index names  | 
 | 149 | +            return true;  | 
 | 150 | +        }  | 
 | 151 | + | 
 | 152 | +        /**  | 
 | 153 | +         * This termQuery impl looks at the context to determine the index that  | 
 | 154 | +         * is being queried and then returns a MATCH_ALL_QUERY or MATCH_NO_QUERY  | 
 | 155 | +         * if the value matches this index. This can be useful if aliases or  | 
 | 156 | +         * wildcards are used but the aim is to restrict the query to specific  | 
 | 157 | +         * indices  | 
 | 158 | +         */  | 
 | 159 | +        @Override  | 
 | 160 | +        public Query termQuery(Object value, @Nullable QueryParseContext context) {  | 
 | 161 | +            if (context == null) {  | 
 | 162 | +                return super.termQuery(value, context);  | 
 | 163 | +            }  | 
 | 164 | +            if (isSameIndex(value, context.index().getName())) {  | 
 | 165 | +                return Queries.newMatchAllQuery();  | 
 | 166 | +            } else {  | 
 | 167 | +                return Queries.newMatchNoDocsQuery();  | 
 | 168 | +            }  | 
 | 169 | +        }  | 
 | 170 | +          | 
 | 171 | +          | 
 | 172 | + | 
 | 173 | +        @Override  | 
 | 174 | +        public Query termsQuery(List values, QueryParseContext context) {  | 
 | 175 | +            if (context == null) {  | 
 | 176 | +                return super.termsQuery(values, context);  | 
 | 177 | +            }  | 
 | 178 | +            for (Object value : values) {  | 
 | 179 | +                if (isSameIndex(value, context.index().getName())) {  | 
 | 180 | +                    // No need to OR these clauses - we can only logically be  | 
 | 181 | +                    // running in the context of just one of these index names.  | 
 | 182 | +                    return Queries.newMatchAllQuery();  | 
 | 183 | +                }  | 
 | 184 | +            }  | 
 | 185 | +            // None of the listed index names are this one  | 
 | 186 | +            return Queries.newMatchNoDocsQuery();  | 
 | 187 | +        }  | 
 | 188 | + | 
 | 189 | +        private boolean isSameIndex(Object value, String indexName) {  | 
 | 190 | +            if (value instanceof BytesRef) {  | 
 | 191 | +                BytesRef indexNameRef = new BytesRef(indexName);  | 
 | 192 | +                return (indexNameRef.bytesEquals((BytesRef) value));  | 
 | 193 | +            } else {  | 
 | 194 | +                return indexName.equals(value.toString());  | 
 | 195 | +            }  | 
 | 196 | +        }  | 
 | 197 | + | 
138 | 198 |         @Override  | 
139 | 199 |         public String value(Object value) {  | 
140 | 200 |             if (value == null) {  | 
 | 
0 commit comments