首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 云计算 >

lucene 构建目录

2012-11-10 
lucene 构建索引package com.bit.section2import static org.junit.Assert.*import java.io.IOException

lucene 构建索引

package com.bit.section2;import static org.junit.Assert.*;import java.io.IOException;import org.apache.lucene.analysis.WhitespaceAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.FieldSelectorResult;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.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.TermQuery;import org.apache.lucene.store.Directory;import org.apache.lucene.store.LockObtainFailedException;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.Version;import org.junit.Before;import org.junit.Test;import com.bit.util.TestUtil;public class IndexingTest {protected String[] ids = {"1","2"};protected String[] unindexed = {"Netherlands","Italy"};protected String[] unsorted = {"Amesterdam has lots of canals","Venice has lots of canals"};protected String[] text = {"Amsterdam","Venice"};private Directory directory;@Beforepublic void setUp() throws Exception {directory = new RAMDirectory();IndexWriter writer = getWriter();for(int i=0;i<ids.length;i++){Document doc = new Document();doc.add(new Field("id", ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED));doc.add(new Field("country",unindexed[i],Field.Store.YES,Field.Index.NO));doc.add(new Field("contents",unsorted[i],Field.Store.NO,Field.Index.ANALYZED));doc.add(new Field("city",text[i],Field.Store.YES,Field.Index.ANALYZED));writer.addDocument(doc);}writer.close();}/** * 生成索引生成器 * @return * @throws IOException  * @throws LockObtainFailedException  * @throws CorruptIndexException  */private IndexWriter getWriter() throws CorruptIndexException, LockObtainFailedException, IOException {return new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,new WhitespaceAnalyzer()));}@Testpublic void test() {fail("Not yet implemented");}protected int getHitCount(String fieldName,String searchString) throws IOException, Exception {IndexSearcher searcher = new IndexSearcher(directory);Term term = new Term(fieldName, searchString);Query query = new TermQuery(term);int hitCount = TestUtil.hitCount(searcher,query);searcher.close();return hitCount;}@Testpublic void testIndexWriter() throws CorruptIndexException, LockObtainFailedException, IOException{IndexWriter writer = getWriter();assertEquals(ids.length, writer.numDocs());writer.close();}@Testpublic void testIndexReader() throws CorruptIndexException, IOException{IndexReader reader = IndexReader.open(directory);assertEquals(ids.length, reader.maxDoc());assertEquals(ids.length, reader.numDocs());reader.close();}/** * 测试删除文档 */@Testpublic void testDeleteBeforeOptimize() throws IOException{IndexWriter writer = getWriter();assertEquals(2, writer.numDocs());//确认被标记删除的文档writer.deleteDocuments(new Term("id", "1"));writer.commit();assertTrue(writer.hasDeletions());//确认删除一个文档并剩余一个文档assertEquals(2, writer.maxDoc());assertEquals(1, writer.numDocs());writer.close();}/** * 优化后的删除 * @throws IOException */@Testpublic void testDeleteAfterOptimize() throws IOException{IndexWriter writer = getWriter();assertEquals(2, writer.numDocs());writer.deleteDocuments(new Term("id", "1"));writer.optimize();writer.commit();assertFalse(writer.hasDeletions());assertEquals(1, writer.maxDoc());assertEquals(1, writer.numDocs());writer.close();}/** * 更新文档 * @throws Exception  */@Testpublic void testUpdate() throws Exception{assertEquals(1, getHitCount("city", "Amsterdam"));IndexWriter writer = getWriter();Document doc = new Document();doc.add(new Field("id", "1",Field.Store.YES,Field.Index.NOT_ANALYZED));doc.add(new Field("country","Netherlands",Field.Store.YES,Field.Index.NO));doc.add(new Field("contents","Den Haag has lots of museums",Field.Store.NO,Field.Index.ANALYZED));doc.add(new Field("city","DenHaag",Field.Store.YES,Field.Index.ANALYZED));writer.updateDocument(new Term("id","1"),doc );writer.commit();assertEquals(0, getHitCount("city", "Amsterdam"));assertEquals(1, getHitCount("city", "DenHaag"));}}


 

热点排行