lucene索引的创建与搜索
package com.cs.multi;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.LockObtainFailedException;
import com.cs.itcast.FileToDocumentUtil;
import com.cs.tool.PrintDocumentUtil;
public class MultiDocument {
private static String dataPath = "D:\\work-tool\\workspace\\luencedemo\\datasource";
private static String indexPath = "D:\\work-tool\\workspace\\luencedemo\\directory";
private static Analyzer analyzer = new StandardAnalyzer();
public static void main(String[] args) throws Exception {
//createIndex();
//search();
}
//创建索引
private static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException, Exception {
File file = new File(dataPath);
IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true, MaxFieldLength.LIMITED);//四个参数是1:建立索引的位置,2:分词器,3:是否创建索引,4:索引的最大文档内容长度.MaxFieldLength.LIMITED表示:10000,MaxFieldLength.UNLIMITED表示无限大
if(file!=null&&file.isDirectory()){
File[] files = file.listFiles();
for (File f : files) {
Document document = FileToDocumentUtil.fileToDocument(f.getAbsolutePath());
indexWriter.addDocument(document);//添加文档索引
}
indexWriter.close();
}else{
return ;
}
}
//搜索
private static void search() throws CorruptIndexException, IOException{
IndexSearcher indexSearcher = new IndexSearcher(indexPath); //搜索索引的位置
/*第一种 构建Query
String keyword = "MySQL";
Query query = new TermQuery(new Term("content", keyword.toLowerCase())); //Term参数是filed字段,要查找的关键词
*/
/*第二种 构建Query
String[] fields = {"path","content","name"};
QueryParser queryParser = new MultiFieldQueryParser(fields,analyzer);//语言分析器 多字段的语言分析器 这个分词器要和创建索引的分词器一样
Query query = queryParser.parse(keyword);
*/
TopDocs topDocs = indexSearcher.search(query, null, 10000); //查找并返回结果集
int totalHits = topDocs.totalHits; //查询出来的文档总数
System.out.println("查询出来的文档总数为:【"+totalHits+"】条记录数");
//遍历结果集
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int doc = scoreDoc.doc;//文档编号
Document document = indexSearcher.doc(doc);//得到该编号的文档
PrintDocumentUtil.print(document);//打印文档内容
}
indexSearcher.close();
}
}
//工具类 将File转化为Document 该Document是lucene的
import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import com.cs.tool.FileToContent;
public class FileToDocumentUtil {
public static Document fileToDocument(String path) throws Exception{
File file = new File(path);
Document doc = new Document();
//后面两个属性是建立索引和分词
doc.add(new Field("name",file.getName(),Store.YES,Index.ANALYZED));
doc.add(new Field("path",file.getCanonicalPath(),Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("content",FileToContent.fileToContent(file),Store.YES,Index.ANALYZED));
doc.add(new Field("length",file.length()+"",Store.YES,Index.NO));
return doc;
}
}
package com.cs.tool;
import org.apache.lucene.document.Document;
public class PrintDocumentUtil {
//打印文档内容
public static void print(Document doc){
System.out.println("文档路径为"+doc.get("path"));//该Document记录下的字段
System.out.println("文档名称为"+doc.get("name"));
System.out.println("文档内容为"+doc.get("content"));
System.out.println("文档长度为"+doc.get("length"));
}
}
lucene索引的创建与搜索的更多相关文章
- Lucene索引库维护、搜索、中文分词器
删除索引(文档) 需求 某些图书不再出版销售了,我们需要从索引库中移除该图书. 1 @Test 2 public void deleteIndex() throws Exception { 3 // ...
- Lucene——索引的创建、删除、修改
package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import ...
- *lucene索引_创建_域选项
[索引建立步骤] [创建Directory] [创建writer] [创建文档并添加索引] 文档和域的概念很重要 文档相当于表中的每一条记录,域相当于表中的每一个字段. [查询索引的基本信息] 使用I ...
- Apache Lucene(全文检索引擎)—创建索引
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理
1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...
- Lucene 4.7 --创建索引
Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...
- Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索
Lucene 的四大索引查询 清单1:使用布尔操作符 Java代码 //Test boolean operator blic void testOperator(String indexD ...
- 42、lucene和机器学习进行全文搜索,并排序
package com.lucene.test; import java.io.BufferedInputStream; import java.io.File; import java.io.Fil ...
- paip.lucene 4.3 中文语义搜索最佳实践
paip.lucene 4.3 中文语义搜索最佳实践 首先一个问题是要不要使用lucene 自带的分词器...我觉得最好不使用哪自带的分词器.效果还凑火,就是不好控制... 先使用ik,ict,mms ...
随机推荐
- WPF Application
Application类作为启动的入口,在VS中,通常自动代码为我们继承了Application类,这样做的有点,我还没有理解到,但是我们先学到这个知识点. 为了能够更好的控制整个启动过程,包括得到A ...
- MySQL数据库安装(CentOS操作系统/tar.gz方式)
1. 上传Mysql安装包“mysql-5.5.40-linux2.6-x86_64.tar.gz”到部署机,位置任意: 2. 将Mysql安装包解压到其所在目录,命令如下: -linux2.-x86 ...
- 面试题-Java Web-Servlet部分
1.什么是Servlet? Servlet是用来处理客户端请求并产生动态网页内容的Java类.Servlet主要是用来处理或者是存储HTML表单提交的数据,产生动态内容,在无状态的HTTP协议下管理状 ...
- Python 智能处理方向的工具
机器视觉类:OpenCV. 自然语言处理:NLTK, jieba(Python中文分词组件),HanLP, FudanNLP, NLPIR, http://tm.itc.ntnu.edu.tw/CNL ...
- python setup.py install 报错ImportError: No module named setuptools
学习光荣之路python课程时,使用python setup.py install安装其他模块时,第一次安装某模块成功了.安装另一模块却报错ImportError: No module named s ...
- python 学习 [day7]面向对象
编程模式概念 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向对象的三大 ...
- Shell学习笔记 ——第二天
1.显示日期 date | cal cal 2010 cal 2 2010 2.改变文件拥有者 chown 3.改变文件权限 chmod 4.显示当前目录 pwd 5.查看文件尾部内容,并 ...
- JAVA内容回顾(二)——面向对象(OOP)
1.类与对象 类:类指的是同种对象的抽象,看不见摸不着的.包含有属性与方法. 对象:是类的具体实现,看的见摸得着的东西. 类是对象的抽象,对象是类的具体实现. 2.访问修饰符 public:在项目的任 ...
- maven添加本地依赖包方法
1.某些情况下不方便上传本地依赖包到Maven repository,可以通过下面方法添加本地依赖包. 2.方法 1).pom.xml中添加以下代码块 <dependency> <g ...
- js中三目运算符和&& || 符的个人浅见
这两天看到别人写的代码,感觉很牛逼,如下,大神请忽视 $(".lgn").on("click", function() { var a = {}; a.logi ...