|
| 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.common.collect.Sets; |
| 23 | +import org.apache.lucene.search.Query; |
| 24 | +import org.codehaus.jackson.JsonParser; |
| 25 | +import org.codehaus.jackson.JsonToken; |
| 26 | +import org.elasticsearch.index.AbstractIndexComponent; |
| 27 | +import org.elasticsearch.index.Index; |
| 28 | +import org.elasticsearch.index.mapper.MapperService; |
| 29 | +import org.elasticsearch.index.query.QueryParsingException; |
| 30 | +import org.elasticsearch.index.settings.IndexSettings; |
| 31 | +import org.elasticsearch.util.lucene.search.MoreLikeThisQuery; |
| 32 | +import org.elasticsearch.util.settings.Settings; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +import static org.elasticsearch.index.query.support.QueryParsers.*; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author kimchy (shay.banon) |
| 41 | + */ |
| 42 | +public class MoreLikeThisFieldJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { |
| 43 | + |
| 44 | + public static final String NAME = "moreLikeThisField"; |
| 45 | + |
| 46 | + public MoreLikeThisFieldJsonQueryParser(Index index, @IndexSettings Settings indexSettings) { |
| 47 | + super(index, indexSettings); |
| 48 | + } |
| 49 | + |
| 50 | + @Override public String name() { |
| 51 | + return NAME; |
| 52 | + } |
| 53 | + |
| 54 | + @Override public Query parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException { |
| 55 | + JsonParser jp = parseContext.jp(); |
| 56 | + |
| 57 | + JsonToken token = jp.nextToken(); |
| 58 | + assert token == JsonToken.FIELD_NAME; |
| 59 | + String fieldName = jp.getCurrentName(); |
| 60 | + |
| 61 | + // now, we move after the field name, which starts the object |
| 62 | + token = jp.nextToken(); |
| 63 | + assert token == JsonToken.START_OBJECT; |
| 64 | + |
| 65 | + |
| 66 | + MoreLikeThisQuery mltQuery = new MoreLikeThisQuery(); |
| 67 | + |
| 68 | + String currentFieldName = null; |
| 69 | + while ((token = jp.nextToken()) != JsonToken.END_OBJECT) { |
| 70 | + if (token == JsonToken.FIELD_NAME) { |
| 71 | + currentFieldName = jp.getCurrentName(); |
| 72 | + } else if (token == JsonToken.VALUE_STRING) { |
| 73 | + if ("likeText".equals(currentFieldName)) { |
| 74 | + mltQuery.setLikeText(jp.getText()); |
| 75 | + } |
| 76 | + } else if (token == JsonToken.VALUE_NUMBER_INT) { |
| 77 | + if ("minTermFrequency".equals(currentFieldName)) { |
| 78 | + mltQuery.setMinTermFrequency(jp.getIntValue()); |
| 79 | + } else if ("maxQueryTerms".equals(currentFieldName)) { |
| 80 | + mltQuery.setMaxQueryTerms(jp.getIntValue()); |
| 81 | + } else if ("minDocFreq".equals(currentFieldName)) { |
| 82 | + mltQuery.setMinDocFreq(jp.getIntValue()); |
| 83 | + } else if ("maxDocFreq".equals(currentFieldName)) { |
| 84 | + mltQuery.setMaxDocFreq(jp.getIntValue()); |
| 85 | + } else if ("minWordLen".equals(currentFieldName)) { |
| 86 | + mltQuery.setMinWordLen(jp.getIntValue()); |
| 87 | + } else if ("maxWordLen".equals(currentFieldName)) { |
| 88 | + mltQuery.setMaxWordLen(jp.getIntValue()); |
| 89 | + } else if ("boostTerms".equals(currentFieldName)) { |
| 90 | + mltQuery.setBoostTerms(jp.getIntValue() != 0); |
| 91 | + } else if ("boostTermsFactor".equals(currentFieldName)) { |
| 92 | + mltQuery.setBoostTermsFactor(jp.getIntValue()); |
| 93 | + } |
| 94 | + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { |
| 95 | + if ("boostTermsFactor".equals(currentFieldName)) { |
| 96 | + mltQuery.setBoostTermsFactor(jp.getFloatValue()); |
| 97 | + } |
| 98 | + } else if (token == JsonToken.START_ARRAY) { |
| 99 | + if ("stopWords".equals(currentFieldName)) { |
| 100 | + Set<String> stopWords = Sets.newHashSet(); |
| 101 | + while ((token = jp.nextToken()) != JsonToken.END_ARRAY) { |
| 102 | + stopWords.add(jp.getText()); |
| 103 | + } |
| 104 | + mltQuery.setStopWords(stopWords); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (mltQuery.getLikeText() == null) { |
| 110 | + throw new QueryParsingException(index, "moreLikeThisField requires 'likeText' to be specified"); |
| 111 | + } |
| 112 | + |
| 113 | + // move to the next end object, to close the field name |
| 114 | + token = jp.nextToken(); |
| 115 | + assert token == JsonToken.END_OBJECT; |
| 116 | + |
| 117 | + mltQuery.setAnalyzer(parseContext.mapperService().searchAnalyzer()); |
| 118 | + MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); |
| 119 | + if (smartNameFieldMappers != null) { |
| 120 | + if (smartNameFieldMappers.hasMapper()) { |
| 121 | + fieldName = smartNameFieldMappers.mapper().names().indexName(); |
| 122 | + mltQuery.setAnalyzer(smartNameFieldMappers.mapper().searchAnalyzer()); |
| 123 | + } |
| 124 | + } |
| 125 | + mltQuery.setMoreLikeFields(new String[]{fieldName}); |
| 126 | + return wrapSmartNameQuery(mltQuery, smartNameFieldMappers, parseContext.filterCache()); |
| 127 | + } |
| 128 | +} |
0 commit comments