|
| 1 | +/* |
| 2 | + * Licensed to Elastic Search and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Elastic Search licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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.json; |
| 21 | + |
| 22 | +import com.google.inject.Inject; |
| 23 | +import org.apache.lucene.index.Term; |
| 24 | +import org.apache.lucene.search.Filter; |
| 25 | +import org.apache.lucene.search.TermsFilter; |
| 26 | +import org.codehaus.jackson.JsonParser; |
| 27 | +import org.codehaus.jackson.JsonToken; |
| 28 | +import org.elasticsearch.index.AbstractIndexComponent; |
| 29 | +import org.elasticsearch.index.Index; |
| 30 | +import org.elasticsearch.index.mapper.FieldMapper; |
| 31 | +import org.elasticsearch.index.mapper.MapperService; |
| 32 | +import org.elasticsearch.index.query.QueryParsingException; |
| 33 | +import org.elasticsearch.index.settings.IndexSettings; |
| 34 | +import org.elasticsearch.util.settings.Settings; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | + |
| 38 | +import static org.elasticsearch.index.query.support.QueryParsers.*; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author kimchy (Shay Banon) |
| 42 | + */ |
| 43 | +public class TermsJsonFilterParser extends AbstractIndexComponent implements JsonFilterParser { |
| 44 | + |
| 45 | + public static final String NAME = "terms"; |
| 46 | + |
| 47 | + @Inject public TermsJsonFilterParser(Index index, @IndexSettings Settings settings) { |
| 48 | + super(index, settings); |
| 49 | + } |
| 50 | + |
| 51 | + @Override public String name() { |
| 52 | + return NAME; |
| 53 | + } |
| 54 | + |
| 55 | + @Override public Filter parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException { |
| 56 | + JsonParser jp = parseContext.jp(); |
| 57 | + |
| 58 | + JsonToken token = jp.getCurrentToken(); |
| 59 | + if (token == JsonToken.START_OBJECT) { |
| 60 | + token = jp.nextToken(); |
| 61 | + } |
| 62 | + assert token == JsonToken.FIELD_NAME; |
| 63 | + String fieldName = jp.getCurrentName(); |
| 64 | + |
| 65 | + FieldMapper fieldMapper = null; |
| 66 | + MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); |
| 67 | + if (smartNameFieldMappers != null) { |
| 68 | + fieldMapper = smartNameFieldMappers.fieldMappers().mapper(); |
| 69 | + if (fieldMapper != null) { |
| 70 | + fieldName = fieldMapper.indexName(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + token = jp.nextToken(); |
| 75 | + if (token != JsonToken.START_ARRAY) { |
| 76 | + throw new QueryParsingException(index, "Terms filter must define the terms to filter on as an array"); |
| 77 | + } |
| 78 | + |
| 79 | + TermsFilter termsFilter = new TermsFilter(); |
| 80 | + while ((token = jp.nextToken()) != JsonToken.END_ARRAY) { |
| 81 | + String value = jp.getText(); |
| 82 | + if (value == null) { |
| 83 | + throw new QueryParsingException(index, "No value specified for term filter"); |
| 84 | + } |
| 85 | + if (fieldMapper != null) { |
| 86 | + value = fieldMapper.indexedValue(value); |
| 87 | + } |
| 88 | + termsFilter.addTerm(new Term(fieldName, value)); |
| 89 | + } |
| 90 | + jp.nextToken(); |
| 91 | + |
| 92 | + |
| 93 | + Filter filter = parseContext.cacheFilterIfPossible(termsFilter); |
| 94 | + return wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext.filterCache()); |
| 95 | + } |
| 96 | +} |
0 commit comments