使用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访问程 ...
随机推荐
- HDU 5974 A Simple Math Problem 数学题
http://acm.hdu.edu.cn/showproblem.php?pid=5974 遇到数学题真的跪.. 题目要求 X + Y = a lcm(X, Y) = b 设c = gcd(x, y ...
- WPF CanExecuteChanged
继承ICommand ,RelayCommand命令 public class RelayCommand : ICommand { private readonly Action _execute; ...
- MySql中查询语句实现分页功能
import java.util.*;import java.sql.*; public class FruitDao { private Connection conn; private ...
- Struts2标签<s:checkboxlist>回显问题
Struts2 checkboxlist回显问题中,说明两种方式,第一种方式很普遍,第二种则是个人根据现有资源加上尝试得来的成果,第二种主要是为个人笔记(其中相关知识点不一一介绍). 一.普通方法: ...
- iOS Block的本质(二)
iOS Block的本质(二) 1. 介绍引入block本质 通过上一篇文章Block的本质(一)已经基本对block的底层结构有了基本的认识,block的底层就是__main_block_impl_ ...
- UVA 247 - Calling Circles (Floyd)
互相可以打电话是一个传递关系,所以Floyd求传递封包,dfs找一个尽量大的圈. #include<bits/stdc++.h> using namespace std; ; map< ...
- SpringMVC、Spring和Struts的区别
http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...
- python打开.pkl的文件并显示里面的内容
pkl文件是pyhthon里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西.正确的打开方式如下: import cPickle as pickle f = open('path') info ...
- 真爱 vs. 种姓:新一代印度人的婚恋观
今日导读 “自由恋爱”是所有世界上所有有情人共同的心愿,而在印度,因为其根深蒂固的种姓制度,仍然有大批情侣只能听从父母的“包办婚姻”,被迫与心爱的人分离.但是最新的一项调查表明,印度的年轻一代开始出现 ...
- Bootstrap历练实例:基本输入框组
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...