Lucene创建索引,删除索引
来源:互联网 发布:移动身份证读卡器软件 编辑:程序博客网 时间:2024/05/17 05:10
Lucene创建索引
package testindex;import java.io.IOException;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.index.IndexWriter;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;public class TestIndex { /** * user(id,name,address) * insert into user(id,name,address) values(1,'zhangsan','shanghaichangningqv'); * @throws IOException */ public static void main(String[] args) throws IOException { String [] ids={"1","2","3"}; String [] names={"zhangsan","lisi","wangwu"}; String [] addresses={"shanghai","beijing","guangzhou"}; Analyzer analyzer=new StandardAnalyzer(); String indexDir="C:/Users/yihong/Desktop/luceneindex"; Directory dir=FSDirectory.getDirectory(indexDir); //true 表示创建或覆盖当前索引;false表示对当前索引进行追加 //Default value is 128 IndexWriter writer=new IndexWriter(dir,analyzer,true,IndexWriter.MaxFieldLength.LIMITED); for(int i=0;i<ids.length;i++){ Document document=new Document(); document.add(new Field("id",ids[i],Field.Store.YES,Field.Index.ANALYZED)); document.add(new Field("name",names[i],Field.Store.YES,Field.Index.NO)); document.add(new Field("address",addresses[i],Field.Store.YES,Field.Index.ANALYZED)); writer.addDocument(document); } writer.optimize(); writer.close(); }}
删除索引
package testindex;import java.io.IOException;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;public class TestDelDocument { public static void main(String[] args) throws IOException { String indexDir="C:/Users/yihong/Desktop/luceneindex"; Directory dir=FSDirectory.getDirectory(indexDir); IndexReader reader=IndexReader.open(dir);// System.out.println(reader.maxDoc()); reader.deleteDocument(0); System.out.println(reader.deleteDocuments(new Term("id","2"))); System.out.println(reader.numDocs()); reader.close(); dir.close(); }}
现在的Field构造函数原型是如下样子的:
public Field(String name, String value, Store store, Index index) <Lucene 的核心索引类 IndexWriter :建立索引的核心组件。
Directory:代表一个 lucene 索引项的位置。
Analyzer :对文本内容进行分析的抽象类,具体实现中有停用词切除、词干分析、大小写切换等功能。
Document :可以视作文本经过处理后所对应的对象,由多个字段组成,如路径、标题、摘要、修改日期等等。
Field :字段,对应于文本的某一部分数据,便于检索时根据结果提取。早期版本分为四个类型: Keyword 、 UnIndexed 、 UnStored 和 Text ,其主要区别归结于三个方面:是否被分析,是否被索引,是否存储于索引中。但是在最新版本的Lucene中,使用了一种更为统一的形式,也即只有Field一个类,然后使用一些参数来描述这个字段的属性,通过参数组合,可以组合出各种类别,甚至那四种不存在的类别理论上也是可以组合出来。
阅读全文
0 0
- Lucene创建索引,删除索引
- Lucene创建、查询、删除、更新 索引
- lucene索引管理(索引创建于删除)
- lucene中的删除索引
- lucene 删除索引
- Lucene在删除索引
- Lucene之删除索引
- Lucene索引删除详解
- lucene学习----创建索引
- lucene创建索引
- Java_ABC_3.Lucene创建索引
- Lucene 索引创建
- LUCENE创建索引【鸡蛋】
- Lucene 索引创建
- lucene--创建索引,搜索
- lucene创建索引
- Lucene 4.4 创建索引
- Lucene.Net创建索引
- 冒泡排序
- 当我们输入URI,按下回车发生了什么?
- Java代码实现接口测试
- 不使用第三方变量 交换两个数
- 设计模式之禅笔记--面向对象设计六大原则之三
- Lucene创建索引,删除索引
- Lintcode102 Linked List Cycle solution 题解
- bash(4) 函数 function
- 问题 : 小希的数表
- Jenkins + Gradle + SonarQube 项目持续集成并分析环境搭建
- 计算机中的数值问题
- java中的泛型
- IntelliJ IDEA上创建Maven Spring MVC项目
- Java复用类