官方demo,裡面思路也算清晰,也可以執行
lucene-6.3.0建立索引**:
public
class searchfiles
string index = "index";
string field = "contents";
string queries = null;
int repeat = 0;
boolean raw = false;
string querystring = null;
int hitsperpage = 10;
for (int i = 0; i < args.length; ++i) else
if ("-field".equals(args[i])) else
if ("-queries".equals(args[i])) else
if ("-query".equals(args[i])) else
if ("-repeat".equals(args[i])) else
if ("-raw".equals(args[i])) else
if ("-paging".equals(args[i]))
++i;}}
//從給出的索引路徑下讀取索引檔案
indexreader reader = directoryreader.open(fsdirectory.open(paths.get(
index, new string[0])));
indexsearcher searcher = new indexsearcher(reader);
//此處的分析器為standardanalyzer
analyzer analyzer = new standardanalyzer();
bufferedreader in = null;
if (queries != null)
in = files.newbufferedreader(paths.get(queries, new string[0]),
standardcharsets.utf_8);
else
//建立queryparser
queryparser parser = new queryparser(field, analyzer);
while (true)
string line = (querystring != null) ? querystring : in.readline();
if (line == null)
break;
if (line.length() == -1)
line = line.trim();
if (line.length() == 0)
query query = parser.parse(line);
system.out.println("searching for: " + query.tostring(field));
if (repeat > 0)
date end = new date();
system.out.println("time: " + (end.gettime() - start.gettime())
+ "ms");
}dopagingsearch(in, searcher, query, hitsperpage, raw,
(queries == null) && (querystring == null));
if (querystring != null)
}reader.close();
}/**
*搜尋索引過程
**/public
static
void
dopagingsearch(bufferedreader in,
indexsearcher searcher, query query, int hitsperpage, boolean raw,
boolean interactive) throws ioexception
hits = searcher.search(query, numtotalhits).scoredocs;
}end = math.min(hits.length, start + hitsperpage);
//控制台列印搜尋結果
for (int i = start; i < end; ++i) else else }}
if (!(interactive))
return;
if (end == 0)
if (numtotalhits >= end)
;boolean quit = false;
while (true)
if (start + hitsperpage < numtotalhits)
system.out.println("(q)uit or enter number to jump to a page.");
string line = in.readline();
if ((line.length() == 0) || (line.charat(0) == 'q'))
if (line.charat(0) == 'p')
if (line.charat(0) == 'n')
int page = integer.parseint(line);
if ((page - 1) * hitsperpage < numtotalhits)
system.out.println("no such page");
}if (quit)
return;
end = math.min(numtotalhits, start + hitsperpage);}}
}
Lucene搜尋 索引過程筆記
lucene索引文件過程 初始化indexwriter 構建document 呼叫indexwriter.adddocument執行寫入 初始化documentwriter。引數指定寫出位置為記憶體 生成自增段id 呼叫documentwriter.adddocument 執行寫入 寫出fieldi...
Lucene全文檢索 索引建立
1.建立乙個indexwriter物件 a 指定索引庫的存放位置 b 指定乙個分析器,對文件內容進行分析 2.建立document物件 3.建立field物件,將field物件新增到document物件中,4.使用indexwriter物件,將document物件寫入索引庫,此過程進行索引建立,並將...
站內搜尋 索引 XML解析
看 看碼說話 string indexpath c index 索引庫 儲存位址 web.config可配性 fsdirectory directory fsdirectory.open new directoryinfo indexpath new nativefslockfactory 索引檔案...