package com.lucene.controller;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import com.lucene.entity.Company;
import com.lucene.jdbc.Jdbc;
public class AnalyseControl {
/* 创建简单中文分析器 创建索引使用的分词器必须和查询时候使用的分词器一样,否则查询不到想要的结果 */
private Analyzer analyzer = new IKAnalyzer(true);
// 索引保存目录
private File indexFile = new File("./indexDir/");
/**
* 创建索引文件到磁盘中永久保存
*/
public void createIndexFile() {
long startTime = System.currentTimeMillis();
System.out.println("*****************创建索引开始**********************");
Directory directory = null;
IndexWriter indexWriter = null;
try {
// 创建哪个版本的IndexWriterConfig,根据参数可知lucene是向下兼容的,选择对应的版本就好
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
// 创建磁盘目录对象
directory = new SimpleFSDirectory(indexFile);
indexWriter = new IndexWriter(directory, indexWriterConfig);
// indexWriter = new IndexWriter(directory, analyzer, true,IndexWriter.MaxFieldLength.UNLIMITED);
// 这上面是使用内存保存索引的创建索引写入对象的例子,和这里的实现方式不一样,但是效果是一样的
Jdbc jdbcdemo=new Jdbc();
List<Company> CompanyList =jdbcdemo.jdbc();
// 为了避免重复插入数据,每次测试前 先删除之前的索引
indexWriter.deleteAll();
// 获取实体对象
for (int i = 0; i < CompanyList.size(); i++) {
Company company = CompanyList.get(i);
// indexWriter添加索引
Document doc = new Document();
// doc.add(new Field("id", company.getEntID(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("name", company.getEntName(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("regno", company.getREGNO(),Field.Store.YES, Field.Index.NO));
doc.add(new Field("orgno", company.getORGNO(),Field.Store.YES, Field.Index.NO));
doc.add(new Field("dom", company.getDOM(),Field.Store.YES, Field.Index.NO));
doc.add(new Field("lerep", company.getLEREP(),Field.Store.YES, Field.Index.NO));
// doc.add(new Field("regcap", company.getREGCAP(),Field.Store.YES, Field.Index.NO));
doc.add(new Field("enttype", company.getENTTYPE(),Field.Store.YES, Field.Index.NO));
// doc.add(new Field("opscope", company.getOPSCOPE(),Field.Store.YES, Field.Index.NO));
// doc.add(new Field("domdistrict", company.getDOMDISTRICT(),Field.Store.YES, Field.Index.NO));
// doc.add(new Field("regnorg", company.getREGNO(),Field.Store.YES, Field.Index.NO));
// 添加到索引中去
indexWriter.addDocument(doc);
// System.out.println("索引添加成功:第" + (i + 1) + "次!!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (indexWriter != null) {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("创建索引文件成功,总共花费" + (endTime - startTime) + "毫秒。");
System.out.println("*****************创建索引结束**********************");
}
/**
* 直接读取索引文件,查询索引记录
*
* @throws IOException
*/
public void openIndexFile() {
long startTime = System.currentTimeMillis();
System.out.println("*****************读取索引开始**********************");
List<Company> companys = new ArrayList<Company>();
// 得到索引的目录
Directory directory = null;
IndexReader indexReader = null;
try {
directory = new SimpleFSDirectory(indexFile);
// 根据目录打开一个indexReader
indexReader = IndexReader.open(directory);
//indexReader = IndexReader.open(directory,false);
System.out.println("在索引文件中总共插入了" + indexReader.maxDoc() + "条记录。");
// 获取第一个插入的document对象
Document minDoc = indexReader.document(0);
// 获取最后一个插入的document对象
Document maxDoc = indexReader.document(indexReader.maxDoc() - 1);
// document对象的get(字段名称)方法获取字段的值
System.out.println("第一个插入的document对象的标题是:" + minDoc.get("name"));
System.out.println("最后一个插入的document对象的标题是:" + maxDoc.get("name"));
//indexReader.deleteDocument(0);
int docLength = indexReader.maxDoc();
for (int i = 0; i < docLength; i++) {
Document doc = indexReader.document(i);
Company company = new Company();
if (doc.get("name") == null) {
System.out.println("name为空");
} else {
// company.setEntID(doc.get("id"));
company.setEntName(doc.get("name"));
company.setREGNO(doc.get("regno"));
company.setORGNO(doc.get("orgno"));
company.setDOM(doc.get("dom"));
company.setLEREP(doc.get("lerep"));
// company.setREGCAP(doc.get("regcap"));
company.setENTTYPE(doc.get("enttype"));
// company.setOPSCOPE(doc.get("opscope"));
// company.setDOMDISTRICT(doc.get("domdistrict"));
// company.setREGNO(doc.get("regnorg"));
companys.add(company);
}
}
System.out.println("显示所有插入的索引记录:");
/*for (Company company : companys) {
System.out.println(company);
}*/
} catch (IOException e) {
e.printStackTrace();
} finally {
if (indexReader != null) {
try {
index
没有合适的资源?快使用搜索试试~ 我知道了~
基于Lucene的搜索引擎的实现
共50个文件
class:17个
java:17个
prefs:2个
需积分: 50 63 下载量 187 浏览量
2018-07-23
10:22:35
上传
评论 6
收藏 540KB ZIP 举报
温馨提示
完整代码,基于Lucene的分词,根据搜索引擎的目标和基本内容,将实现功能模块主要划分为创建中文分析器(创建索引)、读取索引文件查询记录、根据输入的内容进行分词、根据关键字进行全文检索、将结果按JSON格式输出到前台。修改数据库连接,以及索引的Field的值(可参照我的文章《索引的建立》进行修改,即可用于其他功能)
资源推荐
资源详情
资源评论
收起资源包目录
Analyse.zip (50个子文件)
Analyse
src
com
lucene
entity
Company.java 2KB
CompanyList.java 574B
jdbc
Jdbc.java 2KB
controller
AnaAction.java 2KB
AnalyseAction.java 2KB
AnalyseControl.java 14KB
impl
AnalyseNameImpl.java 340B
service
AnalyseName.java 183B
json
Utils
JsonUtils.java 841B
core
AbstractJsonObject.java 887B
ListObject.java 239B
SingleObject.java 221B
writer
JsonWriter.java 3KB
status
StatusCode.java 490B
StatusObject.java 462B
StatusHouse.java 1KB
responseUtils
ResponseUtils.java 2KB
.classpath 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.core.resources.prefs 273B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 570B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 1KB
Images
2.png 4KB
image.gif 490KB
1.png 3KB
WEB-INF
classes
com
lucene
entity
Company.class 2KB
CompanyList.class 986B
jdbc
Jdbc.class 2KB
controller
AnalyseControl.class 13KB
AnaAction.class 2KB
AnalyseAction.class 2KB
impl
AnalyseNameImpl.class 623B
service
AnalyseName.class 269B
json
Utils
JsonUtils.class 1KB
core
SingleObject.class 549B
ListObject.class 684B
AbstractJsonObject.class 1KB
writer
JsonWriter.class 3KB
status
StatusCode.class 708B
StatusHouse.class 2KB
StatusObject.class 798B
responseUtils
ResponseUtils.class 2KB
main.html 831B
lib
web.xml 1KB
共 50 条
- 1
资源评论
zhou_pp
- 粉丝: 155
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功