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 - 简介 官网: ...
随机推荐
- Ubuntu下在Eclipse IDE for C/C++ Developers中怎样执行C语言的GTK程序?(已解决)
(已解决.详见Ubuntu 12.04下在Eclipse IDE for C/C++ Developers中执行C语言的GTK程序) 按"Ubuntu下GTK的安装.编译和測试"( ...
- Ubuntu17.10 下配置caffe 仅CPU i386可以直接apt install caffe-cpu,但是怎么运行mnist代码我懵逼了
Ubuntu16.04下配置caffe(仅CPU) 参考:http://blog.csdn.net/zt_1995/article/details/56283249 第二次配置caffe环境,依 ...
- Cosine Similarity of Two Vectors
#include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...
- Device /dev/sdb1 not found (or ignored by filtering)
/etc/lvm/lvm.conf filters
- 5-7 第五天 微信 JS-SDK-简介
微信的SDK显然呢并不是在这个公众号里面直接使用的,而是在网页里面使用的.什么样的网页呢?就是微信内置的浏览器.你从朋友圈.从好友消息.从群消息,确定是从公众号的回复里面打开一个链接. 便会启动一个浏 ...
- 海量数据统计topK
有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M.返回频数最高的100个词. 思路: 把这1G的数据一次性全部读入内存是不可能了,可以每次读一行,然后将该词存到 ...
- A - Supermarket
Problem description We often go to supermarkets to buy some fruits or vegetables, and on the tag the ...
- Elasticsearch之curl删除
扩展下, Elasticsearch之curl删除索引库 [hadoop@djt002 elasticsearch-2.4.3]$ curl -XDELETE 'http://192.168.80.2 ...
- more-less-cat-tail-head 命令简单分析
区别:cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 操作,适合察看内容短小.不超过一屏的文件:more比cat强大一点,支持分页显示,你可以ctrl+ ...
- YTBro Video 2016-07-30 Sat
明日天气:多云转阴,32~24度 运动向 健身 20中极致疯狂的俯卧撑,新手请勿尝试 跑酷 大神放大招,招招见血,不信你看 赛事 2016年世界室内田径锦标赛 男子4×400接力决赛 台球 RP集中爆 ...