lucene 5的测试程序——API变动太大
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变动太大的更多相关文章
- 生成的API分析文件太大。我们无法在交付前验证您的API使用信息。这只是通知信息。
这次使用了APICloud平台来开发移动APP, 发布的时候在api控制台云编译成ipa后,这次使用apple提供的Application Loader工具提交apa文件到iTunes上去,提交结束的 ...
- Unity3D占用内存太大的解决方法
原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...
- jvm内存快照dump文件太大,怎么分析
1.场景 通常,使用eclipse的mat图形化工具打开dump的时候都会内存溢出. 对于比较小的dump,eclipse可以打开,但一旦dump文件太大,eclipse就有点束手无策. 这时候怎么办 ...
- arcgis for android apk太大
原来大概都要20多M, 太大的原来是.so文件 arcgis for android api里面有armeabi armeabi-v7a x86的 每个so都接近10m 要是都保留就20多m了 由于 ...
- (转)Unity3D占用内存太大的解决方法
自:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. ...
- 如果程序太大而不能在DOS下运行,怎样才能使它在DOS下运行呢?
如果你的程序因太大(超过640KB)而无法在DOS下运行,有两种办法可为该程序提供更多的内存.一种办法是使用覆盖管理程序(overlay manager).覆盖管理程序用来管理程序的模块,并根据需要把 ...
- Unity3D占用内存太大怎么解决呢?
最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个 ...
- 35岁Android程序员被阿里辞退,生活压力太大痛哭,中年危机如何自救?
多数人都喜欢安逸的生活,尤其是随着年龄的增长,很多人都希望工作和生活趋于稳定,不愿意再让生活有很大的变动.可是,当达到一定的年龄时,危机还是存在的. 之前有一位阿里员工在脉脉上,晒出了自己被辞退的经历 ...
- 分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据
分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据 今天开发找我,说数据库insert不进数据,叫我看一下 他发了一个截图给我 然后我登录上服务器,发现了可疑的地方,而且这个数据库之前 ...
随机推荐
- 【shell】shell编程(四)-循环语句
上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...
- spl_autoload_register() && __autoload函数
一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数. 在index.php中,由于没有包含test.class.php,在实例化printit时 ...
- sgu 102模拟欧拉函数
感觉自己弱爆了,做做SGU吧... #include<iostream> #include<cmath> //欧拉函数 using namespace std; int eul ...
- POJ 1991 Turning in Homework(区间DP)
题目链接 Turning in Homework 考虑区间DP $f[i][j][0]$为只考虑区间$[i, j]$且最后在$a[i]$位置交作业的答案. $f[i][j][1]$为只考虑区间$[i, ...
- Codeforces 837 E Vasya's Function
Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程
C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程 串口的定义,请自行了解. C#操作串口通讯在.Net强大类库的支持下,只需要三个步骤: 1 创建 2 打开 3 发送/接受 ...
- u-boot简单学习笔记(一)
一:Bootloader启动结构: 由于 Boot Loader 的实现依赖于 CPU 的体系结构,因此大多数 Boot Loader 都分为 stage1 和 stage2 两大部分.依赖 ...
- ADO直接调用ACESS数据库MDB
1.ADO用ODBC链接不会出现堆栈溢出. 2.直接用ADO链接,因为对象不是NEW出来的,导致其成员变量也是栈上的,数组申请过大,栈溢出. 用VECTOR或者NEW对象,应该能解决.
- Gym - 100341C FFT优化DP
题目链接:传送门 题解: 设定dp[i][j]在深度为i下,使用j个节点的方案数 显然的转移方程组就是 dp[h][n] = dp[h-1][i] * dp[h-1][n-i-1] + 2*dp[h- ...