package hello;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map.Entry; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class HelloLucene222 { public static void main(String[] args) {
HelloLucene222 hLucene = new HelloLucene222();
hLucene.index();
System.out.print("search ...\n");
hLucene.search();
} // 建立索引
public void index() {
System.out.println("Indexing to directory begin...");
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("sleep OK");
long start = System.currentTimeMillis();
IndexWriter writer = null;
try {
// 1、创建Directory
// Directory directory = new RAMDirectory();//索引是建立在内存中的
Directory directory = FSDirectory.open(Paths.get("C:\\exp\\test_data\\index"));// 创建在硬盘上
// 2、创建IndexWriter
IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
writer = new IndexWriter(directory, iwc);
// 3、创建Document对象
Document doc = null;
// 4、为Document添加Field,是Document的一个子元素
// File file = new File("D:\\exp\\test_data\\txt");
File file = new File("C:\\exp\\test_data\\ES");
for (File f : file.listFiles()) {
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line = null;
int i = 0;
Gson gson = new Gson();
while ((line = br.readLine()) != null) {
// process the line.
if ((i & 1) == 1) {
// System.out.println(line);
HashMap<String, String> events = gson.fromJson(line,
new TypeToken<HashMap<String, String>>() {
}.getType());
// System.out.println(events);
doc = new Document();
for (Entry<String, String> entry : events.entrySet()) {
doc.add(new TextField(entry.getKey(), entry.getValue(), Field.Store.NO));
// doc.add(new Field("filename", f.getName(),
// Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("path", f.getAbsolutePath(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 5、通过IndexWriter添加文档到索引中
}
writer.addDocument(doc);
}
i += 1;
}
}
System.out.println("Indexing to directory '" + f.getAbsolutePath() + "'...");
}
long end = System.currentTimeMillis();
System.out.println("add docment Took : " + ((end - start) / 1000.0));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println("Took : " + ((end - start) / 1000.0));
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("sleep OK");
} // 搜索
public void search() {
long start = System.currentTimeMillis();
Directory directory;
try {
// 1、创建Directory
directory = FSDirectory.open(Paths.get("C:\\exp\\test_data\\index"));
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
// 4、创建搜索的Query
// 创建QueryParser来确定要搜索文件的内容,第二个参数表示搜索的域
QueryParser parser = new QueryParser("field-38", new StandardAnalyzer());
// 创建Query,表示搜索域为content中包含java的文档
Query query = parser.parse("tcholo");
// 5、根据searcher搜索并且返回TopDocs
TopDocs tdoc = isearcher.search(query, 10);// 只会显示10条内容 // 6、根据TopDocs获取ScoreDoc对象
ScoreDoc sdocs[] = tdoc.scoreDocs;
for (ScoreDoc s : sdocs) {
// 7、根据searcher行业ScoreDoc获取具体的Document对象
Document document = isearcher.doc(s.doc);
// 8、根据Document对象获取所需要的值
System.out.println(document.get("filename") + "[" + document.get("path") + "]");
}
// 9、关闭reader
ireader.close();
directory.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("Took : " + ((end - start) / 1000.0));
} }

lucene 5的测试程序——API变动太大的更多相关文章

  1. 生成的API分析文件太大。我们无法在交付前验证您的API使用信息。这只是通知信息。

    这次使用了APICloud平台来开发移动APP, 发布的时候在api控制台云编译成ipa后,这次使用apple提供的Application Loader工具提交apa文件到iTunes上去,提交结束的 ...

  2. Unity3D占用内存太大的解决方法

    原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...

  3. jvm内存快照dump文件太大,怎么分析

    1.场景 通常,使用eclipse的mat图形化工具打开dump的时候都会内存溢出. 对于比较小的dump,eclipse可以打开,但一旦dump文件太大,eclipse就有点束手无策. 这时候怎么办 ...

  4. arcgis for android apk太大

    原来大概都要20多M, 太大的原来是.so文件 arcgis for android api里面有armeabi armeabi-v7a  x86的 每个so都接近10m 要是都保留就20多m了 由于 ...

  5. (转)Unity3D占用内存太大的解决方法

    自:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大.  ...

  6. 如果程序太大而不能在DOS下运行,怎样才能使它在DOS下运行呢?

    如果你的程序因太大(超过640KB)而无法在DOS下运行,有两种办法可为该程序提供更多的内存.一种办法是使用覆盖管理程序(overlay manager).覆盖管理程序用来管理程序的模块,并根据需要把 ...

  7. Unity3D占用内存太大怎么解决呢?

    最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个 ...

  8. 35岁Android程序员被阿里辞退,生活压力太大痛哭,中年危机如何自救?

    多数人都喜欢安逸的生活,尤其是随着年龄的增长,很多人都希望工作和生活趋于稳定,不愿意再让生活有很大的变动.可是,当达到一定的年龄时,危机还是存在的. 之前有一位阿里员工在脉脉上,晒出了自己被辞退的经历 ...

  9. 分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据

    分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据 今天开发找我,说数据库insert不进数据,叫我看一下 他发了一个截图给我 然后我登录上服务器,发现了可疑的地方,而且这个数据库之前 ...

随机推荐

  1. android 禁止ViewPager滑动

    最近项目中,有个需求就是要禁止ViewPager滑动事件,我们看下360手机助手的界面,风格就类似这样的 大家如果使用过360手机助手就会发现中间内容是不可以滑动的,现在写一个demo,讲下怎么禁止V ...

  2. HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的 ...

  3. (43)C#网络1 http

    一.HttpClient类 用于发送http请求,并接受请求的相应 (从4.5起开始可用) using System.Net.Http; 异步调用 HttpClient httpClient = ne ...

  4. 阻塞IO下的echo回显实验

    我们只关心代码中的业务逻辑和底层阻塞原因 客户端代码 发送数据 std::string message(len, 'S'); int nw = stream->sendAll(message.c ...

  5. I.Tower Defense

    给你p个重塔,q个轻塔,把这些塔放在n*m的图中,这些塔会相互攻击同行同列的,轻塔不能受到攻击,重塔能承受一个塔的攻击, 问放的方法数. 先假定n < m. 可以先枚举放轻塔的个数为s,显然,方 ...

  6. 属性font-family:Font property font-family does not have generic default

    以前定义字体都是用的常用的字体,也没注意过会有这个提示,昨天在写界面的时候重新定义了一个本地没有的字体,发现会有提示. W3C的文档: font-family:<family-name>, ...

  7. ACM用到的算法。先做个笔记,记一下

    ACM 所有算法 数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 ...

  8. emacs 下 common lisp 配置

    安装 sbcl .emacs 加入 ;for lisp mode (add-to-list 'load-path "D:/kuaipan/.emacs.d/elpa/slime-201311 ...

  9. bootstrap -- col-sm-6 和 col-xs-6

  10. Web常见安全漏洞原理及防范-学习笔记

    公司在i春秋上面报的一个课程.http://www.ichunqiu.com/course/55885,视频学习. OWASP (Open Web Application Secutiry Proje ...