[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]
[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]