/* Copyright: * transaxtions llc * Pleasanton, CA 94588, USA * * License: * Please see License Agreement. Contact transaxtions llc for more info. * * Author: * Nagendra Nagarajayya * * Ver: * 1.0 * * Description: * Needs 3-4 arguments. * First argument, specifies the index, * second the field, * third, the search terms, * fourth optional, field name to output. * * Comments: * 2011/08/18 - First release */ import java.io.File; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.tgels.search.rankingalgorithm.*; /** * LuceneCollectorExample code showing RankingAlgorithm usage. Very easy to uses, similar to * Lucene API. Needs access to a Lucene index to work. *
* {@code
* args[0] is a lucene index path
* args[1] is the field to search on
* args[2] are the search terms
* args[3] is the field to show in the results
*
* Example:
* java LuceneCollectorExample "/lucene/index" contents "ranking algorithm" text
* }
*
*
* @author Nagendra Nagarajayya
*
* @see RankingQuery
* @see RankingHits
* @see RankingScore
*
*/
public class LuceneCollectorExample {
static public void main(String args[]) {
String index = args[0]; // lucene index path
String field = args[1]; // field to search
String searchterms = args[2]; // search terms
String title = null; // field to output, default title
System.out.println("args len=" + args.length);
if (args.length > 3) {
title = args[3];
}
if (title == null) {
title = "title";
}
System.out.println("field contents=" + field + " title=" + title);
try {
IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
RankingQuery rq = new RankingQuery();
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser parser = new QueryParser(Version.LUCENE_30, field, analyzer);
Query query = parser.parse(searchterms);
TopScoreDocCollector tdc = TopScoreDocCollector.create(1000, true);
rq.search(query, null, reader, tdc); //is = Lucene IndexSearcher object
int hits = tdc.getTotalHits();
ScoreDoc sda[] = null;
if (hits > 0) {
sda = tdc.topDocs().scoreDocs;
}
System.out.println("num hits=" + hits + "--no docs=" + reader.maxDoc());
for (int i=0; i