使用Lucene的java api 写入和读取索引库
import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericDocValuesField;
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.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;
import org.junit.Test; import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths; public class Day01 {
public String dataDir="E:\\data";
//2.search检索
@Test
public void search() throws IOException, ParseException {
//01.
Path path=Paths.get("./luceneindex/");
Directory directory=FSDirectory.open(path);
IndexReader indexReader=DirectoryReader.open(directory);
//索引查询工具
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
String word="战斗";
QueryParser queryParser=new QueryParser("filename",new StandardAnalyzer());
Query query=queryParser.parse(word);
TopDocs topDocs = indexSearcher.search(query, 10); long count = topDocs.totalHits;
System.out.println("总记录数:"+count); ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc item:scoreDocs) {
int docid = item.doc; //文档编号
Document document = indexReader.document(docid);
String filename = document.get("filename");
System.out.println(filename);
}
}
@Test
//1.创建索引
public void createIndex() throws IOException {
//01.准备一个写入索引的目录
Path path = Paths.get("./luceneindex/");
//02.将路径与Directory进行绑定
Directory directory=FSDirectory.open(path);
//03.这行code在底层默认已经关联绑定了一个StandardAnalyzer 标准分词器
IndexWriterConfig config=new IndexWriterConfig();
//04.给config设置一个模式 Create:每次都抹掉原来的索引文件,重新构建新的索引文件
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
//05.索引写入器需要两个入参,一个是目录,另一个是配置
IndexWriter writer=new IndexWriter(directory,config);
//06.准备加入索引库的内容
File file = new File(dataDir);
//07.某个目录下的文件集合
File[] files = file.listFiles();
for (File item:files) {
//08.将内容幻化成----->Document
Document document=new Document();
String filename=item.getName(); //file--->String
System.out.println(filename);
System.out.println("===============================");
String filecontent=FileUtils.readFileToString(item);
Long modifydate=item.lastModified();
//09.真正的给Document的field赋值
document.add(new TextField("filename",filename,Field.Store.YES));
document.add(new TextField("filecontent",filecontent,Field.Store.YES));
document.add(new NumericDocValuesField("modifydate",modifydate));
//10.将单个Document(一条记录) 保存到 索引库中
writer.addDocument(document);
}
//11.关闭索引库
writer.close();
System.out.println("add indexes ok!");
}
}
使用Lucene的java api 写入和读取索引库的更多相关文章
- 【Lucene】具体解释Lucene全文检索的信息写入与读取
Lucene的大致结构图: 信息写入索引库的过程: 读取信息的过程: 以下是一个向索引库写入信息与读取信息的样例: public void testCreateIndex() throws Excep ...
- Lucene 05 - 使用Lucene的Java API实现分页查询
目录 1 Lucene的分页查询 2 代码示例 3 分页查询结果 1 Lucene的分页查询 搜索内容过多时, 需要考虑分页显示, 像这样: 说明: Lucene的分页查询是在内存中实现的. 2 代码 ...
- Java文件写入与读取实例求最大子数组
出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...
- [搜索]ElasticSearch Java Api(一) -添加数据创建索引
转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...
- Elasticsearch的CRUD:REST与Java API
CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...
- Lucene 02 - Lucene的入门程序(Java API的简单使用)
目录 1 准备环境 2 准备数据 3 创建工程 3.1 创建Maven Project(打包方式选jar即可) 3.2 配置pom.xml, 导入依赖 4 编写基础代码 4.1 编写图书POJO 4. ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- java一行一行写入或读取数据
原文:http://www.cnblogs.com/linjiqin/archive/2011/03/23/1992250.html 假如E:/phsftp/evdokey目录下有个evdokey_2 ...
- Java Web SSH框架总是无法写入无法读取Cookie
不关乎技术,关乎一个小Tips: 默认情况下,IE和Chrome内核的浏览器会认为http://localhost为无效的域名,所以不会保存它的cookie,使用http://127.0.0.1访问程 ...
随机推荐
- 551 Student Attendance Record I 学生出勤纪录 I
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场如果一个学生的出勤纪 ...
- 页面在Native端滚动时模拟原生的弹性滚动效果
width: 100%;overflow: scroll;overflow-y: hidden;-webkit-overflow-scrolling: touch; ---- 对应的滚动内容内添加 ...
- kafka基础四
消费者消费过程(二) 消费组状态机:消息的产生存储消费看似是杂乱无章的,但万物都会遵循一定的规则成长,任何事物的发展都是有迹可循的. 开始消费组初始状态为Stable,经过第一次Rebalance之后 ...
- codeforces1025
hackforces + fstforces A 很明显当有一个字母出现次数>1时即合法 $n = 1$的情况需要特判 #include<cstdio> #include<ve ...
- html的meta总结
引子 之前的我的博客中对于meta有个介绍,例如:http://www.haorooms.com/post/liulanq_think_ie 浏览器安全性想到的这篇文章,中间介绍了meta下面IE的一 ...
- 关于Retrofit + RxJava 的使用
年前一个月到现在,一直都在忙一个项目.项目使用的三方框架还是蛮多的. 下面来总结一下自己使用Retrofit + RxJava的知识点吧. (以下讲述从一个请求的最初开始) 1.首先定义一个RxMan ...
- NF!=1
NF表示列数,不等于1表示列数不为1列
- Spring 配置定时器(注解+xml)方式—整理
一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.or ...
- Oracle错误(包括PL/SQL)集合与修复
+-----------------------------------------------------------------------+ | 在本篇随笔中,仅根据个人经验累积错误进行描述 ...
- afnetworking NSCocoaErrorDomain Code=3840 解决
afnetworking json解析出错 解决方法1 AFURLResponseSerialization.m 258行修改 responseString = [responseString str ...