Skip to content

Instantly share code, notes, and snippets.

@JobsDong
Created August 11, 2016 11:59
Show Gist options
  • Save JobsDong/60de260e933757d203a3a836b6dbe8f0 to your computer and use it in GitHub Desktop.
Save JobsDong/60de260e933757d203a3a836b6dbe8f0 to your computer and use it in GitHub Desktop.
indexReader
package com.shenbian.test;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* @author JobsDong
*/
class IndexReaderTest {
public static void main(String[] args) throws IOException, ParseException {
Directory directory = FSDirectory.open(new File("/home/wuyadong/repo/aes-lucene/index"));
AESDirectory aesDirectory = new AESDirectory(directory, "password");
IndexReader indexReader = DirectoryReader.open(aesDirectory);
Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_4_9);
IndexSearcher searcher = new IndexSearcher(indexReader);
QueryParser parser = new QueryParser(Version.LUCENE_4_9, "content", analyzer);
Query query = parser.parse("银行");
TopDocs results = searcher.search(query, 3);
ScoreDoc[] hits = results.scoreDocs;
try {
for (ScoreDoc hit : hits) {
Document doc = searcher.doc(hit.doc);
System.out.println(hit.doc + ": " + hit.score + ":" + doc.get("title"));
}
} finally {
indexReader.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment