lucene索引的添加见 http://www.cnblogs.com/getchen/p/8615276.html 入门代码。

公共代码

  public <T extends Query> void printResult(Query query) throws Exception{
IndexSearcher indexSearcher = getIndexSearcher(); TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
int i=0;
for (ScoreDoc doc : scoreDocs) {
int docIndex = doc.doc;
Document document = indexSearcher.doc(docIndex);
String fileName = document.get("fileName");
System.out.println(fileName);
String fileSize = document.get("fileSize");
System.out.println("size:=="+fileSize);
String filePath = document.get("filePath");
System.out.println(filePath);
String fileContent = document.get("fileContent");
System.out.println(fileContent);
System.out.println("==========="+ ++i +"===============");
}
indexSearcher.getIndexReader().close();
} public IndexSearcher getIndexSearcher() throws Exception{
// 第一步: 创建一个Directory 对象,也就是索引库存放的位置。
Directory directory = FSDirectory.open(new File("F:\\lucene\\indexDatabase"));
// 第二步: 创建一个indexReader 对象,需要指定Directory 对象。
IndexReader indexReader = DirectoryReader.open(directory);
// 第三步: 创建一个indexsearcher 对象,需要指定InclexReader 对象。
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
return indexSearcher;
}

lucene索引的查询

查询全部

 @Test
public void queryAll() throws Exception{
Query query = new MatchAllDocsQuery();
printResult(query); }

区间查询

    @Test
public void queryRange() throws Exception{
Query query = NumericRangeQuery.newLongRange("fileSize",0L,800L,true,true);
printResult(query); }

组合条件查询

    /**
* 组合条件查询
*/
@Test
public void queryBoolean() throws Exception{
BooleanQuery query = new BooleanQuery();
TermQuery query1 = new TermQuery(new Term("fileName", "java"));
TermQuery query2 = new TermQuery(new Term("fileName", "lucene"));
query.add(query1, BooleanClause.Occur.MUST);
query.add(query2, BooleanClause.Occur.MUST); // Query query3 = query;
printResult(query); }

其中BooleanClause.Occur 中有三个选项:MUST,NOT_MUST,SHOULD.等同于数据库中的and,not,or


lucene索引的添加
//增
@Test
public void add() throws Exception{
IndexWriter indexWriter = getIndexWriter();
Document document = new Document();
document.add(new TextField("fileContent","电话号码", Field.Store.YES));
indexWriter.addDocument(document);
indexWriter.close();
}

lucene 索引的更新

 /**
* 更新:删除原有的并增加需要更改的。
* 删掉一个添加一个
* @throws Exception
*/
@Test
public void update() throws Exception{
IndexWriter indexWriter = getIndexWriter();
Term term = new Term("fileContent", "电话号码");
/* Document document = new Document();
document.add(new TextField("fileContent","电话号码1", Field.Store.YES));*/
Document document = getDocumentByFile(new File("F:\\lucene\\test.txt"));
indexWriter.updateDocument(term,document,new IKAnalyzer());
indexWriter.close();
} public Document getDocumentByFile(File file) throws Exception{
Document document = new Document();
//获取文件的名称
String fileName = file.getName();
//创建textfield,保存文件名(key,value,是否存储)
TextField fileNameField = new TextField("fileName",fileName, Field.Store.YES);
//文件大小
long fileSize = FileUtils.sizeOf(file); // NumericDocValuesField fileSizeField = new NumericDocValuesField("fileSize", fileSize);
System.out.println(fileSize); // SortedNumericDocValuesField fileSizeField = new SortedNumericDocValuesField("fileSize", fileSize);
// LongPoint fileSizeField = new LongPoint("fileSize", fileSize);
StringField fileSizeField = new StringField("fileSize", String.valueOf(fileSize), Field.Store.YES);
//文件路径
String filePath = file.getPath();
StoredField filePathField = new StoredField("filePath", filePath);
//文件内容
String fileContent = FileUtils.readFileToString(file,"gbk");
TextField fileContentField = new TextField("fileContent", fileContent, Field.Store.YES);
document.add(fileNameField);
document.add(fileSizeField);
document.add(filePathField);
document.add(fileContentField);
return document;
}

