zoukankan      html  css  js  c++  java
  • 每日一练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(); } }}
    
    


     

  • 相关阅读:
    MongoDB + Spark: 完整的大数据解决方案
    07对象字面量
    05JavaScript中数组的使用
    04JavaScript中函数也是对象
    03JavaScript中的函数预解析
    02通过arguments实现方法重载
    01函数重名问题
    mxGraph 学习笔记 --mxGraph常用功能代码
    mxGraph学习笔记--设置节点鼠标事件
    mxGraph 学习笔记 --右键菜单
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313043.html
Copyright © 2011-2022 走看看