Search in sources :

Example 6 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestNumericDocValuesUpdates method testUpdateSegmentWithNoDocValues2.

@Test
public void testUpdateSegmentWithNoDocValues2() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    // prevent merges, otherwise by the time updates are applied
    // (writer.close()), the segments might have merged and that update becomes
    // legit.
    conf.setMergePolicy(NoMergePolicy.INSTANCE);
    IndexWriter writer = new IndexWriter(dir, conf);
    // first segment with NDV
    Document doc = new Document();
    doc.add(new StringField("id", "doc0", Store.NO));
    doc.add(new NumericDocValuesField("ndv", 3));
    writer.addDocument(doc);
    doc = new Document();
    // document without 'ndv' field
    doc.add(new StringField("id", "doc4", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    // second segment with no NDV, but another dv field "foo"
    doc = new Document();
    doc.add(new StringField("id", "doc1", Store.NO));
    doc.add(new NumericDocValuesField("foo", 3));
    writer.addDocument(doc);
    doc = new Document();
    // document that isn't updated
    doc.add(new StringField("id", "doc2", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    // update document in the first segment - should not affect docsWithField of
    // the document without NDV field
    writer.updateNumericDocValue(new Term("id", "doc0"), "ndv", 5L);
    // update document in the second segment - field should be added and we should
    // be able to handle the other document correctly (e.g. no NPE)
    writer.updateNumericDocValue(new Term("id", "doc1"), "ndv", 5L);
    writer.close();
    DirectoryReader reader = DirectoryReader.open(dir);
    for (LeafReaderContext context : reader.leaves()) {
        LeafReader r = context.reader();
        NumericDocValues ndv = r.getNumericDocValues("ndv");
        assertEquals(0, ndv.nextDoc());
        assertEquals(5L, ndv.longValue());
        assertTrue(ndv.nextDoc() > 1);
    }
    reader.close();
    TestUtil.checkIndex(dir);
    conf = newIndexWriterConfig(new MockAnalyzer(random()));
    writer = new IndexWriter(dir, conf);
    writer.forceMerge(1);
    writer.close();
    reader = DirectoryReader.open(dir);
    LeafReader ar = getOnlyLeafReader(reader);
    assertEquals(DocValuesType.NUMERIC, ar.getFieldInfos().fieldInfo("foo").getDocValuesType());
    IndexSearcher searcher = new IndexSearcher(reader);
    TopFieldDocs td;
    // doc0
    td = searcher.search(new TermQuery(new Term("id", "doc0")), 1, new Sort(new SortField("ndv", SortField.Type.LONG)));
    assertEquals(5L, ((FieldDoc) td.scoreDocs[0]).fields[0]);
    // doc1
    td = searcher.search(new TermQuery(new Term("id", "doc1")), 1, new Sort(new SortField("ndv", SortField.Type.LONG), new SortField("foo", SortField.Type.LONG)));
    assertEquals(5L, ((FieldDoc) td.scoreDocs[0]).fields[0]);
    assertEquals(3L, ((FieldDoc) td.scoreDocs[0]).fields[1]);
    // doc2
    td = searcher.search(new TermQuery(new Term("id", "doc2")), 1, new Sort(new SortField("ndv", SortField.Type.LONG)));
    assertEquals(0L, ((FieldDoc) td.scoreDocs[0]).fields[0]);
    // doc4
    td = searcher.search(new TermQuery(new Term("id", "doc4")), 1, new Sort(new SortField("ndv", SortField.Type.LONG)));
    assertEquals(0L, ((FieldDoc) td.scoreDocs[0]).fields[0]);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FieldDoc(org.apache.lucene.search.FieldDoc) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory) Test(org.junit.Test)

Example 7 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class LatLonPoint method nearest.

/**
   * Finds the {@code n} nearest indexed points to the provided point, according to Haversine distance.
   * <p>
   * This is functionally equivalent to running {@link MatchAllDocsQuery} with a {@link LatLonDocValuesField#newDistanceSort},
   * but is far more efficient since it takes advantage of properties the indexed BKD tree.  Currently this
   * only works with {@link Lucene60PointsFormat} (used by the default codec).  Multi-valued fields are
   * currently not de-duplicated, so if a document had multiple instances of the specified field that
   * make it into the top n, that document will appear more than once.
   * <p>
   * Documents are ordered by ascending distance from the location. The value returned in {@link FieldDoc} for
   * the hits contains a Double instance with the distance in meters.
   * 
   * @param searcher IndexSearcher to find nearest points from.
   * @param field field name. must not be null.
   * @param latitude latitude at the center: must be within standard +/-90 coordinate bounds.
   * @param longitude longitude at the center: must be within standard +/-180 coordinate bounds.
   * @param n the number of nearest neighbors to retrieve.
   * @return TopFieldDocs containing documents ordered by distance, where the field value for each {@link FieldDoc} is the distance in meters
   * @throws IllegalArgumentException if the underlying PointValues is not a {@code Lucene60PointsReader} (this is a current limitation), or
   *         if {@code field} or {@code searcher} is null, or if {@code latitude}, {@code longitude} or {@code n} are out-of-bounds
   * @throws IOException if an IOException occurs while finding the points.
   */
// TODO: what about multi-valued documents? what happens?
public static TopFieldDocs nearest(IndexSearcher searcher, String field, double latitude, double longitude, int n) throws IOException {
    GeoUtils.checkLatitude(latitude);
    GeoUtils.checkLongitude(longitude);
    if (n < 1) {
        throw new IllegalArgumentException("n must be at least 1; got " + n);
    }
    if (field == null) {
        throw new IllegalArgumentException("field must not be null");
    }
    if (searcher == null) {
        throw new IllegalArgumentException("searcher must not be null");
    }
    List<BKDReader> readers = new ArrayList<>();
    List<Integer> docBases = new ArrayList<>();
    List<Bits> liveDocs = new ArrayList<>();
    int totalHits = 0;
    for (LeafReaderContext leaf : searcher.getIndexReader().leaves()) {
        PointValues points = leaf.reader().getPointValues(field);
        if (points != null) {
            if (points instanceof BKDReader == false) {
                throw new IllegalArgumentException("can only run on Lucene60PointsReader points implementation, but got " + points);
            }
            totalHits += points.getDocCount();
            BKDReader reader = (BKDReader) points;
            if (reader != null) {
                readers.add(reader);
                docBases.add(leaf.docBase);
                liveDocs.add(leaf.reader().getLiveDocs());
            }
        }
    }
    NearestNeighbor.NearestHit[] hits = NearestNeighbor.nearest(latitude, longitude, readers, liveDocs, docBases, n);
    // Convert to TopFieldDocs:
    ScoreDoc[] scoreDocs = new ScoreDoc[hits.length];
    for (int i = 0; i < hits.length; i++) {
        NearestNeighbor.NearestHit hit = hits[i];
        scoreDocs[i] = new FieldDoc(hit.docID, 0.0f, new Object[] { Double.valueOf(hit.distanceMeters) });
    }
    return new TopFieldDocs(totalHits, scoreDocs, null, 0.0f);
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) BKDReader(org.apache.lucene.util.bkd.BKDReader) PointValues(org.apache.lucene.index.PointValues) Bits(org.apache.lucene.util.Bits) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 8 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class AnalyzingInfixSuggester method createResults.

/**
   * Create the results based on the search hits.
   * Can be overridden by subclass to add particular behavior (e.g. weight transformation).
   * Note that there is no prefix toke (the {@code prefixToken} argument will
   * be null) whenever the final token in the incoming request was in fact finished
   * (had trailing characters, such as white-space).
   *
   * @throws IOException If there are problems reading fields from the underlying Lucene index.
   */
protected List<LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num, CharSequence charSequence, boolean doHighlight, Set<String> matchedTokens, String prefixToken) throws IOException {
    List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
    List<LookupResult> results = new ArrayList<>();
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        FieldDoc fd = (FieldDoc) hits.scoreDocs[i];
        BinaryDocValues textDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), TEXT_FIELD_NAME);
        textDV.advance(fd.doc);
        BytesRef term = textDV.binaryValue();
        String text = term.utf8ToString();
        long score = (Long) fd.fields[0];
        // This will just be null if app didn't pass payloads to build():
        // TODO: maybe just stored fields?  they compress...
        BinaryDocValues payloadsDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), "payloads");
        BytesRef payload;
        if (payloadsDV != null) {
            if (payloadsDV.advance(fd.doc) == fd.doc) {
                payload = BytesRef.deepCopyOf(payloadsDV.binaryValue());
            } else {
                payload = new BytesRef(BytesRef.EMPTY_BYTES);
            }
        } else {
            payload = null;
        }
        // Must look up sorted-set by segment:
        int segment = ReaderUtil.subIndex(fd.doc, leaves);
        SortedSetDocValues contextsDV = leaves.get(segment).reader().getSortedSetDocValues(CONTEXTS_FIELD_NAME);
        Set<BytesRef> contexts;
        if (contextsDV != null) {
            contexts = new HashSet<BytesRef>();
            int targetDocID = fd.doc - leaves.get(segment).docBase;
            if (contextsDV.advance(targetDocID) == targetDocID) {
                long ord;
                while ((ord = contextsDV.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
                    BytesRef context = BytesRef.deepCopyOf(contextsDV.lookupOrd(ord));
                    contexts.add(context);
                }
            }
        } else {
            contexts = null;
        }
        LookupResult result;
        if (doHighlight) {
            result = new LookupResult(text, highlight(text, matchedTokens, prefixToken), score, payload, contexts);
        } else {
            result = new LookupResult(text, score, payload, contexts);
        }
        results.add(result);
    }
    return results;
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) ArrayList(java.util.ArrayList) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestDemoExpressions method testDollarVariable.

