lucene---QueryParser用法示例

packagedemo.first;

importjava.io.IOException;

importjava.util.Date;

importorg.apache.lucene.analysis.standard.StandardAnalyzer;

importorg.apache.lucene.document.Document;

importorg.apache.lucene.document.Field;

importorg.apache.lucene.index.CorruptIndexException;

importorg.apache.lucene.index.IndexWriter;

importorg.apache.lucene.queryParser.QueryParser;

importorg.apache.lucene.search.Hits;

importorg.apache.lucene.search.IndexSearcher;

importorg.apache.lucene.search.Query;

importorg.apache.lucene.search.Searcher;

importorg.apache.lucene.store.LockObtainFailedException;

publicclassTestQueryParser{

publicstaticvoidmain(String[]args)throwsCorruptIndexException,IOException{

Stringpath="D://workspace//fwk//lucenedemo//firstLuceneIndex";

TestQueryParsertqp=newTestQueryParser();

tqp.createIndex(path);

Searchersearch=tqp.getSearcher(path);

System.out.println("#_2");

tqp.testTime(search,tqp.getQueryParser2());

System.out.println("#_1");

tqp.testTime(search,tqp.getQueryParser1());

System.out.println("#_3");

tqp.testTime(search,tqp.getQueryParser3());

System.out.println("#_4");

tqp.testTime(search,tqp.getQueryParser4());

System.out.println("#_5");

tqp.testTime(search,tqp.getQueryParser5());

System.out.println("#_6");

tqp.testTime(search,tqp.getQueryParser6());

System.out.println("#_7");

tqp.testTime(search,tqp.getQueryParser7());

}

publicvoidtestTime(Searchersearch,Queryquery)throwsIOException{

Datestart=newDate();

Hitshits=search.search(query);

for(inti=0;i<hits.length();i++){

System.out.println(hits.id(i));

System.out.println(hits.doc(i));

System.out.println(hits.score(i));

}

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

}

publicSearchergetSearcher(Stringpath)throwsCorruptIndexException,IOException{

returnnewIndexSearcher(path);

}

publicQuerygetQueryParser1(){

//默认搜索字段

QueryParserqueryParser=newQueryParser("content",newStandardAnalyzer());

try{

returnqueryParser.parse("搜索-擎");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

publicQuerygetQueryParser2(){

QueryParserqueryParser=newQueryParser("content",newStandardAnalyzer());

try{

returnqueryParser.parse("欢迎");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

publicQuerygetQueryParser3(){

QueryParserqueryParser=newQueryParser("content",newStandardAnalyzer());

try{

returnqueryParser.parse("搜索and擎");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

publicQuerygetQueryParser4(){

QueryParserqueryParser=newQueryParser("content",newStandardAnalyzer());

try{

//content字段搜索索引title字段搜寻你好

returnqueryParser.parse("索引title:你好");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

publicQuerygetQueryParser5(){

QueryParserqueryParser=newQueryParser("content",newStandardAnalyzer());

//允许使用正则表达式方式

queryParser.setAllowLeadingWildcard(true);

try{

returnqueryParser.parse("*索*");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

/**

*采用标准分词器StandardAnalyzer会在创建索引的时候把存入的英文全部换成小写放在索引中查询的时候也会将查询的关键词转为小写进行查询

*@return

*/

publicQuerygetQueryParser6(){

QueryParserqueryParser=newQueryParser("testCapital",newStandardAnalyzer());

try{

returnqueryParser.parse("hellOwangzi");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

/**

*采用标准分词器StandardAnalyzer会在创建索引的时候把存入的andor等关键字过滤掉所以在查询的时候怎么也查不到

*@return

*/

publicQuerygetQueryParser7(){

QueryParserqueryParser=newQueryParser("testAndOr",newStandardAnalyzer());

try{

//returnqueryParser.parse("and");

returnqueryParser.parse("test");

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

/**

*创建索引

*@parampath

*/

publicvoidcreateIndex(Stringpath){

try{

IndexWriterwriter=newIndexWriter(path,newStandardAnalyzer(),true);

DocumentdocA=newDocument();

//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置

//Field.Store是否覆盖原来的索引文件,而不是重新建一个

FieldfieldA=newField("content","搜索引擎",Field.Store.YES,Field.Index.TOKENIZED);

//我们把列(fieldA)加到某一行(docA)中

docA.add(fieldA);

docA.add(newField("title","你好中国",Field.Store.YES,Field.Index.TOKENIZED));

docA.add(newField("content","欢迎你llying",Field.Store.YES,Field.Index.TOKENIZED));

docA.add(newField("lastModifyTime","2008-9-17",Field.Store.YES,Field.Index.TOKENIZED));

docA.add(newField("testCapital","HelloWangzi",Field.Store.YES,Field.Index.TOKENIZED));

docA.add(newField("testAndOr","testand",Field.Store.YES,Field.Index.TOKENIZED));

DocumentdocB=newDocument();

//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置

FieldfieldB=newField("content","创建索引",Field.Store.YES,Field.Index.TOKENIZED);

//我们把列(fieldA)加到某一行(docA)中

docB.add(fieldB);

docB.add(newField("title","你好世界",Field.Store.YES,Field.Index.TOKENIZED));

docB.add(newField("content","欢迎加入jee高级开发群46176507",Field.Store.YES,Field.Index.TOKENIZED));

docB.add(newField("lastModifyTime","2008-9-6",Field.Store.YES,Field.Index.TOKENIZED));

docB.add(newField("testCapital","hellowangZi",Field.Store.YES,Field.Index.TOKENIZED));

docB.add(newField("testAndOr","testor",Field.Store.YES,Field.Index.TOKENIZED));

writer.addDocument(docA);

writer.addDocument(docB);

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

writer.optimize();

//跟数据库类似,打开一个连接,使用完后,要关闭它

writer.close();

}catch(CorruptIndexExceptione){

e.printStackTrace();

}catch(LockObtainFailedExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

}