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. P1052 过河(状态压缩)

    P1052 过河(状态压缩) 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把 ...

  2. zoj--3870--Team Formation(位运算好题)

    Team Formation Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  3. 【POJ 3635】 Full Tank

    [题目链接] http://poj.org/problem?id=3635 [算法] 优先队列BFS 实现类似于堆优化dijkstra [代码] #include <algorithm> ...

  4. El和标准标签

    EL表达式针对于四大作用域:application,session,request,pagecontext(作用域由大倒小)${作用域获取内容的名字}是根据作用域最小的取,指定作用域${session ...

  5. ubuntu Ngin Install

    安装gcc g++的依赖库 #apt-get install build-essential #apt-get install libtool 安装 pcre依赖库 #sudo apt-get upd ...

  6. 工具分享3:VMware 10虚拟机、MS-DOS 7.1、安装教程(MS-DOS环境安装)

    VMware 10工具下载地址: 网页下载链接:http://www.xp510.com/xiazai/ossoft/desktools/22610.html MS-DOS .10镜像下载地址: 网页 ...

  7. ACM_螺旋填数

    螺旋填数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一天,小明在研究蜗牛的壳时,对其螺旋状的花纹感到十分有趣.于是他回到了家 ...

  8. xhtml1-strict.dtd

    <!-- Extensible HTML version 1.0 Strict DTD This is the same as HTML 4 Strict except for changes ...

  9. 消除svn选定(checkout)桌面上文件显示一大堆问号。

    图片: 解决方法一: 桌面右键选择TortoiseSVN——>点击Settings,如下图,选中Icon Overlays(图标覆盖),去勾选Fixed drives(本地磁盘),点击确定,按F ...

  10. iphone(苹果)手机浏览器顶部下拉出现网页源

    在苹果手机下拉页面,会出现类似上图那样,具体方法如下: function handler(){//禁止默认滑动函数 event.preventDefault();}document.addEventL ...