lucene_03_索引的增删改查
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_索引的增删改查的更多相关文章
- elasticsearch索引的增删改查入门
为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/<index>/&l ...
- 列表(索引切片 增删改查 嵌套) range 元组的初识
li = ["alex", "WuSir", "ritian", "barry", "wenzhou" ...
- lucene4.4 索引的增删改查
package com.lucene.test; import java.io.File; import java.io.FileReader; import java.io.IOException; ...
- Java solr 索引数据增删改查
具体代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClie ...
- elasticsearch java索引的增删改查
1.创建索引并插入数据 Map<String, Object> json = new HashMap<String, Object>(); json.put("use ...
- Elasticsearch 索引文档的增删改查
利用Elasticsearch-head可以在界面上(http://127.0.0.1:9100/)对索引进行增删改查 1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演 ...
- Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查
今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...
- 分布式搜索elasticsearch 索引文档的增删改查 入门
1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/ ...
- mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)
最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...
随机推荐
- Codeforces Round #276 (Div. 1)B. Maximum Value 筛法
D. Maximum Value You are given a sequence a consisting of n integers. Find the maximum possible ...
- yum install mysql(转载)
linux下使用yum安装mysql 1.安装查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查 ...
- LuoguP4365 [九省联考2018]秘密袭击
https://zybuluo.com/ysner/note/1141136 题面 求一颗大小为\(n\)的树取联通块的所有方案中,第\(k\)个数之和. \(n\leq1,667,k\leq n\) ...
- CentOS/ubuntu iscsi initior target
http://docs.oracle.com/cd/E26926_01/html/E25884/fpjwy.html iscsiadm: initiator reported error (24 - ...
- Java中jspf文件的作用
转自:https://blog.csdn.net/xzmeasy/article/details/75103431 为什么要用jspf文件 写jsp页面时,是不是:css和js引用特别多,而且有些页面 ...
- tp 推送微信的模板消息
设置推送类: <?php /** * tpshop 微信支付插件 * ============================================================== ...
- lsit集合去重复 顶级表达式
updateList = updateList.Where((x, i) => updateList.FindIndex(z => z.ID == x.ID) == i).ToList() ...
- 使用IDEA 搭建一个SpringBoot + Hibernate + Gradle
---恢复内容开始--- 打开IDEA创建一个新项目: 第一步: 第二步: 第三步: 最后一步: 如果下载的时候时间太久.可以找到build.gradle文件,添加以下代码.如下图 maven{ ur ...
- Oracle11g自带的SQL developer无法打开解决
在安装完Oracle Database 11g Release 2数据库,想试一下Oracle自带的SQL Developer工具,在操作系统菜单的所有程序中找到SQL Developer如下所示,并 ...
- 3、scala函数入门
1.定义函数 2.在代码块中定义函数体 3.递归函数与返回类型 4.默认参数 5.带名参数 6.变长参数 7.使用序列调用变长参数 8.过程 9.lazy值 10.异常 1 ...