lucene4.6 索引创建和搜索例子
本文转自:http://blog.csdn.net/jyf211314/article/details/17503997 点击打开链接
lucene4.6小例子,Lucene全文检索步骤大体上有两方面:索引过程和搜索过程,具体如下:
1.索引过程
<1>创建IndexWriter,它的作用是用来写索引文件,
可以将IndexWriter看做是一个特定类型的数据库,用来存放各种表,可以将Document看做是一张张的表。
IndexWriter iw=new IndexWriter(Directory dire, IndexWriterConfig iwc);
可见,该方法有两个参数,第一个参数为索引存放位置,参数类型为Directory,第二个参数为 IndexWriter的配置类,
@1.Directory dire=FSDirectory.open(new File(D:\\lucenefiles));
通过Directory的创建指定索引存放位置(D:\lucenefiles文件夹)
@2.Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
通过Analyzer 的创建指定索引语言词汇的分析器。当前版本Lucene-core-4.6.0
@3.IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);
iwc.setOpenMode(OpenMode.CREATE); //CREATE:清空重建(推荐);
IndexWriter indexWriter=new IndexWriter(dire, iwc);
通过IndexWriterConfig的创建指定索引版本和语言词汇分析器
<2>创建Document,代表要索引的文档,
可以将Document看做是数据库中的一张张的表,而每个field都是表中的一个colum用来存放各种类型的信息,如标题、作者、时间等等
@1.File fileDir= new File("D:\\lucenefiles");
File[] files = fileDir.listFiles();
读取文件
@2.Document doc=new Document();
通过创建Document指定要索引的文档
@3.doc.add(new TextField("name", String name=files[i].getName(), Store.YES));
doc.add(new TextField("content", getFileContent(files[i]), Store.YES));
向Document文档中添加Field信息,不同类型的信息用不同类型的Field来表示;
getFileContent(File file)用于读取文件内容。
<3>将Document添加到IndexWriter中并且提交,
表(Document)创建好之后,当然要添加到数据库(IndexWriter)中,同时commit
@1.indexWriter.addDocument(doc); 通过该方法添加
indexWriter.commit();
2.搜索过程
<!>首先指定搜索目录
Directory dire=FSDirectory.open(new File("D:\\luceneindex"));
<2>创建语言词汇解析器,前后版本要一致
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
<3>创建IndexReader将搜索目录读取到内存,
IndexReader ireader=DirectoryReader.open(dire);
<4>创建IndexSearcher准备搜索
IndexSearcher isearcher=new IndexSearcher(ireader);
<5>创建QueryParser对查询语句进行语法分析,确定搜索的内容
QueryParser parser = new QueryParser(Version.LUCENE_46, "path", analyzer);
<6>创建Query生成查询语法树,
QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);
通过调用QueryParser 的parse("");方法来生成Query以及查询语法树
Query query = parser.parse("程序员");
<7>获取搜索结果
TopDocs td=is.search(query, 1000);ScoreDoc[] sds =td.scoreDocs;
<8>对搜索结果遍历操作
3.以下是个人测试例子
public class Constants {
public final static String INDEX_FILE_PATH = "F:\\lucene\\test";
//索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可
public final static String INDEX_STORE_PATH = "F:\\lucene\\index";
//索引的存放位置
}
package com.lucene.jl; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader; 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.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.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; public class LuceneIndex { /**
* 创建索引
* @param analyzer
* @throws Exception
*/
public static void createIndex(Analyzer analyzer) throws Exception{
Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);
IndexWriter iw=new IndexWriter(dire, iwc);
LuceneIndex.addDoc(iw);
iw.close();
} /**
* 动态添加Document
* @param iw
* @throws Exception
*/
public static void addDoc(IndexWriter iw) throws Exception{
File[] files=new File(Constants.INDEX_FILE_PATH).listFiles();
for (File file : files) {
Document doc=new Document();
String content=LuceneIndex.getContent(file);
String name=file.getName();
String path=file.getAbsolutePath();
doc.add(new TextField("content", content, Store.YES));
doc.add(new TextField("name", name, Store.YES));
doc.add(new TextField("path", path,Store.YES));
System.out.println(name+"==="+content+"==="+path);
iw.addDocument(doc);
iw.commit();
}
} /**
* 获取文本内容
* @param file
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static String getContent(File file) throws Exception{
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
StringBuffer sb=new StringBuffer();
String line=br.readLine();
while(line!=null){
sb.append(line+"\n");
line=null;
}
return sb.toString();
} /**
* 搜索
* @param query
* @throws Exception
*/
private static void search(Query query) throws Exception {
Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexReader ir=DirectoryReader.open(dire);
IndexSearcher is=new IndexSearcher(ir);
TopDocs td=is.search(query, 1000);
System.out.println("共为您查找到"+td.totalHits+"条结果");
ScoreDoc[] sds =td.scoreDocs;
for (ScoreDoc sd : sds) {
Document d = is.doc(sd.doc);
System.out.println(d.get("path") + ":["+d.get("path")+"]");
}
} public static void main(String[] args) throws Exception, Exception {
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
LuceneIndex.createIndex(analyzer);
QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);
Query query = parser.parse("人");
LuceneIndex.search(query);
}
}
4.磁盘文件检索如上述,对于数据库的检索页应该是差不多,应该也是先从数据库查出数据,然后对查出的数据做索引然后搜索,大致一样吧
最新文章
- 视频 - 在 VirtualBox 中部署 OpenStack
- 学习笔记——Maven 如何处理传递性依赖
- leetcode 24
- selenium python (十)浏览器多窗口处理
- Extjs combox的详解
- BigInteger
- 设置button 不可被点击
- [转] boost.circular_buffer简介
- python运维开发(八)----面向对象(下)
- POJ 2079 Triangle [旋转卡壳]
- combinations(组合)
- Example of DenseCRF with non-RGB data
- 关于noip2017的感想
- JAVA之Mybatis基础入门--框架搭建与简单查询
- oracle出现sga导致ORA-00821 ORA-00093报错解决办法
- oracle监听的动态注册和静态注册
- Fiddler教程--简介
- Git实战手册(三): stash解惑与妙用
- DataTables合并单元格(rowspan)的实现思路(多分组分类的情况)
- VMware vSphere 5.1 群集深入解析(一)
热门文章
- Android软键盘强制弹出,隐藏输入法.
- TortoiseGit和msysGit安装及使用笔记(windows下使用上传数据到GitHub)[转]
- CH BR8(小学生放假了-clock()/CLOCKS_PER_SEC-斜率优化常错集锦)
- android ViewFlipper的使用
- Java基础知识强化65:基本类型包装类之Integer的构造方法
- Tree( 树) 组件[3]
- JavaScript中函数参数的按值传递与按引用传递(即按地址传递)
- JS高级程序设计学习笔记之第三章基本概念(语法,数据类型,流控制语句,函数)——查漏补缺
- Android推送等耗电原因剖析
- Discuz!NT 3.5.2正式版与Asp.net网站会员信息整合