Search in sources :

Example 6 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project neo4j by neo4j.

the class DocValuesCollectorTest method indexReaderWithMaxDocs.

private IndexReaderStub indexReaderWithMaxDocs(int maxDocs) {
    NumericDocValues identityValues = new NumericDocValues() {

        @Override
        public long get(int docID) {
            return docID;
        }
    };
    IndexReaderStub stub = new IndexReaderStub(identityValues);
    stub.setElements(new String[maxDocs]);
    return stub;
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) IndexReaderStub(org.neo4j.kernel.api.impl.index.IndexReaderStub)

Example 7 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.

the class MultiValueModeTests method verify.

private void verify(SortedNumericDocValues values, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs) throws IOException {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
            final NumericDocValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc);
            int prevRoot = -1;
            for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {
                final long actual = selected.get(root);
                long expected = 0;
                if (mode == MultiValueMode.MAX) {
                    expected = Long.MIN_VALUE;
                } else if (mode == MultiValueMode.MIN) {
                    expected = Long.MAX_VALUE;
                }
                int numValues = 0;
                for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) {
                    values.setDocument(child);
                    for (int j = 0; j < values.count(); ++j) {
                        if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                            expected += values.valueAt(j);
                        } else if (mode == MultiValueMode.MIN) {
                            expected = Math.min(expected, values.valueAt(j));
                        } else if (mode == MultiValueMode.MAX) {
                            expected = Math.max(expected, values.valueAt(j));
                        }
                        ++numValues;
                    }
                }
                if (numValues == 0) {
                    expected = missingValue;
                } else if (mode == MultiValueMode.AVG) {
                    expected = numValues > 1 ? Math.round((double) expected / (double) numValues) : expected;
                }
                assertEquals(mode.toString() + " docId=" + root, expected, actual);
                prevRoot = root;
            }
        }
    }
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) BitSetIterator(org.apache.lucene.util.BitSetIterator)

Example 8 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project lucene-solr by apache.

the class EnumFieldSource method getValues.

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

        final MutableValueInt val = new MutableValueInt();

        int lastDocID;

        private int getValueForDoc(int doc) throws IOException {
            if (doc < lastDocID) {
                throw new AssertionError("docs were sent out-of-order: lastDocID=" + lastDocID + " vs doc=" + 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 {
            Integer intValue = intVal(doc);
            return intValueToStringValue(intValue);
        }

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

        @Override
        public ValueSourceScorer getRangeScorer(LeafReaderContext readerContext, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
            Integer lower = stringValueToIntValue(lowerVal);
            Integer upper = stringValueToIntValue(upperVal);
            if (lower == null) {
                lower = Integer.MIN_VALUE;
            } else {
                if (!includeLower && lower < Integer.MAX_VALUE)
                    lower++;
            }
            if (upper == null) {
                upper = Integer.MAX_VALUE;
            } else {
                if (!includeUpper && upper > Integer.MIN_VALUE)
                    upper--;
            }
            final int ll = lower;
            final int uu = upper;
            return new ValueSourceScorer(readerContext, this) {

                @Override
                public boolean matches(int doc) throws IOException {
                    if (!exists(doc))
                        return false;
                    int val = intVal(doc);
                    return val >= ll && val <= uu;
                }
            };
        }

        @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 = intVal(doc);
                    mval.exists = arr.docID() == doc;
                }
            };
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) ValueSourceScorer(org.apache.lucene.queries.function.ValueSourceScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues)

Example 9 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project lucene-solr by apache.

the class LongFieldSource method getValues.

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

        int lastDocID;

        private long 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 arr.longValue();
            } else {
                return 0;
            }
        }

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

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

        @Override
        public Object objectVal(int doc) throws IOException {
            long value = getValueForDoc(doc);
            if (arr.docID() == doc) {
                return longToObject(value);
            } else {
                return null;
            }
        }

        @Override
        public String strVal(int doc) throws IOException {
            long value = getValueForDoc(doc);
            if (arr.docID() == doc) {
                return longToString(value);
            } else {
                return null;
            }
        }

        @Override
        protected long externalToLong(String extVal) {
            return LongFieldSource.this.externalToLong(extVal);
        }

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

                private final MutableValueLong mval = newMutableValueLong();

                @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) MutableValueLong(org.apache.lucene.util.mutable.MutableValueLong) LongDocValues(org.apache.lucene.queries.function.docvalues.LongDocValues)

Example 10 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project lucene-solr by apache.

the class BaseGeoPointTestCase method verifyRandomRectangles.

