import java.io.File;
import java.util.ArrayList;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.dom4j.DocumentException;
import org.wltea.analyzer.lucene.IKAnalyzer;
/**
* @category 创建所有XML索引
*
*/
public class CreatIndex {
private String INDEX_STORE_PATH ;
//创建索引
@SuppressWarnings("deprecation")
public void creatIndex(){
try{
GetPath path = new GetPath();
INDEX_STORE_PATH = path.getIndexPath();
File file = new File(INDEX_STORE_PATH);
Analyzer analyzer = new IKAnalyzer();
XmlReader xml = new XmlReader();
FSDirectory directory = FSDirectory.open(file);
IndexWriter writer = new IndexWriter(directory, analyzer, true,IndexWriter.MaxFieldLength.LIMITED);
ArrayList<String> lisId = xml.getId();
ArrayList<String> lisTitle = xml.getTitle();
ArrayList<String> lisKeyWords = xml.getKeyWords();
ArrayList<String> lisKind = xml.getKind();
ArrayList<String> lisDescribe = xml.getDescribe();
ArrayList<String> lisDate = xml.getDate();
ArrayList<String> lisUrl = xml.getUrl();
ArrayList<String> lisAuthor = xml.getAuthor();
ArrayList<String> lisPublisher = xml.getPublisher();
//System.out.println(lisUrl.get(5));
for (int i = 0; i < xml.getCount();i++){
Document doc = new Document();
//为ID创建Field
Field field = new Field("id",lisId.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED );
doc.add(field);
//为title创建索引
field = new Field("title",lisTitle.get(i),Field.Store.YES,Field.Index.ANALYZED);
doc.add(field);
//为keywords创建索引
field = new Field("keywords",lisKeyWords.get(i),Field.Store.YES,Field.Index.ANALYZED);
doc.add(field);
//为kind创建索引
field = new Field("kind",lisKind.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(field);
//为describe创建索引
field = new Field("describe",lisDescribe.get(i),Field.Store.YES,Field.Index.ANALYZED);
doc.add(field);
//为data创建索引
field = new Field("date",lisDate.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(field);
//为URL创建索引
field = new Field("url",lisUrl.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(field);
//为author创建索引
field = new Field("author",lisAuthor.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(field);
//为publisher创建索引
field = new Field("publisher",lisPublisher.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(field);
}
writer.addDocument(doc);
}
writer.close();
//directory.close();
System.out.println("索引创建完毕");
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String [] args) throws DocumentException{
CreatIndex index = new CreatIndex();
index.creatIndex();
}
}
Lucene 索引创建
最新推荐文章于 2024-11-05 13:50:04 发布