Lucene学习笔记--简单搜索
方法1:
Date starttime=new Date(); String queryString = "o"; String[] fields={"name","content"}; QueryParser queryParser = new MultiFieldQueryParser(fields,analyzer); Query query=queryParser.parse(queryString); IndexSearcher indexSearcher=new IndexSearcher(INDEX_STORE_PATH); Filter filter=null; TopDocs topDocs=indexSearcher.search(query,filter,1000); Date endtime=new Date(); System.out.println("共有"+(endtime.getTime()-starttime.getTime())+"ms"); System.out.println("共有"+topDocs.totalHits+"个匹配结果"); for(ScoreDoc scoreDoc : topDocs.scoreDocs){ Document doc =indexSearcher.doc(scoreDoc.doc); System.out.println("content:"+doc.get("content")); System.out.println("name:"+doc.get("name")); System.out.println("path:"+doc.get("path")); }
运行结果:
共有143ms 共有14个匹配结果 content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt content:null name:ModernApplication.txt path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
方法2:
starttime=new Date(); IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH); Term t = new Term("content", "o"); Query q= new TermQuery(t); Hits h= searcher.search(q); endtime=new Date(); long timeOfSearch = endtime.getTime()-starttime.getTime(); System.out.println("共有"+timeOfSearch+"ms"); System.out.println("共有"+h.length()+"个匹配结果"); for(int i=0;i<h.length();i++){ System.out.println(h.doc(i)); System.out.println(h.doc(i).get("name")); System.out.println(h.score(i)); System.out.println(h.id(i)); }
运行结果:
共有8ms 共有14个匹配结果 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 0 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 1 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 2 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 3 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 4 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 5 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 6 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 7 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 8 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 9 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 10 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 11 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 12 Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>> ModernApplication.txt 0.05143136 13
相关推荐
renjinlong 2020-09-03
Jacry 2020-07-04
IceStreamLab 2020-06-26
mengyue 2020-06-09
PasserbyX 2020-05-16
mameng 2020-05-12
心丨悦 2020-05-06
编码之路 2020-05-03
mengyue 2020-05-02
qiuzhuoxian 2020-02-23
编码之路 2020-02-20
lionelf 2020-02-03
TyCoding 2020-02-01
heniancheng 2020-01-31
某某某 2020-01-30
PinkBean 2020-01-29
某某某 2020-01-12
编码之路 2020-01-01
itmale 2020-01-01