protected void verifyRandomRectangles(double[] lats, double[] lons) throws Exception {
    IndexWriterConfig iwc = newIndexWriterConfig();
    // Else seeds may not reproduce:
    iwc.setMergeScheduler(new SerialMergeScheduler());
    // Else we can get O(N^2) merging:
    int mbd = iwc.getMaxBufferedDocs();
    if (mbd != -1 && mbd < lats.length / 100) {
        iwc.setMaxBufferedDocs(lats.length / 100);
    }
    Directory dir;
    if (lats.length > 100000) {
        dir = newFSDirectory(createTempDir(getClass().getSimpleName()));
    } else {
        dir = newDirectory();
    }
    Set<Integer> deleted = new HashSet<>();
    // RandomIndexWriter is too slow here:
    IndexWriter w = new IndexWriter(dir, iwc);
    for (int id = 0; id < lats.length; id++) {
        Document doc = new Document();
        doc.add(newStringField("id", "" + id, Field.Store.NO));
        doc.add(new NumericDocValuesField("id", id));
        if (Double.isNaN(lats[id]) == false) {
            addPointToDoc(FIELD_NAME, doc, lats[id], lons[id]);
        }
        w.addDocument(doc);
        if (id > 0 && random().nextInt(100) == 42) {
            int idToDelete = random().nextInt(id);
            w.deleteDocuments(new Term("id", "" + idToDelete));
            deleted.add(idToDelete);
            if (VERBOSE) {
                System.out.println("  delete id=" + idToDelete);
            }
        }
    }
    if (random().nextBoolean()) {
        w.forceMerge(1);
    }
    final IndexReader r = DirectoryReader.open(w);
    w.close();
    IndexSearcher s = newSearcher(r);
    int iters = atLeast(25);
    Bits liveDocs = MultiFields.getLiveDocs(s.getIndexReader());
    int maxDoc = s.getIndexReader().maxDoc();
    for (int iter = 0; iter < iters; iter++) {
        if (VERBOSE) {
            System.out.println("\nTEST: iter=" + iter + " s=" + s);
        }
        Rectangle rect = nextBox();
        Query query = newRectQuery(FIELD_NAME, rect.minLat, rect.maxLat, rect.minLon, rect.maxLon);
        if (VERBOSE) {
            System.out.println("  query=" + query);
        }
        final FixedBitSet hits = new FixedBitSet(maxDoc);
        s.search(query, new SimpleCollector() {

            private int docBase;

            @Override
            public boolean needsScores() {
                return false;
            }

            @Override
            protected void doSetNextReader(LeafReaderContext context) throws IOException {
                docBase = context.docBase;
            }

            @Override
            public void collect(int doc) {
                hits.set(docBase + doc);
            }
        });
        boolean fail = false;
        NumericDocValues docIDToID = MultiDocValues.getNumericValues(r, "id");
        for (int docID = 0; docID < maxDoc; docID++) {
            assertEquals(docID, docIDToID.nextDoc());
            int id = (int) docIDToID.longValue();
            boolean expected;
            if (liveDocs != null && liveDocs.get(docID) == false) {
                // document is deleted
                expected = false;
            } else if (Double.isNaN(lats[id])) {
                expected = false;
            } else {
                expected = rectContainsPoint(rect, lats[id], lons[id]);
            }
            if (hits.get(docID) != expected) {
                StringBuilder b = new StringBuilder();
                b.append("docID=(" + docID + ")\n");
                if (expected) {
                    b.append("FAIL: id=" + id + " should match but did not\n");
                } else {
                    b.append("FAIL: id=" + id + " should not match but did\n");
                }
                b.append("  box=" + rect + "\n");
                b.append("  query=" + query + " docID=" + docID + "\n");
                b.append("  lat=" + lats[id] + " lon=" + lons[id] + "\n");
                b.append("  deleted?=" + (liveDocs != null && liveDocs.get(docID) == false));
                if (true) {
                    fail("wrong hit (first of possibly more):\n\n" + b);
                } else {
                    System.out.println(b.toString());
                    fail = true;
                }
            }
        }
        if (fail) {
            fail("some hits were wrong");
        }
    }
    IOUtils.close(r, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NumericDocValues(org.apache.lucene.index.NumericDocValues) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) SerialMergeScheduler(org.apache.lucene.index.SerialMergeScheduler) SimpleCollector(org.apache.lucene.search.SimpleCollector) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) FixedBitSet(org.apache.lucene.util.FixedBitSet) IndexReader(org.apache.lucene.index.IndexReader) Bits(org.apache.lucene.util.Bits) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Aggregations

NumericDocValues (org.apache.lucene.index.NumericDocValues)81 Document (org.apache.lucene.document.Document)30 Directory (org.apache.lucene.store.Directory)29 LeafReader (org.apache.lucene.index.LeafReader)25 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)25 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)23 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)22 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)22 IOException (java.io.IOException)20 BytesRef (org.apache.lucene.util.BytesRef)19 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)17 HashSet (java.util.HashSet)16 Bits (org.apache.lucene.util.Bits)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)15 SortedDocValues (org.apache.lucene.index.SortedDocValues)15 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)14 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12 IndexReader (org.apache.lucene.index.IndexReader)12 Term (org.apache.lucene.index.Term)12