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));
}
}
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));
}
}
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;
}
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);
}
}
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);
}
}
Aggregations