package l002;
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.StringField;
import
org.apache.lucene.document.TextField;
import
org.apache.lucene.document.Field.Store;
import
org.apache.lucene.index.IndexWriter;
import
org.apache.lucene.index.IndexWriterConfig;
import
org.apache.lucene.store.Directory;
import
org.apache.lucene.store.FSDirectory;
import
org.apache.lucene.util.Version;
public class IndexFile1 {
protected String[] ids={"1", "2"};
protected String[] content={" I LOVE you", " I LOVE you"};
protected String[] city={"Amsterdam", "Venice"};
private Directory dir;
/**
* 初始化添加文档
* @throws
IOException
*/
public void init() throws IOException{
String pathFile =
"D://lucene//index";
dir = FSDirectory.open(new
File(pathFile));
IndexWriter writer = getWriter();
for (int i = 0; i <
ids.length; i++) {
Document doc=new Document();
doc.add(new
StringField("id", ids[i], Store.YES));
doc.add(new TextField("content",
content[i], Store.YES));
doc.add(new StringField("city", city[i],
Store.YES));
writer.addDocument(doc);
}
System.out.println("init
ok");
writer.close();
}
/*
* 获取indexWriter对象
*/
private
IndexWriter getWriter() throws IOException {
Analyzer analyzer=new
StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig iwc=new
IndexWriterConfig(Version.LUCENE_40, analyzer);
return new IndexWriter(dir,
iwc);
}
}
package l002;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import
org.apache.lucene.index.DirectoryReader;
import
org.apache.lucene.index.IndexReader;
import
org.apache.lucene.index.Term;
import
org.apache.lucene.search.IndexSearcher;
import
org.apache.lucene.search.ScoreDoc;
import
org.apache.lucene.search.TermQuery;
import
org.apache.lucene.search.TopDocs;
import
org.apache.lucene.store.Directory;
import
org.apache.lucene.store.FSDirectory;
public class IndexSearch1 {
public void search() throws
IOException{
String pathFile = "D://lucene//index";
Directory dir =
FSDirectory.open(new File(pathFile));
IndexReader reader =
DirectoryReader.open(dir);
IndexSearcher searcher = new
IndexSearcher(reader);
Term term = new Term("content","i");
TermQuery
query = new TermQuery(term);
TopDocs topDocs = searcher.search(query,
5);
ScoreDoc[] scoreDocs =
topDocs.scoreDocs;
System.out.println("查询结果总数----"+topDocs.totalHits+"
最大的评分----"+topDocs.getMaxScore());
for (int i = 0; i < scoreDocs.length;
i++) {
int doc = scoreDocs[i].doc;
Document document =
searcher.doc(doc);
System.out.println("content"+document.get("content"));
System.out.println("id--"+doc+"---scors--"+scoreDocs[i].score+"----idx----"+scoreDocs[i].shardIndex);
}
reader.close();
};
public
static void main(String[] args) throws Exception {
// IndexFile1 file = new
IndexFile1();
// file.init();
IndexSearch1 search = new
IndexSearch1();
search.search();
}
}
原文地址:http://www.cnblogs.com/mrgong/p/3770577.html