自己写了个小例子
package com.lucene.test;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
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.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
public class IndexWriterTest {
public static void main(String[] args) {
try {
File file = new File("d:/index");
IndexWriter writer = new IndexWriter(file, new StandardAnalyzer(), MaxFieldLength.UNLIMITED);
Document doc = new Document();//测试 lucene能否实现不同属性检索
Field field = new Field("title", "测试lucene能否实现不同属性检索", Store.YES, Field.Index.ANALYZED, Field.TermVector.NO);
doc.add(field);
writer.addDocument(doc);
doc = new Document();
field = new Field("descript", "描述内容", Store.YES, Field.Index.ANALYZED, Field.TermVector.NO);
doc.add(field);
writer.addDocument(doc);
writer.optimize();
writer.close();
//检索
IndexSearcher search = new IndexSearcher(FSDirectory.getDirectory("d:/index"));
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("descript", analyzer);
Query query = parser.parse("描述");
// BooleanQuery q = new BooleanQuery();
// RangeQuery range = new RangeQuery(new Term("time", ""), new Term("time",""), false);
TopDocs docs = search.search(query, 10);
System.out.println("找到记录共"+docs.totalHits+"条。");
ScoreDoc[] d = docs.scoreDocs;
for(ScoreDoc s: d){
int id = s.doc;
Document dd = search.doc(id);
System.out.println(dd.get("descript"));
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
9万+

被折叠的 条评论
为什么被折叠?



