*lucene索引_创建_域选项
2024-09-24 18:10:41
【索引建立步骤】
【创建Directory】
【创建writer】
【创建文档并添加索引】
文档和域的概念很重要
文档相当于表中的每一条记录,域相当于表中的每一个字段。
【查询索引的基本信息】
使用IndexReader进行查询。
【实践】
附:
IndexUtil.java:
package cn.hk.index; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
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.store.LockObtainFailedException;
import org.apache.lucene.util.Version; public class IndexUtil {
private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"aa@hk.arg","bb@hk.org","cc@hk.arg",
"dd@hk.org","ee@hk.org","ff@hk.org"};
private String[] content = {
"welcome to visited the space","hello boy","my name is aa","i like football",
"I like football and I like Basketball too","I like movie and swim"
};
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","mike","jetty","jake"}; private Directory directory = null; public IndexUtil(){
try {
directory = FSDirectory.open(new File("d://lucene/index02"));
} catch (IOException e) {
e.printStackTrace();
}
} public void query(){
try {
IndexReader reader = IndexReader.open(directory);
//通过reader可以获取文档的数量
System.out.println("numDocs:" + reader.numDocs());
System.out.println("maxDocs" + reader.maxDoc());
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} public void index(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc = null;
for(int i=0;i<ids.length;i++){
doc = new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",content[i],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} }
} }
TestIndex.java:
package cn.hk.test; import org.junit.Test; import cn.hk.index.IndexUtil; public class TestIndex { @Test
public void testIndex(){
IndexUtil iu = new IndexUtil();
iu.index();
} @Test
public void testQuery(){
IndexUtil iu = new IndexUtil();
iu.query();
}
}
index()运行结果:
query()运行结果
最新文章
- MSSQL sp_helptextplus
- ruby(&;gem) koala安装
- asp.net mvc下的多语言方案 包含Html,Javascript和图片
- 【C语言】12-指向一维数组元素的指针
- 拖拽碰撞效果,高级浏览器下全部搞定(ie6-8还没有搞定)
- 绑定CPU
- AllJoyn Bundled Daemon 使用方式研究
- Delphi XE5 Device compatibility
- Yii 打造带有缓存功能的AR
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c
- hdu 3917 (最大权闭合图)
- OC-Protocol实现业务代理
- thinkphp5.0 自动加载
- asp.net mvc项目实记-开启伪静态-Bundle压缩css,js
- angular2 ----字符串、对象、base64 之间的转换
- 延迟提交form
- 【转】宽带路由器应用(三)—ARP欺骗防护功能的使用
- 10-openldap同步原理
- CRC-16的原理和实现
- 操作MySQL
热门文章
- [Usaco2012 Nov]Concurrently Balanced Strings
- Fighting
- C#中的list的System.Predicate<;in T>;和System.Comparison<;in T>;的应用
- java中实参与形参的概念
- dubbo系列--集群容错
- 2017广东工业大学程序设计竞赛决赛 G 等凹数字
- 关于 user agent ua
- 百度地图对https的支持
- (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5
- Java集合框架源码(四)——Vector