`
smallearth
  • 浏览: 37444 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Lucene小练七--(索引删除 清空 加权 排序)复习

 
阅读更多
package Java.se.lucene;
//主类
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;


public class index {
	private String[] ids={"1","2","3","4","5","6"};
	private String[] emails={"aa@aa.com","bb@bb.com",
			"cc@cc.com","dd@dd.com","ee@ee.com","ff@ff.com"};
	private String[] contents={"i like  gdsfgfds","i like fsdfs","i like fdsfsd",
			"i like fdsfsd","i like like fdfs","i like like like fsefsdfg"};
	private int[] attachs={1,2,3,4,5,6};
	private String[] names={"liwu","zhangsan","xiaoqinag","laona",
			"dabao","lisi"};
	private Directory directory=null;
	private IndexWriter writer=null;
	private Map<String,Float> scores=new HashMap<String,Float>();
	
	public index()
	{
		try {
		//	scores.put("aa.com", 2.0f);
		//	scores.put("bb.com", 1.0f);
		//	scores.put("cc.com", 3.0f);
		//	scores.put("dd.com", 4.0f);
			scores.put("ee.com", 5.0f);
			scores.put("ff.com", 6.0f);
			directory=FSDirectory.open(new File("f:/lucene/Index04"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//建立索引
	public void Index()
	{
		Document document=null;
         try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
				new StandardAnalyzer(Version.LUCENE_36)));
			writer.deleteAll();//更新索引
			for(int i=0;i<ids.length;i++)
			{
				document=new Document();
				document.add(new Field("id", ids[i], 
						Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS  ));
				document.add(new Field("email",emails[i],
						Field.Store.YES,Field.Index.NOT_ANALYZED));
				document.add(new Field("content", contents[i], 
						Field.Store.YES, Field.Index.ANALYZED));
				document.add(new Field("name",names[i],
						Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
		    	
		    	String str=emails[i].substring(emails[i].lastIndexOf("@")+1);
		    	System.out.println(str);
    //		    	if(scores.containsKey(str))
	//	    	{
	//	    		document.setBoost(scores.get(str));
	//	    	}else{
	//	    		document.setBoost(0.5f);
	//	    	}
		    	writer.addDocument(document);
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
            try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
    //遍历各种视频 
	public void query()
	{
		try {
			IndexReader reader=IndexReader.open(directory);
			System.out.println("numdocs:"+reader.numDocs());//文档总数
			System.out.println("maxDocs:"+reader.maxDoc());//可存储文章做大数目
			System.out.println("detelemaxDocs:"+reader.numDeletedDocs());
			reader.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 //删除索引,但并没有完全删除,可以恢复的
	public void delete()
	{
		try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
					new StandardAnalyzer(Version.LUCENE_36)));
			writer.deleteDocuments(new Term("id","1"));
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//使用reader进行恢复
	@SuppressWarnings("deprecation")
	public void undelete()
	{
		IndexReader reader = null;
			try {
				reader = IndexReader.open(directory,false);
				reader.undeleteAll();
				reader.close();
			} catch (StaleReaderException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (LockObtainFailedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	//清空回收站
	public void forceDelete()
	{
		try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
					new StandardAnalyzer(Version.LUCENE_36)));
			writer.forceMergeDeletes();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
    //已经停用
    public void forceMerge()
    {
    	try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
					new StandardAnalyzer(Version.LUCENE_36)));
			writer.forceMerge(3);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    //更新索引
    public void update()
    {
    	Document document=null;
        try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
				new StandardAnalyzer(Version.LUCENE_36)));
			/*
			 * lucene没有提供更新,只能先删除再添加
			 * 
			 */
			for(int i=0;i<ids.length;i++)
			{
				document=new Document();
				document.add(new Field("id", "11", 
						Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS  ));
				document.add(new Field("email",emails[0],
						Field.Store.YES,Field.Index.ANALYZED));
				document.add(new Field("content", contents[0], 
						Field.Store.NO, Field.Index.NOT_ANALYZED));
				document.add(new Field("name",names[0],
						Field.Store.YES,Field.Index.NOT_ANALYZED));
		    	writer.updateDocument(new Term("id","1"), document);
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
           try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    //搜索 
    public void search() 
    {
    	IndexReader reader=null;
		try { 	
			reader = IndexReader.open(directory);
			IndexSearcher searcher=new IndexSearcher(reader);
			TermQuery query=new TermQuery(new Term("content","like"));
			TopDocs tds =searcher.search(query, 10);
			for(ScoreDoc sdc:tds.scoreDocs)
			{
				Document document=searcher.doc(sdc.doc);
				System.out.println("("+sdc.doc+")"+document.get("name")+"["+document.get("email")+
						"]-->"+document.get("id"));
			}
			reader.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
	
    
 }
// 测试类

package Java.se.lucene;


import org.junit.Test;

public class Test_Index {
	@Test
	public void test_index() //测试索引
	{
		index ind=new index();
		ind.Index();
	}
	@Test
	public void test_query() //遍历
	{
		index ind=new index();
		ind.query();
	}
	@Test
	public void test_delete() //删除索引
	{
		index ind=new index();
		ind.delete();
	}
	@Test
	public void test_undelete() //恢复删除
	{
		index ind=new index();
		ind.undelete();
	}
	@Test
	public void test_forceDelete() //清空回收站站
	{
		index ind=new index();
		ind.forceDelete();
	}
	@Test
	public void test_forceMerge() //清空回收站站
	{
		index ind=new index();
		ind.forceMerge();
	}
	@Test
	public void test_update() //更新索引
	{
		index ind=new index();
		ind.update();
	}
	@Test
	public void test_search() //更新索引
	{
		index ind=new index();
		ind.search();
	}

/*	public void check() throws IOException{						//检查索引是否被正确建立(打印索引)
		Directory directory = FSDirectory.open(new File("f:/lucene/Index04/"));//创建directory,其储存方式为在
		IndexReader reader = IndexReader.open(directory);
		for(int i = 0;i<reader.numDocs();i++){
			System.out.println(reader.document(i));
		}
		reader.close();
	}
		public static void main(String[] args) throws IOException {
			new index().check();
		}*/
}


分享到:
评论

相关推荐

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...

    lucene-spatial3d-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-spatial3d-6.6.0.jar; 赠送原API文档:lucene-spatial3d-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial3d-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-6.6.0.pom; 包含...

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-spatial3d-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial3d-7.3.1.jar; 赠送原API文档:lucene-spatial3d-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.3.1.pom; 包含...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...

    lucene-spatial3d-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-spatial3d-7.2.1.jar; 赠送原API文档:lucene-spatial3d-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.2.1.pom; 包含...

    lucene-spatial3d-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial3d-6.6.0.jar; 赠送原API文档:lucene-spatial3d-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial3d-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-6.6.0.pom; 包含...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-core-2.9.4,lucene-core-3.0.2,lucene-core-3.0.3,lucene-core-3.4.0

    《Apache Lucene核心技术详解:从2.9.4到3.4.0的演变》 Apache Lucene,作为开源的全文检索库,是Java开发人员进行高效信息检索的重要工具。这个压缩包文件包含了Lucene从2.9.4版本到3.4.0版本的核心组件,让我们...

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-spatial3d-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-spatial3d-7.7.0.jar; 赠送原API文档:lucene-spatial3d-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial3d-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.7.0.pom; 包含...

    lucene-spatial3d-7.3.1-API文档-中文版.zip

    赠送jar包:lucene-spatial3d-7.3.1.jar; 赠送原API文档:lucene-spatial3d-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.3.1.pom; 包含...

    lucene-backward-codecs-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-spatial3d-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial3d-7.7.0.jar; 赠送原API文档:lucene-spatial3d-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial3d-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.7.0.pom; 包含...

    lucene-spatial3d-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial3d-7.2.1.jar; 赠送原API文档:lucene-spatial3d-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.2.1.pom; 包含...

Global site tag (gtag.js) - Google Analytics