Skip to content

Instantly share code, notes, and snippets.

@zzzfree
Created March 8, 2013 02:52
Show Gist options
  • Save zzzfree/5113873 to your computer and use it in GitHub Desktop.
Save zzzfree/5113873 to your computer and use it in GitHub Desktop.
Lucene search page
import org.apache.lucene.analysis.Analyzer
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.index.IndexReader
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.QueryParser
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.search.Query
import org.apache.lucene.search.TopDocs
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.search.ScoreDoc;
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("./db")));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
def field = "contents"
QueryParser parser = new QueryParser(Version.LUCENE_41, field, analyzer);
Query query = parser.parse("门");
def pageSize = 10
TopDocs results = searcher.search(query, pageSize);
def show = {
// print results
println "-------------------------------------"
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
println(numTotalHits + " total matching documents");
hits.each{
println "path:" + searcher.doc(it.doc).get("path");
println "contents:" + searcher.doc(it.doc).get("contents");
}
}
show()
// next page
results = searcher.searchAfter(results.scoreDocs[results.scoreDocs.size()-1] , query, pageSize)
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment