gerlowskija commented on code in PR #3922: URL: https://github.com/apache/solr/pull/3922#discussion_r3095927653
########## solr/core/src/java/org/apache/solr/schema/IntField.java: ########## @@ -0,0 +1,331 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.solr.schema; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.IntPoint; +import org.apache.lucene.document.InvertableType; +import org.apache.lucene.document.SortedNumericDocValuesField; +import org.apache.lucene.document.StoredField; +import org.apache.lucene.document.StoredValue; +import org.apache.lucene.index.DocValuesType; +import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.queries.function.ValueSource; +import org.apache.lucene.queries.function.valuesource.MultiValuedIntFieldSource; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.SortedNumericSelector; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; +import org.apache.solr.common.SolrException; +import org.apache.solr.search.QParser; +import org.apache.solr.uninverting.UninvertingReader.Type; + +/** + * An {@code NumericField} implementation of a field for {@code Int} values using {@code IntPoint}, {@code StringField}, {@code SortedNumericDocValuesField} and {@code StoredField}. + * + * @see PointField + * @see IntPoint + */ +public class IntField extends NumericField implements IntValueFieldType { + + public IntField() { + type = NumberType.INTEGER; + } + + @Override + public Object toNativeType(Object val) { + if (val == null) return null; + if (val instanceof Number) return ((Number) val).intValue(); + if (val instanceof CharSequence) return Integer.parseInt(val.toString()); + return super.toNativeType(val); + } + + @Override + public Query getPointFieldQuery(QParser parser, SchemaField field, String value) { + return IntPoint.newExactQuery(field.getName(), parseIntFromUser(field.getName(), value)); + } + + @Override + public Query getDocValuesFieldQuery(QParser parser, SchemaField field, String value) { + return SortedNumericDocValuesField.newSlowExactQuery(field.getName(), parseIntFromUser(field.getName(), value)); + } + + @Override + public Query getPointRangeQuery( + QParser parser, + SchemaField field, + String min, + String max, + boolean minInclusive, + boolean maxInclusive) { + int actualMin, actualMax; + if (min == null) { + actualMin = Integer.MIN_VALUE; + } else { + actualMin = parseIntFromUser(field.getName(), min); + if (!minInclusive) { + if (actualMin == Integer.MAX_VALUE) return new MatchNoDocsQuery(); + ++actualMin; + } + } + if (max == null) { + actualMax = Integer.MAX_VALUE; + } else { + actualMax = parseIntFromUser(field.getName(), max); + if (!maxInclusive) { + if (actualMax == Integer.MIN_VALUE) return new MatchNoDocsQuery(); + --actualMax; + } + } + return IntPoint.newRangeQuery(field.getName(), actualMin, actualMax); + } + + @Override + public Query getDocValuesRangeQuery( + QParser parser, + SchemaField field, + String min, + String max, + boolean minInclusive, + boolean maxInclusive) { + int actualMin, actualMax; + if (min == null) { + actualMin = Integer.MIN_VALUE; + } else { + actualMin = parseIntFromUser(field.getName(), min); + if (!minInclusive) { + if (actualMin == Integer.MAX_VALUE) return new MatchNoDocsQuery(); + ++actualMin; + } + } + if (max == null) { + actualMax = Integer.MAX_VALUE; + } else { + actualMax = parseIntFromUser(field.getName(), max); + if (!maxInclusive) { + if (actualMax == Integer.MIN_VALUE) return new MatchNoDocsQuery(); + --actualMax; + } + } + return SortedNumericDocValuesField.newSlowRangeQuery(field.getName(), actualMin, actualMax); + } + + @Override + public Query getPointSetQuery(QParser parser, SchemaField field, Collection<String> externalVals) { + int[] values = new int[externalVals.size()]; + int i = 0; + for (String val : externalVals) { + values[i++] = parseIntFromUser(field.getName(), val); + } + return IntPoint.newSetQuery(field.getName(), values); + } + + @Override + public Query getDocValuesSetQuery(QParser parser, SchemaField field, Collection<String> externalVals) { + long[] points = new long[externalVals.size()]; + int i = 0; + for (String val : externalVals) { + points[i++] = parseIntFromUser(field.getName(), val); + } + return SortedNumericDocValuesField.newSlowSetQuery(field.getName(), points); + } + + @Override + public Object toObject(SchemaField sf, BytesRef term) { + return IntPoint.decodeDimension(term.bytes, term.offset); + } + + @Override + public Object toObject(IndexableField f) { + final StoredValue storedValue = f.storedValue(); + if (storedValue != null) { + return storedValue.getIntValue(); + } + final Number val = f.numericValue(); + if (val != null) { + return val.intValue(); + } else { + throw new AssertionError("Unexpected state. Field: '" + f + "'"); + } + } + + @Override + public String storedToReadable(IndexableField f) { + return Integer.toString(f.storedValue().getIntValue()); + } + + @Override + protected String indexedToReadable(BytesRef indexedForm) { + return Integer.toString(IntPoint.decodeDimension(indexedForm.bytes, indexedForm.offset)); + } + + @Override + public void readableToIndexed(CharSequence val, BytesRefBuilder result) { + result.grow(Integer.BYTES); + result.setLength(Integer.BYTES); + IntPoint.encodeDimension(parseIntFromUser(null, val.toString()), result.bytes(), 0); + } + + @Override + public Type getUninversionType(SchemaField sf) { + if (sf.multiValued()) { + return null; + } else { + return Type.INTEGER_POINT; + } + } + + @Override + public ValueSource getValueSource(SchemaField field, QParser qparser) { + field.checkFieldCacheSource(); + return new MultiValuedIntFieldSource(field.getName(), SortedNumericSelector.Type.MIN); + } + + @Override + protected ValueSource getSingleValueSource(SortedNumericSelector.Type choice, SchemaField f) { + return new MultiValuedIntFieldSource(f.getName(), choice); + } + + @Override + public List<IndexableField> createFields(SchemaField sf, Object value) { + int intValue = + (value instanceof Number) + ? ((Number) value).intValue() + : Integer.parseInt(value.toString()); + return Collections.singletonList(new SolrIntField(sf.getName(), intValue, sf.indexed(), sf.enhancedIndex(), sf.hasDocValues(), sf.stored())); Review Comment: [Q] Is there any efficiency or other difference between returning a true list of multiple instances from this method vs. returning a single field instance? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
