Lucene学习笔记--创建索引(Create Index)
public void createIndex() { try { File file = new File(FILE_PATH); //FILE_PATH为所要创建索引的文件的绝对地址,在地址完全可以用String来代替 Document doc = new Document(); IndexWriter indexWriter = new IndexWriter(INDEX_STORE_PATH,analyzer, true,MaxFieldLength.LIMITED); //INDEX_STORE_PATH索引存放的地址 Field field =new Field("content",loadFileToString(file),Field.Store.NO, Field.Index.TOKENIZED); doc.add(field); field=new Field("name",file.getName(),Field.Store.YES, Field.Index.TOKENIZED); doc.add(field); field=new Field("path",file.getPath(),Field.Store.YES, Field.Index.TOKENIZED); doc.add(field); indexWriter.addDocument(doc); indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } }
public String loadFileToString(File file) { try { BufferedReader br = new BufferedReader(new FileReader(file)); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line); line = br.readLine(); } br.close(); return sb.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }
相关推荐
ribavnu 2020-11-16
moyekongling 2020-11-13
坚持是一种品质 2020-11-16
chenjiazhu 2020-09-29
kikaylee 2020-10-31
Ida 2020-09-16
liuweiq 2020-09-09
silencehgt 2020-09-07
sunnyxuebuhui 2020-09-07
西瓜皮儿的皮儿 2020-09-07
LuckyLXG 2020-09-08
明瞳 2020-08-19
MissFuTT 2020-08-18
jzlixiao 2020-08-18
zhushenghan 2020-08-16
罗罗 2020-08-16
mrandy 2020-08-15
houdaxiami 2020-08-15