/** Uses variables with $ */
public void testDollarVariable() throws Exception {
    Expression expr = JavascriptCompiler.compile("$0+$score");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("$0", SortField.Type.SCORE));
    bindings.add(new SortField("$score", SortField.Type.SCORE));
    Sort sort = new Sort(expr.getSortField(bindings, true));
    Query query = new TermQuery(new Term("body", "contents"));
    TopFieldDocs td = searcher.search(query, 3, sort, true, true);
    for (int i = 0; i < 3; i++) {
        FieldDoc d = (FieldDoc) td.scoreDocs[i];
        float expected = 2 * d.score;
        float actual = ((Double) d.fields[0]).floatValue();
        assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) FieldDoc(org.apache.lucene.search.FieldDoc) Sort(org.apache.lucene.search.Sort) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Example 10 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestDemoExpressions method testStaticExtendedVariableExample.

public void testStaticExtendedVariableExample() throws Exception {
    Expression popularity = JavascriptCompiler.compile("doc[\"popularity\"].value");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add("doc['popularity'].value", DoubleValuesSource.fromIntField("popularity"));
    Sort sort = new Sort(popularity.getSortField(bindings, true));
    TopFieldDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
    FieldDoc d = (FieldDoc) td.scoreDocs[0];
    assertEquals(20D, (Double) d.fields[0], 1E-4);
    d = (FieldDoc) td.scoreDocs[1];
    assertEquals(5D, (Double) d.fields[0], 1E-4);
    d = (FieldDoc) td.scoreDocs[2];
    assertEquals(2D, (Double) d.fields[0], 1E-4);
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) Sort(org.apache.lucene.search.Sort) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

FieldDoc (org.apache.lucene.search.FieldDoc)50 Sort (org.apache.lucene.search.Sort)26 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)23 SortField (org.apache.lucene.search.SortField)21 ScoreDoc (org.apache.lucene.search.ScoreDoc)20 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 ArrayList (java.util.ArrayList)14 Directory (org.apache.lucene.store.Directory)14 TopDocs (org.apache.lucene.search.TopDocs)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 TermQuery (org.apache.lucene.search.TermQuery)10 Document (org.apache.lucene.document.Document)9 Term (org.apache.lucene.index.Term)9 BytesRef (org.apache.lucene.util.BytesRef)9 HashMap (java.util.HashMap)8 Query (org.apache.lucene.search.Query)8 IndexReader (org.apache.lucene.index.IndexReader)7 HashSet (java.util.HashSet)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5