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学习 - 索引基本操作(创建,查询,更新,删除,分页)的更多相关文章

  1. TODO:MongoDB的查询更新删除总结

    TODO:MongoDB的查询更新删除总结 常用查询,条件操作符查询,< .<=.>.>=.!= 对应 MongoDB的查询操作符是$lt.$lte.$gt.$gte.$ne ...

  2. Android Sqlite数据库执行插入查询更新删除的操作对比

    下面是在Android4.0上,利用Sqlite数据库的insert,query,update,delete函数以及execSql,rawQuery函数执行插入,查询,更新,删除操作花费时间的对比结果 ...

  3. MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作

    映射 在 MongoDB 中,映射(Projection)指的是只选择文档中的必要数据,而非全部数据.如果文档有 5 个字段,而你只需要显示 3 个,则只需选择 3 个字段即可. find() 方法 ...

  4. SQL入门(1): 创建/查询/更新/连接/视图/SSMS简介

    本文介绍SQL的基本查询语句 (1) select... from  * 表示全部, 选择的东西还可以进行简单的运算, 可以列别名 select * from student; -sage from ...

  5. Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]

    在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一. ...

  6. python 读取 查询 更新 删除 sql2008 类及应用

    import pymssql class MSSQL: def __init__(self,host,user,pwd,db): self.host = host self.user = user s ...

  7. MongoDB学习笔记二:创建、更新及删除文档

    插入并保存文档 对目标集使用insert方法插入一个文档: > db.foo.insert({"bar" : "baz"}) 这个操作会给文档增加一个&q ...

  8. ElasticSearch基本操作(安装,索引的创建和删除,映射)

    ElasticSearch基于Lucene的搜索服务器,支持分布式,提供REST接口,可用于云计算,可以实现实时搜索,开源免费.这时很官方的一句话,在使用之前,我们简单的介绍一下安装过程.在官网下载之 ...

  9. 三、MongoDB的创建、更新和删除

    一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 概要 下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D( ...

随机推荐

  1. Java 文本文件 读写

    Use File/FileInputStream/FileOutputStream. public void testWithFIS() throws IOException{ File file=n ...

  2. iOS适配https详解

    马上就要元旦了,网上流传元旦之后苹果会对所有的app进行https的验证,据说会拒绝所有没有使用https的app.但是后来又听说是我们开发者误解了,元旦过后还是会支持http,不过开发者需要说明为什 ...

  3. Python下载进度条

    import dialog,e32,appuifw def ru(x):return x.decode('utf8') def qu(): appuifw.note(ru('已取消')) wait.c ...

  4. RapidJson读取json文档

    Json格式定义如下 Object: { _Name:_Data,... } 最后一项后面没有逗号 Array: [_Data,_Data,...] 最后一项后面没有逗号 _Name: String ...

  5. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  6. 在VC++6.0开发中实现全屏显示

    全屏显示是一些应用软件程序必不可少的功能.比如在用VC++编辑工程源文件或编辑对话框等资源时,选择菜单“View\Full Screen”,即可进入全屏显示状态,按“Esc”键后会退出全屏显示状态. ...

  7. WinForm 窗体基本属性、公共控件

    一.WinForm:客户端程序制作 - C/S (B/S:服务器端) 它是基于.NET Framework框架上运行,不是必须在windows系统上才能运行---------------------- ...

  8. AutoCAD安装失败

    问题一: Installing .NET Framework Runtime 4.0: D:\安装包\CAD\cad2012(x64)\Map3D2012(x64)\3rdParty\NET\4\wc ...

  9. C#读取Excel文件

    //excel模板的路径 string strSource = Application.StartupPath + "\\" + "ExcelTemplet" ...

  10. NetworkComms V3 使用Json序列化器进行网络通信

    刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...