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. unity的List构造函数在IOS平台存在缺陷

    当迩使用一个int[]或者string[]类似的数组时,以数组来初始化List对象,有可能在IOS平台上会出现初始化对象为空,比如 , }; List<int> listTest = ne ...

  2. webStorm汉化

    http://www.sdbeta.com/xiazai/2015/0603/35070.html 注册时选择“License server”输入“http://idea.imsxm.com/”点击“ ...

  3. 某考试 T1 sigfib

    设 g(x) = f(x) * x ,多项式 A = Σ g(i) * x^i , 多项式  B = Σ f(i) * x^i. 首先,g(x) = g(x-1) + g(x-2) + f(x-1) ...

  4. UVA 11346 Probability

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 3 10 5 20 1 1 1 2 2 0 输出样例#1: 23.348371% 0.000000% 100.00 ...

  5. Win10 - 默认图片查看器恢复

    1. 新建一个文本文件“1.txt” 2. 在“1.txt”中添加如下代码,并保存: Windows Registry Editor Version 5.00 ; Change Extension's ...

  6. Unity3d中制作异步Loading进度条所遇到的问题

    背景 通常游戏的主场景包括的资源较多,这会导致载入场景的时间较长.为了避免这个问题,能够首先载入Loading场景.然后再通过Loading场景来载入主场景. 由于Loading场景包括的资源较少,所 ...

  7. PAT 1003 Sharing (25)

    题目描写叙述 To store English words, one method is to use linked lists and store a word letter by letter. ...

  8. Dubbo简单介绍及实例

    1.概念 Dubbo是一个分布式服务框架,以及阿里巴巴内部的SOA服务化治理方案的核心框架.其功能主要包含:高性能NIO通讯及多协议集成.服务动态寻址与路由.软负载均衡与容错,依赖分析与降级等. 说通 ...

  9. 利用卷积神经网络(CNN)构造社区问答系统

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/                                                     author: 张俊林 ...

  10. 字符串类型ip与数值型ip地址相互转换

    /** * 返回Integer类型的ip地址 * @return */ private static Integer ipToInt(){ String ip="192.168.1.201& ...