• 每日一练1.1(Lucene)


    package org.itat.text1;
    import java.io.IOException;
    
    public class IndexText {
    
     /**
      * @param args
      */
    
    package org.itat.text1;
    import java.io.File;
    import java.io.IOException;
    
    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.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.store.LockObtainFailedException;
    import org.apache.lucene.util.Version;
    
    
    class IndexUtil_11 {
    	
    	private   String [] ids={"1","2","3","4"};
    	private   String [] emails={"lele@qq.com","1534432371@qq.com","hello@sine.com","Tom@163.com"};
    	private  String [] contents={"Hello world!","lele is so cool!","hello@sine.com","Tom@163.com"};
    	private   String [] names={"lele!","Tom","zhangsan ","zhaosi"};
    	private  int  [] attachs={1,2,3,5};
    	private  Directory directory=null;
    	
    	public  void  index()
    	{
    		//创建Derectory
    		try {
    			directory=FSDirectory.open(new File ("G:/Lucene/indext2"));
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		// 创建Writer
    		IndexWriter writer=null;
    		try {
    			writer=new IndexWriter (directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
    		Document doc=null;
    		for (int i=0;i<ids.length;i++)
    		{
    			//创建Decoment并添加Field
    			doc=new Document();
    			doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
    			doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
    			doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
    			doc.add(new Field("content",contents[i],Field.Store.NO,Field.Index.ANALYZED));
    			//将文档写到索引中
    			writer.addDocument(doc);
    		}
    		
    		
    		} 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
    		{
    			if (writer!=null)
    			{
    				try {
    					writer.close();
    				} catch (CorruptIndexException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    					//throw new Exception ();
    				}
    			}
    				
    		}
    		
    		
    	}
    	public void query() throws IOException
    	{
    		
    		try {
    			//通过IndexReader进行查询 
    			IndexReader reader=IndexReader.open(directory);
    			//通过reader可以有效的获取文档的数量
    			System.out.println(reader.maxDoc());
    			System.out.println(reader.numDocs());
    			
    		} catch (CorruptIndexException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    }
    

    public static void main(String[] args) { // TODO Auto-generated method stub IndexUtil_11 indextUtil=new IndexUtil_11(); indextUtil.index(); //indextUtil.IndexUtil(); try { indextUtil.query(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
    
    


     

  • 相关阅读:
    Nbear实体和接口 CodeSmith模版
    prototype1.4版中文参考手册(word,pdf,chm)
    SharePoint 2013 (SharePoint 15)的新特性
    没有域环境下安装SharePoint 2010
    产品经理(PM)常用原型图设计工具
    【转贴】mysql导入数据load data infile用法
    重新学javaweb!
    关于HIbernate中的lazy属性的一些解释
    JAVA程序员基本测试题目
    添加sql server约束
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313043.html
Copyright © 2020-2023  润新知