lucene索引的删除

  //删除全部
@Test
public void deleteAll() throws Exception{
IndexWriter indexWriter = getIndexWriter();
indexWriter.deleteAll();
indexWriter.close(); } //删除部分
@Test
public void delete() throws Exception{
IndexWriter indexWriter = getIndexWriter();
Term term = new Term("fileContent", "电话号码"); //索引文件中包含的有电话号码,分词器也分析出了电话号码一词,删除之后就无法通过该关键词索引到文件了。
indexWriter.deleteDocuments(term);
indexWriter.close();
}

lucene_03_索引的增删改查的更多相关文章

  1. elasticsearch索引的增删改查入门

    为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/<index>/&l ...

  2. 列表(索引切片 增删改查 嵌套) range 元组的初识

    li = ["alex", "WuSir", "ritian", "barry", "wenzhou" ...

  3. lucene4.4 索引的增删改查

    package com.lucene.test; import java.io.File; import java.io.FileReader; import java.io.IOException; ...

  4. Java solr 索引数据增删改查

    具体代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClie ...

  5. elasticsearch java索引的增删改查

    1.创建索引并插入数据 Map<String, Object> json = new HashMap<String, Object>(); json.put("use ...

  6. Elasticsearch 索引文档的增删改查

    利用Elasticsearch-head可以在界面上(http://127.0.0.1:9100/)对索引进行增删改查 1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演 ...

  7. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  8. 分布式搜索elasticsearch 索引文档的增删改查 入门

    1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/ ...

  9. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

随机推荐

  1. luogu2341 [HAOI2006]受欢迎的牛

    题目大意 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜欢B,B喜欢C,那么A也喜欢C.牛 ...

  2. git出错调试

    https://stackoverflow.com/questions/6178401/how-can-i-debug-git-git-shell-related-problems git_trace ...

  3. C# WINFORM 局域网PING 工具(技术改变世界-cnblog)

    WINFORM 局域网PING 工具(技术改变世界-cnblog) 需求: 1.实时更新 日期时间 2.可以ping多个IP 地址,必须判断 IP地址的正确性,不能为广播地址 3.对ping结果的显示 ...

  4. hdoj-看病要排队

    看病要排队 Problem Description 看病要排队这个是地球人都知道的常识. 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看 ...

  5. DataGuard总体结构

    一.DataGuard总体结构 总体目标 1.   描述计划和非计划停机的不同因数 2.   DataGuard的主要组件 3.   物理以及逻辑DataGuard的异同 4.   建立DataGua ...

  6. CentOS7 网卡名称重命名为eth*

    CentOS7 禁用网卡名称命名规则,启动时传递"net.ifnames=0 biosdevname=0"/etc/default/grupgrup2-mkconfig -o /b ...

  7. LMS、NLMS最优步长理论分析与Speex回声消除可能的改进想法

    一.回声消除算法模型 先来分析下自适应回声消除的主要组成部分,大体上可以把回声消除模型分为两个部分 横向滤波器结构 滤波器系数自适应与步长控制 横向滤波器用脉冲响应w(n)[有的地方也称为回声路径]与 ...

  8. POJ 2823 线段树 Or 单调队列

    时限12s! 所以我用了线段树的黑暗做法,其实正解是用单调队列来做的. //By SiriusRen #include <cstdio> #include <cstring> ...

  9. bootstrap-paginator基于bootstrap的分页插件

    bootstrap-paginator基于bootstrap的分页插件 GitHub 官网地址:https://github.com/lyonlai/bootstrap-paginator 步骤 引包 ...

  10. js-事件处理(重点)

    1:各种常用事件: 2:简单用法: <body onLoad="javascript:alert('hello world');" onUnload="javasc ...