/* * Licensed to ElasticSearch and Shay Banon under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. ElasticSearch licenses this * file to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.elasticsearch.index.mapper.core; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.search.Filter; import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; import java.util.Map; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeShortValue; import static org.elasticsearch.index.mapper.MapperBuilders.shortField; import static org.elasticsearch.index.mapper.core.TypeParsers.parseNumberField; /** * */ public class ShortFieldMapper extends NumberFieldMapper<Short> { public static final String CONTENT_TYPE = "short"; public static class Defaults extends NumberFieldMapper.Defaults { public static final Short NULL_VALUE = null; } public static class Builder extends NumberFieldMapper.Builder<Builder, ShortFieldMapper> { protected Short nullValue = Defaults.NULL_VALUE; public Builder(String name) { super(name); builder = this; } public Builder nullValue(short nullValue) { this.nullValue = nullValue; return this; } @Override public ShortFieldMapper build(BuilderContext context) { ShortFieldMapper fieldMapper = new ShortFieldMapper(buildNames(context), precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, ignoreMalformed(context)); fieldMapper.includeInAll(includeInAll); return fieldMapper; } } public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException { ShortFieldMapper.Builder builder = shortField(name); parseNumberField(builder, name, node, parserContext); for (Map.Entry<String, Object> entry : node.entrySet()) { String propName = Strings.toUnderscoreCase(entry.getKey()); Object propNode = entry.getValue(); if (propName.equals("null_value")) { builder.nullValue(nodeShortValue(propNode)); } } return builder; } } private Short nullValue; private String nullValueAsString; protected ShortFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, float boost, boolean omitNorms, IndexOptions indexOptions, Short nullValue, Explicit<Boolean> ignoreMalformed) { super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, ignoreMalformed, new NamedAnalyzer("_short/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)), new NamedAnalyzer("_short/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE))); this.nullValue = nullValue; this.nullValueAsString = nullValue == null ? null : nullValue.toString(); } @Override protected int maxPrecisionStep() { return 32; } @Override public Short value(Fieldable field) { byte[] value = field.getBinaryValue(); if (value == null) { return null; } return Numbers.bytesToShort(value); } @Override public Short valueFromString(String value) { return Short.valueOf(value); } @Override public String indexedValue(String value) { return NumericUtils.intToPrefixCoded(Short.parseShort(value)); } @Override public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { short iValue = Short.parseShort(value); short iSim; try { iSim = Short.parseShort(minSim); } catch (NumberFormatException e) { iSim = (short) Float.parseFloat(minSim); } return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); } @Override public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { short iValue = Short.parseShort(value); short iSim = (short) (minSim * dFuzzyFactor); return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); } @Override public Query fieldQuery(String value, @Nullable QueryParseContext context) { int iValue = Integer.parseInt(value); return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, iValue, iValue, true, true); } @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, lowerTerm == null ? null : Integer.parseInt(lowerTerm), upperTerm == null ? null : Integer.parseInt(upperTerm), includeLower, includeUpper); } @Override public Filter fieldFilter(String value, @Nullable QueryParseContext context) { int iValue = Integer.parseInt(value); return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, iValue, iValue, true, true); } @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, lowerTerm == null ? null : Integer.parseInt(lowerTerm), upperTerm == null ? null : Integer.parseInt(upperTerm), includeLower, includeUpper); } @Override public Filter rangeFilter(FieldDataCache fieldDataCache, String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { return NumericRangeFieldDataFilter.newShortRange(fieldDataCache, names.indexName(), lowerTerm == null ? null : Short.parseShort(lowerTerm), upperTerm == null ? null : Short.parseShort(upperTerm), includeLower, includeUpper); } @Override public Filter nullValueFilter() { if (nullValue == null) { return null; } return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, nullValue.intValue(), nullValue.intValue(), true, true); } @Override protected boolean customBoost() { return true; } @Override protected Fieldable innerParseCreateField(ParseContext context) throws IOException { short value; float boost = this.boost; if (context.externalValueSet()) { Object externalValue = context.externalValue(); if (externalValue == null) { if (nullValue == null) { return null; } value = nullValue; } else if (externalValue instanceof String) { String sExternalValue = (String) externalValue; if (sExternalValue.length() == 0) { if (nullValue == null) { return null; } value = nullValue; } else { value = Short.parseShort(sExternalValue); } } else { value = ((Number) externalValue).shortValue(); } if (context.includeInAll(includeInAll, this)) { context.allEntries().addText(names.fullName(), Short.toString(value), boost); } } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL || (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { if (nullValue == null) { return null; } value = nullValue; if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { context.allEntries().addText(names.fullName(), nullValueAsString, boost); } } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { XContentParser.Token token; String currentFieldName = null; Short objValue = nullValue; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) { if (parser.currentToken() != XContentParser.Token.VALUE_NULL) { objValue = parser.shortValue(); } } else if ("boost".equals(currentFieldName) || "_boost".equals(currentFieldName)) { boost = parser.floatValue(); } } } if (objValue == null) { // no value return null; } value = objValue; } else { value = parser.shortValue(); if (context.includeInAll(includeInAll, this)) { context.allEntries().addText(names.fullName(), parser.text(), boost); } } } CustomShortNumericField field = new CustomShortNumericField(this, value); field.setBoost(boost); return field; } @Override public FieldDataType fieldDataType() { return FieldDataType.DefaultTypes.SHORT; } @Override protected String contentType() { return CONTENT_TYPE; } @Override public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException { super.merge(mergeWith, mergeContext); if (!this.getClass().equals(mergeWith.getClass())) { return; } if (!mergeContext.mergeFlags().simulate()) { this.nullValue = ((ShortFieldMapper) mergeWith).nullValue; this.nullValueAsString = ((ShortFieldMapper) mergeWith).nullValueAsString; } } @Override protected void doXContentBody(XContentBuilder builder) throws IOException { super.doXContentBody(builder); if (index != Defaults.INDEX) { builder.field("index", index.name().toLowerCase()); } if (store != Defaults.STORE) { builder.field("store", store.name().toLowerCase()); } if (termVector != Defaults.TERM_VECTOR) { builder.field("term_vector", termVector.name().toLowerCase()); } if (omitNorms != Defaults.OMIT_NORMS) { builder.field("omit_norms", omitNorms); } if (indexOptions != Defaults.INDEX_OPTIONS) { builder.field("index_options", indexOptionToString(indexOptions)); } if (precisionStep != Defaults.PRECISION_STEP) { builder.field("precision_step", precisionStep); } if (fuzzyFactor != Defaults.FUZZY_FACTOR) { builder.field("fuzzy_factor", fuzzyFactor); } if (nullValue != null) { builder.field("null_value", nullValue); } if (includeInAll != null) { builder.field("include_in_all", includeInAll); } } public static class CustomShortNumericField extends CustomNumericField { private final short number; private final NumberFieldMapper mapper; public CustomShortNumericField(NumberFieldMapper mapper, short number) { super(mapper, mapper.stored() ? Numbers.shortToBytes(number) : null); this.mapper = mapper; this.number = number; } @Override public TokenStream tokenStreamValue() { if (isIndexed) { return mapper.popCachedStream().setIntValue(number); } return null; } @Override public String numericAsString() { return Short.toString(number); } } }