欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lucene学习笔记--创建索引(Create Index)

程序员文章站 2022-03-05 12:42:11
...

 

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;
		}
	}
相关标签: lucene

上一篇: Lucene 分词

下一篇: Lucene分页查询