⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexfiles.java

📁 java阿里巴巴代码
💻 JAVA
字号:
package com.saas.biz.searchMgr;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.saas.biz.commen.commMethodMgr;
import com.saas.biz.dao.attachDAO.AttachExt;
import com.saas.sys.exp.SaasApplicationException;
import com.saas.sys.log.Logger;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import com.ahbay.commenMgr.*;
import com.saas.biz.searchMgr.*;
import java.sql.*;
import java.util.*;
import com.saas.biz.commen.config;

public class IndexFiles 
{  
       
    public IndexFiles() 
    {             
    }

    
    
  
    public static void main(String[] args)
    {    	
        config configFile;
        configFile= new config();
        configFile.init();   
        File INDEX_DIR = new File(configFile.getString("mysqlbase.rootpath")+"index");
        try
        {
            IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true);
            //IndexWriter writer = new IndexWriter("/usr/www/soft.xsaas.com/zone_b2b/index/", new StandardAnalyzer(), true);
            indexDocs(writer);
            System.out.println("Optimizing...");
            writer.optimize();
            writer.close();
 
        } 
        catch (IOException e)
        {
            System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
        }
    }

    static void indexDocs(IndexWriter writer) throws IOException 
    {
        try 
        {
        	DataBaseCommMgr DBQuery = new DataBaseCommMgr();
        	ResultSet rst = null;
        	ResultSetMetaData  rstMetaDate = null;
        	int columnCount = 0;
        	String strRage = "";
    		String strId = "";
    		String strTitle = "";
    		String strContent = "";
    		String sql=""; 
    		sql=sql+"select 'customerRage' rage,a.cust_id id,a.cust_name title,a.scope content from tf_f_customer a                     ";
            sql=sql+"union all                                                                                                          ";
            sql=sql+"select 'saleRage' rage,b.sale_id id,b.title title,b.content content from tf_f_sale b                               ";
            sql=sql+"union all                                                                                                          ";
            sql=sql+"select 'stockRage' rage,c.stock_id id,c.title title,c.content content from tf_f_stockorder c                       ";
            sql=sql+"union all                                                                                                          ";
            sql=sql+"select 'jobRage' rage,d.job_unit id, d.title title,d.request content from tf_f_job d                                 ";
            sql=sql+"union all                                                                                                          ";
            sql=sql+"select 'resumeRage' rage,e.resume_id id,e.work_history title,e.profession content from tf_f_resume e               ";
            sql=sql+"union all                                                                                                          ";
            sql=sql+"select 'commodityRage' rage,f.commodity_id id,f.commodity_name title,f.content content from tf_f_commodity f       ";
            sql=sql+"union all "; 
            sql=sql+"select 'repositoryRage' rage,h.repository_id id,h.title title,h.content content from tf_f_repository h             "; 
               
    		DBQuery.setStrQuery(sql);
    		rst = DBQuery.SelBizQuery();
    		try
    		{
    			while (rst.next())    			
    			{
    				if (rst.getString(1) != null)
    				{
    			        strRage = rst.getString(1);
    			    }
    			    else
    			    {
    			        strRage = "";
    			    }
    				if (rst.getString(2) != null)
    				{
    			        strId = rst.getString(2);
    			    }
    			    else
    			    {
    			        strId = "";
    			    }
    			    if (rst.getString(3) != null)
    				{
    			        strTitle = rst.getString(3);
    			    }
    			    else
    			    {
    			        strTitle = "";
    			    }
    			    if (rst.getString(4) != null)
    				{
    			        strContent = rst.getString(4);
    			    }
    			    else
    			    {
    			        strContent = "";
    			    }    
    				System.out.println("indexing....."+strTitle);
    				writer.addDocument(FileDocument.Document(strRage,strId,strTitle,strContent)); 
    			}
            }
    		catch (Exception e)
    		{	 	
    		    System.out.println(e);
    		}
    		DataBaseCommMgr DBQuerynews = new DataBaseCommMgr();
    		rst = null;
    		sql="select 'infoRage' rage,g.news_id id,g.title title,g.content content from tf_f_news g                               ";
            DBQuerynews.setStrQuery(sql);            
        	rst = DBQuerynews.SelBizQuery();
        	rstMetaDate = rst.getMetaData();
    		columnCount = rstMetaDate.getColumnCount();
        	try
        	{
        		while (rst.next())    			
        		{
    				if (rst.getString(1) != null)
    				{
    			        strRage = rst.getString(1);
    			    }
    			    else
    			    {
    			        strRage = "";
    			    }
    				if (rst.getString(2) != null)
    				{
    			        strId = rst.getString(2);
    			    }
    			    else
    			    {
    			        strId = "";
    			    }
    			    if (rst.getString(3) != null)
    				{
    			        strTitle = rst.getString(3);
    			    }
    			    else
    			    {
    			        strTitle = "";
    			    }     		    
        		    for (int i = 1; i <= columnCount; i++) 
    		        {   		    
        		          String strFieldName = rstMetaDate.getColumnName(i).toLowerCase();
        		          if (strFieldName.equals("content"))
        		          {
        		        	  if (rst.getCharacterStream(i) != null)
		        	          {   
            		        	  java.io.Reader reader = (java.io.Reader)rst.getCharacterStream(i); 
            		        	  String strtmp = "";
            		        	  try
            		        	  {
            		        		  strtmp = (String)getLargerString(reader); 
            		        		  strContent = strtmp;
            		        		  break;
            		        	  }
            		        	  catch (Exception e)
            		      		  {
            		        		  throw new RuntimeException(e);
            		      		  }	
            		      	  }
            		      	  else
            		      	  {
            		      	      strContent ="";
            		      	      break;
            		      	  }	        	  
        		          }		          
    		        }
    		        System.out.println("indexing....."+strTitle);
    				writer.addDocument(FileDocument.Document(strRage,strId,strTitle,strContent)); 
    		    }
        	}
            catch (Exception e)
            {	 	
                System.out.println(e);
            }
        }
        catch (Exception fnfe)
        {
            System.out.println(fnfe);
        }
    }
    public static String getLargerString(java.io.Reader reader) throws Exception 
	{ 

		char[] content = new char[1024000]; 
		char[] buffer = new char[1024]; 
		int len = 0; 
		int off = 0; 
		int contentLen = 1024000; 

		while(true) 
		{ 
		len = reader.read(buffer); 
		if( len == -1) 
		break; 
		if( off + len > contentLen ) 
		{ 
		char[] tmp = new char[contentLen + 1024000]; 
		System.arraycopy(content, 0, tmp, 0, off); 
		content = tmp; 
		contentLen = contentLen + 1024000; 
		} 
		System.arraycopy(buffer, 0, content, off, len); 
		off = off + len; 
		} 
		return new String(content, 0, off); 
	}  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -