基于Lucene的QueryParser搜索

本文介绍了一个基于Lucene的简单搜索测试实例,通过创建索引、查询解析及搜索演示了Lucene的基本使用流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=blue]

[code]
import java.io.*;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;


public class QueryParserTest {

Date startTime,endTime;

/**
* 索引文件的存放位置
*/
private String path = "C:/lucene/test.txt";

/**
* 创建索引
*/
public void createIndexByFile(){
IndexWriter writer;
try {

String filePath = "C:/lucene/test.txt";
String content = file2String(filePath, "GBK");

//System.out.println(content);

writer = new IndexWriter(path,new StandardAnalyzer(),true);

Document docA = new Document();

Field fieldA = new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
docA.add(new Field("path",filePath,Field.Store.YES,Field.Index.UN_TOKENIZED));
docA.add(fieldA);

writer.addDocument(docA);

//如果对海量数据进行创建索引的时候,需要对索引进行优化,以便提高速度
writer.optimize();

//跟数据库类似,打开一个连接,使用完后,要关闭它
writer.close();

} catch (Exception e) {
e.printStackTrace();
}
}


public Query queryParser(){
QueryParser queryParser = new QueryParser("content", new StandardAnalyzer());
try {
return queryParser.parse("搜索");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public void search(){
try {
//相当于sql中的 select * from talbeName
IndexSearcher search = new IndexSearcher(path);

startTime = new Date();
//抽象的查询对象
Query query = queryParser();

//lucene在设计的时候,就参照了JDBC的很多概念
Hits hits = search.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println("id= "+hits.id(i));
System.out.println("搜索的内容: "+hits.doc(i));
//System.out.println(hits.score(i));
}
endTime = new Date();

System.out.println("本次搜索用时:" + (endTime.getTime() - startTime.getTime()) + "毫秒");

} catch (Exception e) {
e.printStackTrace();
}
}



private String file2String(String fileName,String charset) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),charset));
//StringBuilder ,StringBuffer
StringBuilder builder = new StringBuilder();
String line = null;
while((line = reader.readLine())!=null){
builder.append(line);
}
return builder.toString();
}

/**
* @param args
*/
public static void main(String[] args) {
QueryParserTest ff = new QueryParserTest();
ff.createIndexByFile();

ff.search();

}

}

[/code]

搜索结果如下:

id= 0
搜索的内容: Document<stored/uncompressed,indexed<path:C:/lucene/test.txt> stored/uncompressed,indexed,tokenized<content:最近在学习Lucene,学习这个东西也差不多一个月了,现在想写一些搜索的测试看看。呵呵,很想做一个属于自己的简单的搜索引擎,继续坚持学习吧!>>
本次搜索用时:138毫秒[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值