lucene可以做搜索引擎 baidu,google都是很好的搜索引擎

package com.tfy.lucene;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.print.Doc;


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.MultiFieldQueryParser;
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;


/**
 * 1、把一个对象放入到索引库中
 * 2、从索引库中把一个对象检索出来
 * 3、更新、删除
 * @author Think
 *
 */
public class ArticleIndex {
@Test
public void testCresteIndex() throws Exception{
/**
* 1、创建一个对象,并且设置值
* 2、创建一个IndexWriter对象
* 3、利用IndexWriter把该对象放入到索引库中
* 4、关闭indexWriter
*/
Article article=new Article();
article.setId(1L);
article.setName("it coud   lucene可以做搜索引擎");
article.setContent("在程序  baidu,google都是很好的搜索引擎 ");
Directory directory=FSDirectory.open(new File("./indexDir"));
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_30);
/**
* directory指明了索引库的位置
* analyzer把要输入的内容进行分词,把分词后的结果存储到目录库中
* MaxFieldLength.LIMITED  在内容库中存储的字段最大的长度
*/
IndexWriter indexWriter=new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
//把 article转化为document
Document document=new Document();
/**
* name
*    存储到索引库中的名称
* value
*    存储到索引库中的值
* Store
*    YES
*       该字段向内容库中储存
*    NO
*       该字段不向内容库中储存
* Index
*  No  在目录库中不存储
*    NOT_ANALYZED  在目录库中存储,但是不分词
*    ANALYZED      在目录库中存储,并且分词
*/
Field idfield=new Field("id",article.getId().toString(), Store.YES, Index.NOT_ANALYZED);
Field namefield=new Field("name",article.getName().toString(), Store.YES, Index.ANALYZED);
Field contentfield=new Field("content",article.getContent().toString(), Store.YES, Index.ANALYZED);

document.add(idfield);
document.add(namefield);
document.add(contentfield);
//向索引库中增加一行记录
indexWriter.addDocument(document);

indexWriter.close();
}


@Test
public void testSearchIndex() throws Exception{
Directory directory=FSDirectory.open(new File("./indexDir"));
/**
* 创建一个indexSearch,用于检索
* directory
*  指向索引库的位置
*/
IndexSearcher indexSearcher=new IndexSearcher(directory);
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_30);
/**
* version
*    版本号
* f
*  在那个字段中进行检索
* String[]
*   在多个字段中进行检索
* analyzer
*   在检索的时候,会填入一些词汇进行检索,那么在lucene内部会对这个词汇进行分词
*/
QueryParser queryParser=new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"name","content"}, analyzer);
/**
* parse反复发存放关键词
*/

Query query=queryParser.parse("lucene");
/**
* TopDocs代表前几个文档
*/
TopDocs topDocs=indexSearcher.search(query, 1);
//根据关键词检索出来的总的记录数
int count=topDocs.totalHits;
//由关键词及索引值组成的对象为ScoreDoc
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
List<Article> articles=new ArrayList<Article>();
//遍历目录库中所有的关键词
for(int i=0;i<scoreDocs.length;i++){
//doc是关键词对应的索引值
int index=scoreDocs[i].doc;
//根据索引值检索内容
Document document=indexSearcher.doc(index);
//由document转变为article的过程
Article article=new Article();
article.setId(Long.parseLong(document.get("id")));
article.setName(document.get("name"));
article.setContent(document.get("content"));

articles.add(article);
}
for(Article article:articles){
System.out.println(article.getContent());
System.out.println(article.getName());
}

}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值