Search in sources :

Example 6 with IntDocValues

use of org.apache.lucene.queries.function.docvalues.IntDocValues in project lucene-solr by apache.

the class IntFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final NumericDocValues arr = getNumericDocValues(context, readerContext);
    return new IntDocValues(this) {

        int lastDocID;

        private int getValueForDoc(int doc) throws IOException {
            if (doc < lastDocID) {
                throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
            }
            lastDocID = doc;
            int curDocID = arr.docID();
            if (doc > curDocID) {
                curDocID = arr.advance(doc);
            }
            if (doc == curDocID) {
                return (int) arr.longValue();
            } else {
                return 0;
            }
        }

        @Override
        public int intVal(int doc) throws IOException {
            return getValueForDoc(doc);
        }

        @Override
        public String strVal(int doc) throws IOException {
            return Integer.toString(intVal(doc));
        }

        @Override
        public boolean exists(int doc) throws IOException {
            getValueForDoc(doc);
            return arr.docID() == doc;
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {

                private final MutableValueInt mval = new MutableValueInt();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) throws IOException {
                    mval.value = getValueForDoc(doc);
                    mval.exists = arr.docID() == doc;
                }
            };
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues)

Example 7 with IntDocValues

use of org.apache.lucene.queries.function.docvalues.IntDocValues in project lucene-solr by apache.

the class OrdFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final int off = readerContext.docBase;
    final LeafReader r;
    Object o = context.get("searcher");
    if (o instanceof SolrIndexSearcher) {
        SolrIndexSearcher is = (SolrIndexSearcher) o;
        SchemaField sf = is.getSchema().getFieldOrNull(field);
        if (sf != null && sf.hasDocValues() == false && sf.multiValued() == false && sf.getType().getNumberType() != null) {
            // it's a single-valued numeric field: we must currently create insanity :(
            List<LeafReaderContext> leaves = is.getIndexReader().leaves();
            LeafReader[] insaneLeaves = new LeafReader[leaves.size()];
            int upto = 0;
            for (LeafReaderContext raw : leaves) {
                insaneLeaves[upto++] = Insanity.wrapInsanity(raw.reader(), field);
            }
            r = SlowCompositeReaderWrapper.wrap(new MultiReader(insaneLeaves));
        } else {
            // reuse ordinalmap
            r = ((SolrIndexSearcher) o).getSlowAtomicReader();
        }
    } else {
        IndexReader topReader = ReaderUtil.getTopLevelContext(readerContext).reader();
        r = SlowCompositeReaderWrapper.wrap(topReader);
    }
    // if it's e.g. tokenized/multivalued, emulate old behavior of single-valued fc
    final SortedDocValues sindex = SortedSetSelector.wrap(DocValues.getSortedSet(r, field), SortedSetSelector.Type.MIN);
    return new IntDocValues(this) {

        private int lastDocID;

        private int getOrdForDoc(int docID) throws IOException {
            if (docID < lastDocID) {
                throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
            }
            if (docID > sindex.docID()) {
                sindex.advance(docID);
            }
            if (docID == sindex.docID()) {
                return sindex.ordValue();
            } else {
                return -1;
            }
        }

        protected String toTerm(String readableValue) {
            return readableValue;
        }

        @Override
        public int intVal(int doc) throws IOException {
            return getOrdForDoc(doc + off);
        }

        @Override
        public int ordVal(int doc) throws IOException {
            return getOrdForDoc(doc + off);
        }

        @Override
        public int numOrd() {
            return sindex.getValueCount();
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return getOrdForDoc(doc + off) != 0;
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {

                private final MutableValueInt mval = new MutableValueInt();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) throws IOException {
                    mval.value = getOrdForDoc(doc);
                    mval.exists = mval.value != 0;
                }
            };
        }
    };
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) MultiReader(org.apache.lucene.index.MultiReader) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SortedDocValues(org.apache.lucene.index.SortedDocValues) SchemaField(org.apache.solr.schema.SchemaField) IndexReader(org.apache.lucene.index.IndexReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues)

Aggregations

IntDocValues (org.apache.lucene.queries.function.docvalues.IntDocValues)7 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)4 MutableValueInt (org.apache.lucene.util.mutable.MutableValueInt)4 IndexReader (org.apache.lucene.index.IndexReader)3 SortedDocValues (org.apache.lucene.index.SortedDocValues)3 BytesRef (org.apache.lucene.util.BytesRef)3 LeafReader (org.apache.lucene.index.LeafReader)2 MultiReader (org.apache.lucene.index.MultiReader)2 NumericDocValues (org.apache.lucene.index.NumericDocValues)2 Terms (org.apache.lucene.index.Terms)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 SchemaField (org.apache.solr.schema.SchemaField)2 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)2 IOException (java.io.IOException)1 Map (java.util.Map)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 Fields (org.apache.lucene.index.Fields)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)1 ValueSourceScorer (org.apache.lucene.queries.function.ValueSourceScorer)1