【Java开源代码栏目提醒】:本文主要为网学会员提供CNIndexer.java,希望对需要CNIndexer.java网友有所帮助,学习一下!
//CNIndexer
package tianen;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import java.io.*;
import tool.FileText;
import tool.FileList;
public class CNIndexer
{
public static void main(String[] args) throws java.io.IOException
{
String indexPath = "cnindex";
//IndexWriter
IndexWriter writer = new IndexWriter(indexPath,new ChineseAnalyzer());
String[] files = FileList.getFiles("doc");
int num = files.length;
for(int i=0;i<num;i++)
{
Document doc = new Document();
File f = new File(files[i]);
//Field -name
String name = f.getName();
Field field = new Field("name",name ,Field.Store.YES, Field.Index.TOKENIZED);
//add field
doc.add(field);
//Field -content
String content = FileText.getText(f);
field = new Field("content", content ,Field.Store.YES, Field.Index.TOKENIZED);
//add field
doc.add(field);
//Field -path
String path = f.getPath();
field = new Field("path", path ,Field.Store.YES, Field.Index.NO);
//add field
doc.add(field);
System.out.println("File : " + name + " Indexed!");
//add document
writer.addDocument(doc);
}
//close IndexWriter
writer.close();
//message
System.out.println("CN Index Created!");
}
}
上一篇:
cNative.java
下一篇:
台湾海峡台风浪的数值模拟