java搜索引擎代码 java搜索功能怎么实现流程

怎么用java 开发一个搜索引擎呀?一.创建索引
1.一般创建索引的核心步骤
(1). 创建索引写入对象IndexWriter:
IndexWriter indexWriter = new IndexWriter(INDEX_STORE_PATH,new StandardAnalyzer(),create);
参数说明:INDEX_STORE_PATH:索引文件存放路径
new StandardAnalyzer(): 分词工具
create: 此参数为Boolean型,true表示重新创建整个索引, false 表示增量式创建索引 。
(2).创建文档模型,并用IndexWriter对象写入
Document doc = new Document();
Field field1 = new Field(fieldName1, fieldValue ,Field.Store.YES, Field.Index.TOKENIZED);
doc.add(field1);
Field field2 = new Field(fieldName2, fieldValue ,Field.Store.YES, Field.Index.TOKENIZED);
doc.add(field2);
……
indexWriter.addDocument(doc);
indexWriter.close();
参数说明:
Document :负责搜集数据源,它可以从不同的物理文件提取数据并放入同一个Document 中或从一个物理文件中提取出不同的数据并放入同一个Document中 。
如下图所示
Field :用来表示不同的数据源
fieldName1: 表示field名称
fieldValue:表示field 的值
Field.Store.YES,:表示是否在索引文件中完整的存储该值 。
在创建索引时,有些内容需要以摘要的形式完整地或以片段的方式显示在页面上,来便于用户查找想要的记录 , 那么就应该选择存储,如果不需要完整或片段的显示就不需要存储 。
Field.Index.TOKENIZED :表示是否索引和分词 。
只要是需要当作关键字让用户查找的字段就需要建立索引 。
在建立索引的过程中,如果像文章标题、文章内容这样的Field, 一般是靠用户输入几个关键字来查询的,就应该选择分词 。
如果需要用户输入完整字符也就是精确查找才能查询到的,例如:beanName , 就可以不分词 。
Document最直观的理解方式:
Document就相当于我们平台中的一个普通javaBean, , 而Field 就是javaBean中的一个属性 。lucene搜索的机制就是靠搜索指定的Field的值 ,来得到含有要搜索内容的Document 集合,所以问题的关键在于如何组织Document .
2.结合平台创建索引的思路
(1) 经分析搜索元素应该由如下内容组成(Document的属性)
(2) 数据库数据转化为Document 的构造过程:
JavaBean / Attachment→(Temp Object) BaseData→(Finally Object) Document
分析:
要建立索引的源数据分为两大部分:一个是数据库数据 BeanData ,另一个是附件数据 FileData , 这样可以建立一个抽象类 BaseData , 来存放它们共有的属性 。同时为了管理这些相应的数据 , 在相同的等级结构上,建立了相应的管理类(xxxDataManager),对这些数据类的操作(建立或删除索引)进行管理,并用一个工厂类(DataManagerFactory)来创建所需要的管理类,IndexHelper用来充当整个索引模块对外的接口,为了实现一些与平台特定的业务 , 特用SupportManager来提供一些额外的业务支持,索引模块代码结构如下图所示 。
二.搜索索引
1.lucene 搜索的核心步骤:
String[]fields={“title”, “summary”,……};//要查找的field范围
BooleanClause.Occur[]flags={BooleanClause.Occur.SHOULD, BooleanClause.Occur. MUST ,……};
Queryquery = MultiFieldQueryParser.parse(queryStr, fields,flags,new StandardAnalyzer());
Hitshits=newIndexSearcher(INDEX_STORE_PATH).search(query);
for (int i = 0;ihitsLength ; i)
{
Document doc = hits.doc(i);
String title = doc.get(“title”);
String summary = doc.get(“summary”);
// 搜索出来的结果高亮显示在页面上
if (title != null) {
TokenStream tokenStream = analyzer.tokenStream(“title”,new StringReader(title));
String highlighterValue = https://www.04ip.com/post/highlighter.getBestFragment(tokenStream, title) ;
if(highlighterValue != null){
title = highlighterValue ;
}
//log.info("SearchHelper.search.title=" title);
}
if(summary!= null){
TokenStream tokenStream = analyzer.tokenStream(“summary”,new StringReader(summary));
String highlighterValue = https://www.04ip.com/post/highlighter.getBestFragment(tokenStream, creator) ;
if(highlighterValue != null){
summary = highlighterValue ;
}
//log.info("SearchHelper.search. summary ="summary);
}
}
2.结合平台构造搜索模块
PageData 类用来存放检索结果集数据 。
PageInfo 类用来存放页面相关信息例如,PageData对象集合、总记录个数、每一页的记录数、 总页面数量等等 。
SearchHelper用来充当整个搜索模块的对外接口 。
三.为平台组件添加索引的步骤(以知识中心为例)
1.在com.cscec.oa.searchengine.extend.module 目录下添加一个新的package
例如:com.cscec.oa.searchengine.extend.module.resourcestore
2.在新的目录下建立data package 并建立相应的数据类,并使这个数据类继承BeanData 。
例如:
package com.cscec.oa.searchengine.extend.module.resourcestore.data
public class ResourceStoreBeanData extends BeanData{
}
3. 与data package 同一级目录建立manager package 并建立相应管理类,并使这个管理类继承BeanDataManager
例如:
com.cscec.oa.searchengine.extend.module.resourcestore.manager
public class ResourceStoreBeanDataManagerImpl extends BeanDataManager{
}
4.以管理员的身份登陆OA后,在菜单中找到“索引模块管理”链接,将相应信息添加完成后,便可以在List 页面 点击“创建索引”对该模块的数据进行索引的建立,建立完成后便可以进行查询 。
java如何实现文件搜索功能java实现文件搜索主要使用file类和正则表达式,如下示例:
package com.kiritor.util;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* 文件的相关操作类
*
* @author Kiritor
*/
public class FileOperation {
private static String contentPath;
private static String filePath;
private static File[] fileList = null;// 保存文件列表,过滤掉目录
public FileOperation() {
}
/** 构造函数的参数是一个目录 */
public FileOperation(String path) {
File file = new File(path);
if (file.isDirectory())
this.contentPath = path;
else
this.filePath = path;
}
/**获取文件列表*/
public static File[] getFiles() {
if (contentPath == null) {
File file = new File(filePath);
fileList = new File[1];
fileList[0] = file;
return fileList;
}
fileList = new File(contentPath).listFiles(new FileFilter() {
/**使用过滤器过滤掉目录*/
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory())
{
return false;
}else
return true;
}
});
return fileList;
}
/** 对当前目录下的所有文件进行排序 */
public static File[] sort() {
getFiles();
Arrays.sort(fileList, new FileComparator());
return fileList;
}
public static void tree(File f, int level) {
String preStr = "";
for(int i=0; ilevel; i) {
preStr= "";
}
File[] childs = f.listFiles();
//返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件 。
for(int i=0; ichilds.length; i) {
System.out.println(preStrchilds[i].getName());
if(childs[i].isDirectory()) {
tree(childs[i], level1);
}
}
}
// 提供一个"比较器"
static class FileComparator implements java.util.ComparatorFile {
@Override
public int compare(File o1, File o2) {
// 按照文件名的字典顺序进行比较
return o1.getName().compareTo(o2.getName());
}
}
}
java如何实现搜索功能 。比如,输入txt就能搜索出这个文件夹内所有txt格式的文件 。请给完整代码 。import java.io.*;
public class FileDemo{
public static void main(String[] args)throws Exception{
//第一个参数是文件路径,第二个参数是要搜索的文件扩展名
getFile("D:\\JavaDemo",".txt");
}
private static void getFile(String pathName, final String endsWith)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("文件不存在,你检索个P呀 。");
file.listFiles(new FileFilter(){
public boolean accept(File file){
if(file.getName().endsWith(endsWith)){
System.out.println(file.getName());
return true;
}else
return false;
}
});
}
}
【java搜索引擎代码 java搜索功能怎么实现流程】java搜索引擎代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java搜索功能怎么实现流程、java搜索引擎代码的信息别忘了在本站进行查找喔 。

    推荐阅读