「Java建立检索」java 字符串检索
今天给各位分享Java建立检索的知识,其中也会对java 字符串检索进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
如何用java语言建立多个索引?
使用jdbc技术连接数据库
使用语句对象statement的execute方法可以执行对索引的操作
例如代码如下
public class IndexFiles {
//使用方法:: IndexFiles [索引输出目录] [索引的文件列表] ...
public static void main(String[] args) throws Exception {
String indexPath = args[0];
IndexWriter writer;
//用指定的语言分析器构造一个新的写索引器(第3个参数表示是否为追加索引)
writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);
for (int i=1; iargs.length; i++) {
System.out.println("Indexing file " + args[i]);
InputStream is = new FileInputStream(args[i]);
//构造包含2个字段Field的Document对象
//一个是路径path字段,不索引,只存储
//一个是内容body字段,进行全文索引,并存储
Document doc = new Document();
doc.add(Field.UnIndexed("path", args[i]));
doc.add(Field.Text("body", (Reader) new InputStreamReader(is)));
//将文档写入索引
writer.addDocument(doc);
is.close();
};
//关闭写索引器
writer.close();
}
}
public class Search {
public static void main(String[] args) throws Exception {
String indexPath = args[0], queryString = args[1];
//指向索引目录的搜索器
Searcher searcher = new IndexSearcher(indexPath);
//查询解析器:使用和索引同样的语言分析器
Query query = QueryParser.parse(queryString, "body",
new SimpleAnalyzer());
//搜索结果使用Hits存储
Hits hits = searcher.search(query);
//通过hits可以访问到相应字段的数据和查询的匹配度
for (int i=0; ihits.length(); i++) {
System.out.println(hits.doc(i).get("path") + "; Score: " +
hits.score(i));
};
}
}在整个检索过程中,语言分析器,查询分析器,甚至搜索器(Searcher)都是提供了抽象的接口,可以根据需要进行定制。
分 析:
索引过程中可以看到:
语言分析器提供了抽象的接口,因此语言分析(Analyser)是可以定制的,虽然lucene缺省提供了2个比较通用的分析器SimpleAnalyser和StandardAnalyser,这2个分析器缺省都不支持中文,所以要加入对中文语言的切分规则,需要修改这2个分析器。
Lucene并没有规定数据源的格式,而只提供了一个通用的结构(Document对象)来接受索引的输入,因此输入的数据源可以是:数据库,WORD文档,PDF文档,HTML文档……只要能够设计相应的解析转换器将数据源构造成成Docuement对象即可进行索引。
对于大批量的数据索引,还可以通过调整IndexerWrite的文件合并频率属性(mergeFactor)来提高批量索引的效率。
总 结:
搜索结果返回的是Hits对象,可以通过它再访问Document==Field中的内容。假设根据body字段进行全文检索,可以将查询结果的path字段和相应查询的匹配度(score)打印出来,
如何用java做一个检索txt数据csdn
首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。
package com.campu;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H20121012 {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";
// "res/";
readTxtFile(filePath);
}
}
java的luence全文检索怎么做
lucene是一个公用的全文索引组件,它的目标是把各种各样格式的数据转化成lucene特有的索引文件格式,这样才能通过lucene的高速检索机制进行全文检索。
你的数据来源可以是关系数据库,可以是word、execl、txt文档,可以是html网页,对于这些数据源,你必须将它们内部的数据读取出来,并封装成lucene的document实例,之后让lucene帮你构建索引。
举个例子:你的有一个用户数据库,里面存储了几十万的用户信息,你现在要对这个数据库进行全文索引,那么你要做的事情是:
1.写一段传统的JDBC程序,讲每条的用户信息从数据库读取出来
2.针对每条用户记录,建立一个lucene document
Document doc = new Document();
并根据你的需要,将用户信息的各个字段对应luncene document中的field 进行添加,如:
doc.add(new Field("NAME","USERNAME",Field.Store.YES,Field.Index.UN_TOKENIZED));
然后将该条doc加入到索引中, 如: luceneWriter.addDocument(doc);
这样袱常递端郛得店全锭户就建立了lucene的索引库
3.编写对索引库的搜索程序(看lucene文档),通过对lucene的索引库的查找,你可以快速找到对应记录的ID
4.通过ID到数据库中查找相关记录
ITjob里学。习到的,希望对你有所帮助
用Java如何实现站内搜索
1,使用lucene
2. 使用solr
3. 使用sphinx
4. 低效率使用sql like
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(preStr + childs[i].getName());
if(childs[i].isDirectory()) {
tree(childs[i], level + 1);
}
}
}
// 提供一个"比较器"
static class FileComparator implements java.util.ComparatorFile {
@Override
public int compare(File o1, File o2) {
// 按照文件名的字典顺序进行比较
return o1.getName().compareTo(o2.getName());
}
}
}
Java建立检索的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 字符串检索、Java建立检索的信息别忘了在本站进行查找喔。
发布于:2022-12-02,除非注明,否则均为
原创文章,转载请注明出处。