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. Google Code Jam在线測试题目--Alien Language

    Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...

  2. nyoj860 又见01背包(背包变形)

    题目860 pid=860" style="text-decoration:none; color:rgb(55,119,188)">题目信息 执行结果 本题排行 ...

  3. IOS7中动态计算UILable的高度

    .h文件 #import <UIKit/UIKit.h> @interface UILabel (ContentSize) - (CGSize)contentSize; @end .m文件 ...

  4. oc54--auatorelease应用场景

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonat ...

  5. ubuntu如何完全卸载和安装 Java及android环境?【转】

    本文转载自:https://my.oschina.net/lxrm/blog/110638 最近,迷上了java,一时间什么环境变量/虚拟机都猛然袭来,有点不适.环境配置在前,这所自然.平时搞PHP都 ...

  6. golang LMDB入门例子——key range查询

    如下,使用gomb库 package main import ( "bytes" "fmt" "io/ioutil" "os&qu ...

  7. hdu 6112 今夕何夕(模拟)

    今夕何夕 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. H264的RTP负载打包的数据包格式,分组,分片

    H264的RTP负载打包的数据包格式,分组,分片 1.    RTP数据包格式 RTP报文头格式(见RFC3550 Page12): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ...

  9. simpleOS 1.0

    做了一个so simple的OS,本不好意思多说的....不过还是说下吧. 首先,买不起开发板的我没有办完完成一件事,那就是保存任务上下文,因为这个过程实际上是将寄存器的值存放到任务堆栈中去的. 而要 ...

  10. Linux 安装配置JDK 、 MySQL 、nginx

    今天我来讲一下在Linux下各环境的搭建,主要就讲一下jdk.MySQL.和一个代理服务器nginx 1. jdk的安装配置 1)卸载自带openjdk 当我们拿到一个全新的ECS的时候上面有的会自带 ...