lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)
package lucene5; import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Lucene5Utils { private static String[] ids={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
private static String[] emails={"aa@aa.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org","cc@cc.org","dd@dd.org","bb@bb.org","ee@ee.org","ff@ff.org"};
private static String[] contents={"welcome to you","hello a boy","hello a girl","how are you","goog luck","oh shit","hello a boy","hello a girl","how are you","goog luck","oh shit","hello a boy","hello a girl","how are you","goog luck","oh shit","hello a boy","hello a girl","how are you","goog luck","oh shit","hello a boy","hello a girl","how are you","goog luck","oh shit","hello a boy","hello a girl","how are you","goog luck","oh shit"};
private static String[] names={"liwu","zhangsan","xiaoqinag","laona","dabao","lisi","zhangsan","xiaoqinag","laona","dabao","lisi","zhangsan","xiaoqinag","laona","dabao","lisi","zhangsan","xiaoqinag","laona","dabao","lisi","zhangsan","xiaoqinag","laona","dabao","lisi","zhangsan","xiaoqinag","laona","dabao","lisi"};
private static int[] attachs={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
private static Date[] dates=null ; //创建索引
private static Directory directory = null ;
//创建IndexWriter
private static IndexWriter indexWriter = null ;
//创建IndexReader
private static IndexReader indexReader = null ;
//创建searcher
private static IndexSearcher search = null ;
static {
try {
setDate() ;
directory = FSDirectory.open(Paths.get("D:\\lucene\\index")) ;
indexWriter = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer())) ;
indexReader = DirectoryReader.open(directory) ;
search = new IndexSearcher(indexReader);
} catch (IOException e) {
e.printStackTrace();
}
} public static void setDate() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-kk");
try {
dates=new Date[ids.length];
dates[0]=sdf.parse("2010-08-17");
dates[1]=sdf.parse("2011-02-17");
dates[2]=sdf.parse("2012-03-17");
dates[3]=sdf.parse("2011-04-17");
dates[4]=sdf.parse("2012-05-17");
dates[5]=sdf.parse("2011-07-17");
dates[6]=sdf.parse("2011-02-17");
dates[7]=sdf.parse("2012-03-17");
dates[8]=sdf.parse("2011-04-17");
dates[9]=sdf.parse("2012-05-17");
dates[10]=sdf.parse("2011-07-17");
dates[11]=sdf.parse("2011-02-17");
dates[12]=sdf.parse("2012-03-17");
dates[13]=sdf.parse("2011-04-17");
dates[14]=sdf.parse("2012-05-17");
dates[15]=sdf.parse("2011-07-17");
dates[16]=sdf.parse("2011-02-17");
dates[17]=sdf.parse("2012-03-17");
dates[18]=sdf.parse("2011-04-17");
dates[19]=sdf.parse("2012-05-17");
dates[20]=sdf.parse("2011-07-17");
dates[21]=sdf.parse("2011-02-17");
dates[22]=sdf.parse("2012-03-17");
dates[23]=sdf.parse("2011-04-17");
dates[24]=sdf.parse("2012-05-17");
dates[25]=sdf.parse("2011-07-17");
dates[26]=sdf.parse("2011-02-17");
dates[27]=sdf.parse("2012-03-17");
dates[28]=sdf.parse("2011-04-17");
dates[29]=sdf.parse("2012-05-17");
dates[30]=sdf.parse("2011-07-17");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// createIndex () ;
// delete() ;
// search () ;
// paserQuery() ;
searchBypage02(1,2,"welcome to you");
} /**
* 创建/更新索引
*/
public static void createIndex () {
try {
//清空当前所有索引
indexWriter.deleteAll() ;
//创建写入流
// reader = new java.io.BufferedReader(new InputStreamReader(new FileInputStream("D:\\lucene\\files\\产品名称-拼音.txt"),"GBK"));
for (int i = 0 ; i<=30; i++) {
Document d = new Document();
System.out.println(ids[i]+"-"+emails[i]+"-"+contents[i]+"-"+names[i]+"-"+attachs[i]+"-"+dates[i]);
d.add(new Field("id",ids[i],TextField.TYPE_STORED)) ;
d.add(new Field("email",emails[i],TextField.TYPE_STORED)) ;
d.add(new Field("content",contents[i],TextField.TYPE_NOT_STORED)) ;
d.add(new Field("name",names[i],TextField.TYPE_STORED)) ;
d.add(new IntField("attach",1,IntField.TYPE_STORED)) ;
d.add(new LongField("date",dates[i].getTime(),LongField.TYPE_STORED)) ;
indexWriter.addDocument(d) ;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//在这里writer必须要关掉或者commit。不然,会写入不进去数据
indexWriter.commit() ;
// indexWriter.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
} //简单的查询
public static void search (){
System.out.println(indexReader.maxDoc()); //输出所有的DOC数
System.out.println(indexReader.numDocs()); //输出可用的DOC数
System.out.println(indexReader.numDeletedDocs()); //输出删除的DOC数
try {
//获取搜索域--shouzimu
QueryParser parser = new QueryParser("email",new StandardAnalyzer()) ;
//设置查询条件
Query query = parser.parse("@dd.org~");
//设置查找结果 -最多为10条
TopDocs tocs = search.search(query,10) ;
//遍历查询结果
ScoreDoc[] docs = tocs.scoreDocs ;
for (ScoreDoc sd : docs) {
Document doc = search.doc(sd.doc) ;
//这里的content输出为null。因为我们没有保存哦~
System.out.println(doc.get("id")+":" + doc.get("email") + "-" + doc.get("content")+"-" + doc.get("name")+"-" + doc.get("attach")+"-" + doc.get("date"));
}
} catch (org.apache.lucene.queryparser.classic.ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //删除索引 public static void delete () {
try {
//删除,放置在回收站中,可恢复
indexWriter.deleteDocuments(new Term("shouzimu","xxxxlxw")) ;
//删除,不可恢复
indexWriter.forceMergeDeletes() ;
indexWriter.commit() ;
} catch (IOException e) {
e.printStackTrace();
}
} //5中的方法 暂时不会啊= =、
public static void undelete () {
} //详解ParserQuery
public static void paserQuery () {
//这里我们定义一个基于标准分词器的Query;搜索域默认为content
QueryParser parser = new QueryParser("content",new StandardAnalyzer()) ;
// parser.setAllowLeadingWildcard(true) ;
try {
//在默认域中查找 有you的
Query query = parser.parse("you");
//在默认域中查找有 boy 或者girl 的
query = parser.parse("boy girl");
query = parser.parse("boy OR girl");
//在name域中查号 name 为xiaoqinag的
query = parser.parse("name:xiaoqinag");
query = parser.parse("name : xiaoqinag");
//查找邮箱结尾为@dd.org的 通配符查询不能用?!!! 待解决
query = parser.parse("email:a?@aa.org"); //会报错 提示通配符不能放在首位
//在默认域中查找 有how 且有 you的
query = parser.parse("how AND you");
//在默认域中查找 有hello 但是没有 girl的 千万要注意空格!
query = parser.parse("-girl +hello");
//完全匹配hello a boy
query = parser.parse("\"hello a girl\"");
//显示id 2-4 的闭区间 TO大写
query = parser.parse("id:[2 TO 4]");
//显示id 2-4 的开区间 TO大写
query = parser.parse("id:{2 TO 4}");
//显示id hello 和 boy 之间有小于1的
query = parser.parse("\"hello boy\"~1");
//模糊查询
query = parser.parse("name:xiaoqiang~");
query = parser.parse("attach:{2 TO 4}");
TopDocs tdocs = search.search(query, 10) ;
ScoreDoc[] sdoc = tdocs.scoreDocs;
for (ScoreDoc sd : sdoc) {
Document doc = search.doc(sd.doc) ;
System.out.println(doc.get("id")+":" + doc.get("email") + "-" + doc.get("content")+"-" + doc.get("name")+"-" + doc.get("attach")+"-" + doc.get("date"));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //分页查询一
public static void searchBypage01 (int page,int pageSize,String str) {
int start = (page-1) * pageSize ;
int end = start + pageSize ;
QueryParser parser = new QueryParser("content",new StandardAnalyzer()) ;
try {
Query query = parser.parse(str) ;
TopDocs tdos = search.search(query, 100) ;
ScoreDoc[] sds = tdos.scoreDocs ;
for (int i = start ;i <end;i++ ) {
Document doc = search.doc(sds[i].doc) ;
System.out.println(doc.get("id")+":" + doc.get("email") + "-" + doc.get("content")+"-" + doc.get("name")+"-" + doc.get("attach")+"-" + doc.get("date"));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //分页查询二
public static void searchBypage02 (int page,int pageSize,String str) {
int start = (page-1) * pageSize ;
// int end = start + pageSize ;
QueryParser parser = new QueryParser("content",new StandardAnalyzer()) ;
try {
Query query = parser.parse(str) ;
TopDocs tdos = search.search(query, 100) ;
ScoreDoc[] sds = tdos.scoreDocs ;
tdos = search.searchAfter(sds[start], query, pageSize) ;
sds = tdos.scoreDocs ;
for (ScoreDoc sd : sds) {
Document doc = search.doc(sd.doc) ;
System.out.println(doc.get("id")+":" + doc.get("email") + "-" + doc.get("content")+"-" + doc.get("name")+"-" + doc.get("attach")+"-" + doc.get("date"));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)的更多相关文章
- TODO:MongoDB的查询更新删除总结
TODO:MongoDB的查询更新删除总结 常用查询,条件操作符查询,< .<=.>.>=.!= 对应 MongoDB的查询操作符是$lt.$lte.$gt.$gte.$ne ...
- Android Sqlite数据库执行插入查询更新删除的操作对比
下面是在Android4.0上,利用Sqlite数据库的insert,query,update,delete函数以及execSql,rawQuery函数执行插入,查询,更新,删除操作花费时间的对比结果 ...
- MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作
映射 在 MongoDB 中,映射(Projection)指的是只选择文档中的必要数据,而非全部数据.如果文档有 5 个字段,而你只需要显示 3 个,则只需选择 3 个字段即可. find() 方法 ...
- SQL入门(1): 创建/查询/更新/连接/视图/SSMS简介
本文介绍SQL的基本查询语句 (1) select... from * 表示全部, 选择的东西还可以进行简单的运算, 可以列别名 select * from student; -sage from ...
- Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]
在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一. ...
- python 读取 查询 更新 删除 sql2008 类及应用
import pymssql class MSSQL: def __init__(self,host,user,pwd,db): self.host = host self.user = user s ...
- MongoDB学习笔记二:创建、更新及删除文档
插入并保存文档 对目标集使用insert方法插入一个文档: > db.foo.insert({"bar" : "baz"}) 这个操作会给文档增加一个&q ...
- ElasticSearch基本操作(安装,索引的创建和删除,映射)
ElasticSearch基于Lucene的搜索服务器,支持分布式,提供REST接口,可用于云计算,可以实现实时搜索,开源免费.这时很官方的一句话,在使用之前,我们简单的介绍一下安装过程.在官网下载之 ...
- 三、MongoDB的创建、更新和删除
一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 概要 下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D( ...
随机推荐
- Opentaps安装小记
这周了解了个MES项目需求,于是乎找了些开源项目了解下,有php+mysql写的weberp,配置起来相当方便,下一版webERP_v4.13,往F:\Apache\htdocs\一放,启动F:\Ap ...
- REST架构之Apache Wink
Apache Wink是一个使用简单,稳定的Java框架,用于创建RESTful web services应用程序.Wink包括了一个服务器端模块和一个客户端模块,用于帮助开发者快速高效的开发REST ...
- Anaconda died after receiving signal 7
安装RHEL6-64时出现如下提示 Anaconda died after receiving signal 7 1 Anaconda是啥 是RedHat.CentOS.Fedora等Linux的安装 ...
- Unity3D 中的定时器
不算上 C# 自带的,目前知道两种,以下分别介绍. 1.每帧检查 定义一个时间变量 timer,每帧将此时间减去帧间隔时间 Time.deltaTime,如果小于或者等于零,说明定时器到了,执行相应功 ...
- 使用Axis2建立WebService
Axis是apache重量级的WebService框架,虽然相比Xfire和CXF而言相对比较臃肿,但是企业中最常用的就是Axis,Axis2是Axis的升级版: 建立一个最简单的Axis2 W ...
- JavaScript中常用语句
1.document.write( " "); 输出语句 2.JS中的行注释为:// 块注释:/**/ 3.传统的HTML文档顺序是:document- >html- > ...
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- 手机客户端UI测试常见的测试点
1.各种分辨率下,显示正常.现市场上主流的塞班V3系统手机为240*320.320*240.WM系统主要为240*320.320*480.Android系统主要为320*480,Iphone系统为32 ...
- iOS版本更新的App提交审核流程
App的版本更新估计是在所难免的了!更新App和新的App发布有何不同了?今天我们一起来看看吧!在发布App的时候我们需要通过开发者帐号——(申请)——>发布证书(需要钥匙串对证书签名也叫加密( ...
- IE9以下 placeholder兼容
//input placeholder兼容!(function ($, doc, win) { $.fn.placeholder = function () { var i = doc.createE ...