package cn.itcast0210.lucene.hellworld;
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.Fieldable;
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.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import cn.itcast0210.lucene.bean.Article;
/*
* 把一篇文章上传到索引库中,并且检索出来
*/
public class HelloWorld {
/**
* 为这个文章创建一个索引
*/
@Test
public void createIndex() throws Exception {
/*
* 思路: * 模拟创建一个文章对象 * 把文章这个对象放入到索引库中
*/
// 1 创建文章对象
Article article = new Article();
article.setId(1);
article.setTitle("lucene可以做搜索引擎");
article.setContent("baidu,google都是很好的搜索引擎");
// 2 放入到索引库中
IndexWriter indexWriter = null;
// 1 对indexWriter进行初始化
/*
* 创建IndexWriter的三个参数 * Directory 索引库 * Analyzer 分词器
*/
/**
* 创建Directory 在当前目录下创建索引库,索引库的名称为indexDir
*/
Directory directory = FSDirectory.open(new File("./indexDir"));
/*
* 创建分词器 基本的分析器
*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
indexWriter = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);
/** ******************************************************************************* */
/*
* 从artile到document的转化
*/
/**
* 1、创建Document对象
*/
Document document = new Document();
/*
* Field构造函数有4个参数 name 属性的名称 value 属性的值 Store Index
*/
Field idField = new Field("id", article.getId().toString(), Store.YES,
Index.NOT_ANALYZED);
Field titleField = new Field("title", article.getTitle(), Store.YES,
Index.ANALYZED);
Field contentField = new Field("content", article.getContent(),
Store.YES, Index.ANALYZED);
document.add(idField);
document.add(titleField);
document.add(contentField);
indexWriter.addDocument(document);
// 关闭io流的过程
indexWriter.close();
}
/**
* 根据关键字检索信息
*/
@Test
public void searchIndex() throws Exception {
List<Article> articleList = new ArrayList<Article>();
IndexSearcher indexSearcher = null;
/*
* 构建query对象
*/
/**
* 先构建QueryParser对象 参数3个 * 版本 * 字段 * 分词器
*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "content",
analyzer);
Query query = queryParser.parse("lucene");// 关键词
Directory directory = FSDirectory.open(new File("./indexDir"));
// 创建indexSearch过程
indexSearcher = new IndexSearcher(directory);
TopDocs topDocs = indexSearcher.search(query, 1);
int totalCount = topDocs.totalHits;// 根据关键词搜索出来的总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < scoreDocs.length; i++) {
int index = scoreDocs[i].doc;
float score = scoreDocs[i].score;// 相关度得分
Document document = indexSearcher.doc(index);
/*
* 把document转化成article ,并且输出
*/
// document.getField("id").stringValue();
Article article = new Article();
article.setId(Integer.parseInt(document.get("id")));
article.setTitle(document.get("title"));
article.setContent(document.get("content"));
articleList.add(article);
}
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
}