http://m.oschina.net/bbs/thread/7707
/**
*
*/
package test;
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
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.NIOFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
/**
* @author george.yin
*
* @email:
[email protected]
*
* 2010-12-8
*/
public class ScoreDemo {
public final static String INDEX_STORE_PATH = "D:\\dev_root\\install\\eclipse_j2ee\\java\\workspace\\LuceneDemo\\ScoreDemo";
public static void main(String[] args) throws Exception {
boolean createIndex = false;
Directory dic = new NIOFSDirectory(new File(INDEX_STORE_PATH));
Analyzer analyzer = new IKAnalyzer();
if (createIndex) {
IndexWriter writer = new IndexWriter(dic, analyzer, true, MaxFieldLength.UNLIMITED);
writer.setUseCompoundFile(false);
Field nameField;
Field infoField;
Field demoField;
Document doc1 = new Document();
nameField = new Field("Name", "我们", Field.Store.YES, Field.Index.ANALYZED);
nameField.setBoost(100f);
infoField = new Field("Info", "测试1", Field.Store.YES, Field.Index.ANALYZED);
infoField.setBoost(10f);
demoField = new Field("Demo", "测试1", Field.Store.YES, Field.Index.ANALYZED);
demoField.setBoost(5f);
doc1.add(nameField);
doc1.add(infoField);
doc1.add(demoField);
writer.addDocument(doc1);
Document doc2 = new Document();
nameField = new Field("Name", "我们的爱", Field.Store.YES, Field.Index.ANALYZED);
nameField.setBoost(100f);
infoField = new Field("Info", "我们2", Field.Store.YES, Field.Index.ANALYZED);
infoField.setBoost(10f);
demoField = new Field("Demo", "我们测试1", Field.Store.YES, Field.Index.ANALYZED);
demoField.setBoost(5f);
doc2.add(nameField);
doc2.add(infoField);
doc2.add(demoField);
writer.addDocument(doc2);
Document doc3 = new Document();
nameField = new Field("Name", "我们的爱情", Field.Store.YES, Field.Index.ANALYZED);
nameField.setBoost(100f);
infoField = new Field("Info", "测试3", Field.Store.YES, Field.Index.ANALYZED);
infoField.setBoost(10f);
demoField = new Field("Demo", "我们的爱情公式", Field.Store.YES, Field.Index.ANALYZED);
demoField.setBoost(5f);
doc3.add(nameField);
doc3.add(infoField);
doc3.add(demoField);
writer.addDocument(doc3);
Document doc4 = new Document();
nameField = new Field("Name", "我", Field.Store.YES, Field.Index.ANALYZED);
nameField.setBoost(100f);
infoField = new Field("Info", "测试3", Field.Store.YES, Field.Index.ANALYZED);
infoField.setBoost(10f);
demoField = new Field("Demo", "们", Field.Store.YES, Field.Index.ANALYZED);
demoField.setBoost(5f);
doc4.add(nameField);
doc4.add(infoField);
doc4.add(demoField);
writer.addDocument(doc4);
writer.commit();
writer.close();
System.out.println("索引创建成功...");
} else {
IndexSearcher searcher = new IndexSearcher(dic, true);
String searchKey = "我们";
/*
* TermQuery tq;
BooleanQuery bq = new BooleanQuery();
tq = new TermQuery(new Term("Name", "我们"));
bq.add(tq, BooleanClause.Occur.SHOULD);
tq = new TermQuery(new Term("Info", "我们"));
bq.add(tq, BooleanClause.Occur.SHOULD);
*/
String[] fieldStrArr = new String[] { "Name", "Info" ,"Demo"};
MultiFieldQueryParser mfqp = new MultiFieldQueryParser(Version.LUCENE_CURRENT, fieldStrArr, analyzer);
Query query = mfqp.parse(searchKey);
System.out.println("查询条件为:" + query);
TopDocs topDocs = searcher.search(query,10000);
ScoreDoc[] docArr = topDocs.scoreDocs;
System.out.println("命中:" + docArr.length);
int docid = 0;
for (int i = 0; i < docArr.length; i++) {
docid = docArr[i].doc;
Document doc = searcher.doc(docid);
System.out.println("Score :" + docArr[i].score + "Name :" + doc.get("Name") + "\t\t" + "Info :" + doc.get("Info") + "\t\t" + "Demo :" + doc.get("Demo") );
// System.out.println(searcher.explain(q, docid));//
}
}
}
}