今天敲的代码是添加索引的代码,之前看视频讲解的是更新索引,但有很大的局限(必须先删除之后再添加索引),现在学了实时索引之后就不用那样做了,可以直接向原有索引里添加新的索引。以下是代码:
//创建IndexField类,可以通过这个类来添加需要创建索引的内容
package Lucy;
import java.security.MessageDigest;
public class IndexField
{
private String id;
private String title;
private String content;
public void setId(String id)
{
this.id=id;
}
public String getId()
{
return id;
}
public void setTitle(String title)
{
this.title=title;
}
public String getTitle()
{
return title;
}
public void setContent(String content)
{
this.content=content;
}
public String getContent()
{
return content;
}
}
//通过该类创建管理Index的NRTManager
package Lucy;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NRTManager;
import org.apache.lucene.search.NRTManagerReopenThread;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.SearcherWarmer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneContext
{
private static LuceneContext instance;
private static final String path="e:Lucene_Index";
private static IndexWriter writer;
private static Analyzer analyzer;
private static NRTManager nrtMgr;
private static SearcherManager mgr;
private static Directory directory;
private static Version version;
public static LuceneContext getInstance()
{
if(instance==null)
{
init();
instance=new LuceneContext();
}
return instance;
}
private static void init()
{
try
{
directory=FSDirectory.open(new File(path));
version=Version.LUCENE_35;
//String s=LuceneContext.class.getClassLoader().getResource("data").getPath();
analyzer=new StandardAnalyzer(version);
writer=new IndexWriter(directory,new IndexWriterConfig(version,analyzer));
nrtMgr=new NRTManager(writer,new SearcherWarmer()
{
public void warm(IndexSearcher s)throws IOException
{
System.out.println("reopen");
}
}
);
NRTManagerReopenThread reopen=new NRTManagerReopenThread(nrtMgr,5.0,0.025);
reopen.setDaemon(true);
reopen.start();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public IndexSearcher getSearcher()
{
return mgr.acquire();
}
public void releaseSearcher(IndexSearcher searcher)
{
try{
mgr.release(searcher);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void commitIndex() throws CorruptIndexException, IOException
{
writer.commit();
}
public NRTManager getNRTManager()
{
return nrtMgr;
}
public static void check() throws IOException
{ //检查索引是否被正确建立(打印索引)
FSDirectory directory = FSDirectory.open(new File(path));
IndexReader reader = IndexReader.open(directory);
for(int i = 0;i<reader.numDocs();i++)
{
System.out.println(reader.document(i));
}
}
}
//测试类
package Lucy;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.NRTManager;
public class Test
{
public static void main(String[] args) throws IOException
{
IndexField fields=new IndexField();
fields.setId("01");
fields.setTitle("我的大学");
fields.setContent("我的大学坐落在美丽的海滨城市--烟台");
LuceneContext.check();
//addIndex(fields);
// LuceneContext.commitIndex();
}
//通过该方法直接添加索引
public static void addIndex(IndexField fields)
{
try{
NRTManager nrtMgr=LuceneContext.getInstance().getNRTManager();
Document doc=field2Doc(fields);
nrtMgr.addDocument(doc);
}
catch(IOException e)
{
e.printStackTrace();
}
}
//通过该方法添加Document的域
private static Document field2Doc(IndexField field)
{
Document doc=new Document();
doc.add(new Field("id",field.getId(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("title",field.getTitle(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("content",field.getContent(),Field.Store.YES,Field.Index.ANALYZED));
return doc;
}
}
不足之处:1、存在重复建立索引的问题
2、当只需要添加一个域是没法执行