刚刚写了一个Lucene程序  不过就是检索不出数据  求高手解释package cn.itcast.a_helloworld;import java.io.File;
import java.util.ArrayList;
import java.util.List;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.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
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.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;import cn.itcast._domain.Article;public class HelloWorld { // 建立索引(模拟在贴吧中发表了一个文章,会保存到数据库中,并且应该建立索引,以便能搜索到)
@Test
public void createIndex() throws Exception {
// 模拟一条刚保存到数据库中的数据
Article article = new Article();
article.setId(1);
article.setTitle("Lucene是全文检索的框架");
article.setContent("如果信息检索系统在用户发出了检索请求后再去互联网上找答案,根本无法在有限的时间内返回结果。"); // 建立索引 ?
// >> 1,把Article转成Document
Document doc = new Document();
doc.add(new Field("id", article.getId().toString(), Store.YES, Index.ANALYZED));
doc.add(new Field("title", article.getTitle(), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED)); // >> 2,建立索引
Directory directory = FSDirectory.open(new File("./indexDir/")); // 索引库文件所在的目录
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); IndexWriter indexWriter = new IndexWriter(directory, analyzer, new MaxFieldLength(10000)); // MaxFieldLength.LIMITED
indexWriter.addDocument(doc);
indexWriter.close();
} // 搜索
@Test
public void search() throws Exception {
// 搜索条件
String queryString = "lucene";
// String queryString = "compass"; // 进行搜索,得到结果 ?
// ====================================================================
Directory directory = FSDirectory.open(new File("./indexDir/")); // 索引库文件所在的目录
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); // 1,把查询字符串转为Query对象
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "title", analyzer); // 只在title中查询
Query query = queryParser.parse(queryString); // 2,查询,得到中间结果
IndexSearcher indexSearcher = new IndexSearcher(directory);
TopDocs topDocs = indexSearcher.search(query, 100); // 按指定条件条询,只返回前n条结束
int count = topDocs.totalHits; // 总结果数
ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 前n条结果的信息 // 3,处理结果
List<Article> list = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
float score = scoreDoc.score; // 相关度得分
int docId = scoreDoc.doc; // Document数据库的内部编号(是唯一的,由Lucene自动生成的) // 根据编号取出真正的Document数据
Document doc = indexSearcher.doc(docId); // 把Document转成Article
Article article = new Article();
article.setId(Integer.parseInt(doc.get("id"))); // 需要转Integer型
article.setTitle(doc.get("title")); // doc.getField("title").stringValue()
article.setContent(doc.get("content"));
list.add(article);
} indexSearcher.close();
// ==================================================================== // 显示结果
System.out.println("总结果数量为:" + list.size());
for (Article article : list) {
System.out.println("--------> id = " + article.getId());
System.out.println("title  = " + article.getTitle());
System.out.println("content= " + article.getContent());
}
}
}