Search in sources :

Example 51 with TopDocs

use of org.apache.lucene.search.TopDocs in project ansj_seg by NLPchina.

the class HeightLightTest method search.

private static void search(Analyzer analyzer, Query query) throws IOException {
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    // 查询索引
    IndexSearcher isearcher = new IndexSearcher(directoryReader);
    System.out.println(query);
    TopDocs hits = isearcher.search(query, 5);
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int docId = hits.scoreDocs[i].doc;
        Document document = isearcher.doc(docId);
        System.out.println(toHighlighter(analyzer, query, document));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document)

Example 52 with TopDocs

use of org.apache.lucene.search.TopDocs in project ansj_seg by NLPchina.

the class IndexTest method search.

private void search(Analyzer queryAnalyzer, Directory directory, String queryStr) throws CorruptIndexException, IOException, ParseException {
    IndexSearcher isearcher;
    DirectoryReader directoryReader = IndexReader.open(directory);
    // 查询索引
    isearcher = new IndexSearcher(directoryReader);
    QueryParser tq = new QueryParser(Version.LUCENE_44, "text", queryAnalyzer);
    Query query = tq.parse(queryStr);
    System.out.println(query);
    TopDocs hits = isearcher.search(query, 5);
    System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int docId = hits.scoreDocs[i].doc;
        Document document = isearcher.doc(docId);
        System.out.println(toHighlighter(queryAnalyzer, query, document));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document)

Example 53 with TopDocs

use of org.apache.lucene.search.TopDocs in project neo4j-mobile-android by neo4j-contrib.

the class HitDoc method getMoreDocs.

/**
   * Tries to add new documents to hitDocs.
   * Ensures that the hit numbered <code>min</code> has been retrieved.
   */
private final void getMoreDocs(int min) throws IOException {
    if (hitDocs.size() > min) {
        min = hitDocs.size();
    }
    // double # retrieved
    int n = min * 2;
    //  TopDocs topDocs = (sort == null) ? searcher.search(weight, filter, n) : searcher.search(weight, filter, n, sort);
    TopDocs topDocs = null;
    if (sort == null) {
        topDocs = searcher.search(weight, filter, n);
    } else {
        if (this.score) {
            TopFieldCollector collector = LuceneDataSource.scoringCollector(sort, n);
            searcher.search(weight, null, collector);
            topDocs = collector.topDocs();
        } else {
            topDocs = searcher.search(weight, filter, n, sort);
        }
    }
    length = topDocs.totalHits;
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    float scoreNorm = 1.0f;
    if (length > 0 && topDocs.getMaxScore() > 1.0f) {
        scoreNorm = 1.0f / topDocs.getMaxScore();
    }
    int start = hitDocs.size() - nDeletedHits;
    // any new deletions?
    int nDels2 = countDeletions(searcher);
    debugCheckedForDeletions = false;
    if (nDeletions < 0 || nDels2 > nDeletions) {
        // either we cannot count deletions, or some "previously valid hits" might have been deleted, so find exact start point
        nDeletedHits = 0;
        debugCheckedForDeletions = true;
        int i2 = 0;
        for (int i1 = 0; i1 < hitDocs.size() && i2 < scoreDocs.length; i1++) {
            int id1 = ((HitDoc) hitDocs.get(i1)).id;
            int id2 = scoreDocs[i2].doc;
            if (id1 == id2) {
                i2++;
            } else {
                nDeletedHits++;
            }
        }
        start = i2;
    }
    int end = scoreDocs.length < length ? scoreDocs.length : length;
    length += nDeletedHits;
    for (int i = start; i < end; i++) {
        hitDocs.addElement(new HitDoc(scoreDocs[i].score * scoreNorm, scoreDocs[i].doc));
    }
    nDeletions = nDels2;
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TopFieldCollector(org.apache.lucene.search.TopFieldCollector) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 54 with TopDocs

use of org.apache.lucene.search.TopDocs in project neo4j by neo4j.

the class LuceneBatchInserterIndex method removeFromCache.

private void removeFromCache(long entityId) throws IOException, CorruptIndexException {
    IndexSearcher searcher = searcherManager.acquire();
    try {
        Query query = type.idTermQuery(entityId);
        TopDocs docs = searcher.search(query, 1);
        if (docs.totalHits > 0) {
            Document document = searcher.doc(docs.scoreDocs[0].doc);
            for (IndexableField field : document.getFields()) {
                String key = field.name();
                Object value = field.stringValue();
                removeFromCache(entityId, key, value);
            }
        }
    } finally {
        searcherManager.release(searcher);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document)

Example 55 with TopDocs

use of org.apache.lucene.search.TopDocs in project neo4j by neo4j.

the class NodeRangeDocumentLabelScanStorageStrategy method labelsForNode.

@Override
public PrimitiveLongIterator labelsForNode(IndexSearcher searcher, long nodeId) {
    try {
        TopDocs topDocs = searcher.search(format.rangeQuery(format.bitmapFormat().rangeOf(nodeId)), 1);
        if (topDocs.scoreDocs.length < 1) {
            return PrimitiveLongCollections.emptyIterator();
        } else if (topDocs.scoreDocs.length > 1) {
            throw new RuntimeException("This label scan store seems to contain an incorrect number of entries (" + topDocs.scoreDocs.length + ")");
        }
        int doc = topDocs.scoreDocs[0].doc;
        PrimitiveLongSet labels = Primitive.longSet();
        for (IndexableField fields : searcher.doc(doc).getFields()) {
            if ("range".equals(fields.name())) {
                continue;
            }
            Number numericValue = fields.numericValue();
            if (numericValue != null) {
                Long bitmap = numericValue.longValue();
                if (format.bitmapFormat().hasLabel(bitmap, nodeId)) {
                    labels.add(Long.decode(fields.name()));
                }
            }
        }
        return labels.iterator();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) IOException(java.io.IOException)

Aggregations

TopDocs (org.apache.lucene.search.TopDocs)496 IndexSearcher (org.apache.lucene.search.IndexSearcher)302 Document (org.apache.lucene.document.Document)275 TermQuery (org.apache.lucene.search.TermQuery)189 IndexReader (org.apache.lucene.index.IndexReader)187 Term (org.apache.lucene.index.Term)174 Directory (org.apache.lucene.store.Directory)174 Query (org.apache.lucene.search.Query)172 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)144 BooleanQuery (org.apache.lucene.search.BooleanQuery)127 ScoreDoc (org.apache.lucene.search.ScoreDoc)127 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)120 Sort (org.apache.lucene.search.Sort)94 Field (org.apache.lucene.document.Field)85 SortField (org.apache.lucene.search.SortField)74 IOException (java.io.IOException)58 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)56 TextField (org.apache.lucene.document.TextField)47 PhraseQuery (org.apache.lucene.search.PhraseQuery)46 PrefixQuery (org.apache.lucene.search.PrefixQuery)45