java+lucene中文分詞,百度搜索原理解析,十分重要的東西

我想只要是學過數據庫的孩紙,無論是mysql,仍是sqlsever,一提到查找,本能的想到的即是like關鍵字,其實去轉盤網分類模式)以前也是採用這種算法,但我能夠告訴你們一個很不幸的事情,like匹配其實會浪費大量的有用資源,緣由這裏不說了請本身想想,咱們仍是直接擺事實驗證。java

   如今用去轉盤網搜:hello 找個單詞,以下:mysql

   http://www.quzhuanpan.com/source/search.action?q=hello&currentPage=1linux

翻頁你會發現只要是包含hello的單詞都找到了,可是若是你用like的話是不會有這個效果的,不信讓咱們再看一下,還好他來講電影網的分詞算法我還沒來得及修改,還能夠看到現象:算法

   http://www.talaishuo.com/searchResult.do?searchFileName=hellosql

你會發現只有開始包含hello這個字段的搜索串才能獲得匹配,這就問題來了,數據庫中大量的資源豈不是白白浪費了,不過沒事,偉大的人類仍是很聰明的,發明了分詞,分詞的原理我就不講了,請本身百度吧,仍是直接上代碼,提示,這裏須要四個jar包做爲工具,我先上傳的去轉盤,想要作分詞的請先下載:數據庫

   分詞包下載地址1apache

   分詞包下載地址2app

直接看代碼:jsp

package com.tray.indexData;

import java.io.File;

import java.io.IOException;

import java.io.StringReader;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.TokenStream;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Fieldable;

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.IndexWriterConfig.OpenMode;

import org.apache.lucene.index.Term;

import org.apache.lucene.queryParser.MultiFieldQueryParser;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.PrefixQuery;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.TermQuery;

import org.apache.lucene.search.TopDocs;

import org.apache.lucene.search.TopScoreDocCollector;

import org.apache.lucene.search.WildcardQuery;

import org.apache.lucene.search.highlight.Highlighter;

import org.apache.lucene.search.highlight.QueryScorer;

import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

import org.wltea.analyzer.lucene.IKAnalyzer;



import com.tray.bean.SerachResult;

import com.tray.common.tools.DateFormater;



public class LuceneSearch {

     

    private static String DISC_URL = "/home/indexData/data";

     

    static {

        String os = System.getProperty("os.name");  

        if(os.toLowerCase().startsWith("win")){  

            DISC_URL = "E:\\indexData\\data"; 

        }

        else{

            DISC_URL ="/home/indexData/data";

        }

    }

         

    //指定分詞器 

    private Analyzer analyzer=new IKAnalyzer(); 

    private static Directory directory;

    //配置

    private static IndexWriterConfig iwConfig;

    //配置IndexWriter

    private static IndexWriter writer;  

    private static File indexFile = null;  

     

    private static Version version = Version.LUCENE_36;

     

    private final int PAPGESIZE=10;



    /**

     * 全量索引

     * @Author haoning

     */

    public void init() throws Exception {

         

        try {

            indexFile = new File(DISC_URL);

            if (!indexFile.exists()) {

                indexFile.mkdir();

            }

            directory=FSDirectory.open(indexFile);  

            //配置IndexWriterConfig  

            iwConfig = new IndexWriterConfig(version,analyzer);  

            iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);  

                //建立寫索引對象  

            writer = new IndexWriter(directory,iwConfig);   

        } catch (Exception e) {

        }

    }

     

    public void closeWriter(){

        try {

            writer.close();

        } catch (CorruptIndexException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

     

    public void commit(){

         

        try {

            writer.commit();

        } catch (CorruptIndexException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

     

    /**

     * 一個一個索引

     * @Author haoning

     */

    public void singleIndex(Document doc) throws Exception {

        writer.addDocument(doc);

    }

     

    /**

     * 一個跟新

     * @Author haoning

     */

    public void singleUpdate(Document doc) throws Exception {

        Term term = new Term("url", doc.get("url"));

        writer.updateDocument(term,doc);

    }

     

    /**

     * 全量索引

     * @Author haoning

     */

    public void fullIndex(Document[] documentes) throws Exception {

         

        writer.deleteAll();

        for (Document document : documentes) {

            writer.addDocument(document);

        }

        writer.commit();

    }

     

    /**

     * 根據id刪除索引

     * @Author haoning

     */

    public void deleteIndex(Document document)throws Exception{

        Term term = new Term("url", document.get("url"));//url纔是惟一標誌

        writer.deleteDocuments(term);

        writer.commit();

    }

     

    /**

     * 根據id增量索引

     * @Author haoning

     */

    public void updateIndex(Document[] documentes) throws Exception{

        for (Document document : documentes) {

            Term term = new Term("url", document.get("url"));

            writer.updateDocument(term, document);

        }

        writer.commit();

    }

     

    /**

     * 直接查詢

     * @Author haoning

     */

    public void simpleSearch(String filedStr,String queryStr,int page, int pageSize) throws Exception{

        File indexDir = new File(DISC_URL);  

        //索引目錄  

        Directory dir=FSDirectory.open(indexDir);  

        //根據索引目錄建立讀索引對象  

        IndexReader reader = IndexReader.open(dir);  

        //搜索對象建立  

        IndexSearcher searcher = new IndexSearcher(reader);

        TopScoreDocCollector topCollector = TopScoreDocCollector.create(searcher.maxDoc(), false);

         

        Term term = new Term(filedStr, queryStr);

        Query query = new TermQuery(term);

        searcher.search(query, topCollector);

        ScoreDoc[] docs = topCollector.topDocs((page-1)*pageSize, pageSize).scoreDocs;

         

        printScoreDoc(docs, searcher);

    }

     

    /**

     * 高亮查詢

     * @Author haoning

     */

    public Map<String, Object> highLightSearch(String filed,String keyWord,int curpage, int pageSize) throws Exception{

        List<SerachResult> list=new ArrayList<SerachResult>();

        Map<String,Object> map = new HashMap<String,Object>();

        if (curpage <= 0) {

            curpage = 1;

        }

        if (pageSize <= 0 || pageSize>20) {

             pageSize = PAPGESIZE;

        }

        File indexDir = new File(DISC_URL); //索引目錄   

        Directory dir=FSDirectory.open(indexDir);//根據索引目錄建立讀索引對象    

        IndexReader reader = IndexReader.open(dir);//搜索對象建立    

        IndexSearcher searcher = new IndexSearcher(reader);

         

        int start = (curpage - 1) * pageSize;

         

        Analyzer analyzer = new IKAnalyzer(true);

        QueryParser queryParser = new QueryParser(Version.LUCENE_36, filed, analyzer);

        queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);

        Query query = queryParser.parse(keyWord);

         

        int hm = start + pageSize;

        TopScoreDocCollector res = TopScoreDocCollector.create(hm, false);

        searcher.search(query, res);

         

        SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");

        Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));

         

        long amount = res.getTotalHits();

        //long pages = (rowCount - 1) / pageSize + 1; //計算總頁數

         

        map.put("amount",amount);//總共多少條記錄

         

        TopDocs tds = res.topDocs(start, pageSize);

        ScoreDoc[] sd = tds.scoreDocs;

         

        for (int i = 0; i < sd.length; i++) {

            Document doc = searcher.doc(sd[i].doc);

            String temp=doc.get("name");

            //作高亮處理

            TokenStream ts = analyzer.tokenStream("name", new StringReader(temp));

             

            SerachResult record=new SerachResult();

            String name = highlighter.getBestFragment(ts,temp); 

            String skydirverName=doc.get("skydirverName");

            String username=doc.get("username");

            String shareTime=doc.get("shareTime");

            String describ=doc.get("describ");

            String typeId=doc.get("typeId");

            String id=doc.get("id");

            String url=doc.get("url");

             

            record.setName(name);

            record.setSkydriverName(skydirverName);

            record.setUsername(username);

            record.setShareTime(DateFormater.getFormatDate(shareTime,"yyyy-MM-dd HH:mm:ss"));

            record.setDescrib(describ);

            record.setTypeId(Integer.parseInt(typeId));

            record.setId(new BigInteger(id));

            record.setUrl(url);

            list.add(record);

             

            /*System.out.println("name:"+name);

            System.out.println("skydirverName:"+skydirverName);

            System.out.println("username:"+username);

            System.out.println("shareTime:"+shareTime);

            System.out.println("describ:"+describ);

            System.out.println("typeId:"+typeId);

            System.out.println("id:"+id);

            System.out.println("url:"+url);*/

        }

        map.put("source",list);

        return map;

    }

     

    /**

     * 根據前綴查詢

     * @Author haoning

     */

    public void prefixSearch(String filedStr,String queryStr) throws Exception{

        File indexDir = new File(DISC_URL);  

        //索引目錄  

        Directory dir=FSDirectory.open(indexDir);  

        //根據索引目錄建立讀索引對象  

        IndexReader reader = IndexReader.open(dir);  

        //搜索對象建立  

        IndexSearcher searcher = new IndexSearcher(reader);

         

        Term term = new Term(filedStr, queryStr);

        Query query = new PrefixQuery(term);

         

        ScoreDoc[] docs = searcher.search(query, 3).scoreDocs;

        printScoreDoc(docs, searcher);

    }

     

    /**

     * 通配符查詢

     * @Author haoning

     */

    public void wildcardSearch(String filedStr,String queryStr) throws Exception{

        File indexDir = new File(DISC_URL);  

        //索引目錄  

        Directory dir=FSDirectory.open(indexDir);  

        //根據索引目錄建立讀索引對象  

        IndexReader reader = IndexReader.open(dir);  

        //搜索對象建立  

        IndexSearcher searcher = new IndexSearcher(reader);

         

        Term term = new Term(filedStr, queryStr);

        Query query = new WildcardQuery(term);

        ScoreDoc[] docs = searcher.search(query, 3).scoreDocs;

        printScoreDoc(docs, searcher);

    }

     

    /**

     * 分詞查詢

     * @Author haoning

     */

    public void analyzerSearch(String filedStr,String queryStr) throws Exception{

        File indexDir = new File(DISC_URL);  

        //索引目錄  

        Directory dir=FSDirectory.open(indexDir);  

        //根據索引目錄建立讀索引對象  

        IndexReader reader = IndexReader.open(dir);  

        //搜索對象建立  

        IndexSearcher searcher = new IndexSearcher(reader);

         

        QueryParser queryParser = new QueryParser(version, filedStr, analyzer);

        Query query = queryParser.parse(queryStr);

         

        ScoreDoc[] docs = searcher.search(query, 3).scoreDocs;

        printScoreDoc(docs, searcher);

    }

     

    /**

     * 多屬性分詞查詢

     * @Author haoning

     */

    public void multiAnalyzerSearch(String[] filedStr,String queryStr) throws Exception{

        File indexDir = new File(DISC_URL);  

        //索引目錄  

        Directory dir=FSDirectory.open(indexDir);  

        //根據索引目錄建立讀索引對象  

        IndexReader reader = IndexReader.open(dir);  

        //搜索對象建立  

        IndexSearcher searcher = new IndexSearcher(reader);

        QueryParser queryParser = new MultiFieldQueryParser(version, filedStr, analyzer);

        Query query = queryParser.parse(queryStr);

         

        ScoreDoc[] docs = searcher.search(query, 3).scoreDocs;

        printScoreDoc(docs, searcher);

    }

     

    public void printScoreDoc(ScoreDoc[] docs,IndexSearcher searcher)throws Exception{

        for (int i = 0; i < docs.length; i++) {

            List<Fieldable> list = searcher.doc(docs[i].doc).getFields();

            for (Fieldable fieldable : list) {

                String fieldName = fieldable.name();

                String fieldValue = fieldable.stringValue();

                System.out.println(fieldName+" : "+fieldValue);

            }

        }

    }

}

注意因爲去轉盤網(http://www.quzhuanpan.com)是部署到linux上的,因此DISC_URL能夠更具系統變換,我是經過url來斷定索引文件是否惟一的,你能夠更具id來判斷,具體狀況具體對待吧。ionic

package com.tray.indexData;



import java.sql.SQLException;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import com.mysql.jdbc.Connection;

import com.mysql.jdbc.ResultSet;

import com.mysql.jdbc.Statement;



public class IndexFile {

     

     private static Connection conn = null;     

     private static Statement stmt = null;  

     private final int NUM=500000;

     private LuceneSearch ls;

     private long count=0;

      

     public ResultSet deal6SourceTable(String tableName) throws SQLException{

           String sql = "SELECT distinct `NAME`,SKYDRIVER_NAME,USERNAME,SHARE_TIME,DESCRIB,TYPE_ID,ID,URL FROM "+tableName+" where STATUS=1 and TYPE_ID !='-1' and (TYPE_NAME is null or TYPE_NAME!=1) limit "+NUM;

           //System.out.println(sql);

           ResultSet rs = (ResultSet) stmt.executeQuery(sql);

           return rs;

     }

      

     public void update6SourceTable(String tableName) throws SQLException{

           Statement st = (Statement) conn.createStatement();

           String sql = "update "+tableName+" set TYPE_NAME=1 where STATUS=1 and TYPE_ID !='-1' and (TYPE_NAME is null or TYPE_NAME!=1) limit "+NUM;

           //System.out.println("update"+sql);

            try {

                st.executeUpdate(sql);

            } catch (SQLException e) {

                e.printStackTrace();

            }

     }

      

     public void indexInit(){//數據庫+lcene初始化

        conn = (Connection) JdbcUtil.getConnection();     

        if(conn == null) {     

            try {

                throw new Exception("數據庫鏈接失敗!");

            } catch (Exception e) {

                e.printStackTrace();

            }     

        }

        ls=new LuceneSearch();

        try {

            ls.init();

        } catch (Exception e2) {

            e2.printStackTrace();

        }

     }

      

     public void indexEnd(){//數據庫+lcene關閉

          

         ls.closeWriter();

         try {

                conn.close();//關閉數據庫

             } catch (SQLException e) {

                e.printStackTrace();

          }

     }

      

     public void Index6Data() throws SQLException{   

            try {

                stmt = (Statement) conn.createStatement();

            } catch (SQLException e1) {

                e1.printStackTrace();

            }

             

            ResultSet r1=null;

            ResultSet r2=null;

            ResultSet r3=null;

            ResultSet r4=null;

            ResultSet r5=null;

            ResultSet r6=null;

             

            boolean stop=false;

            do{

                 r1=deal6SourceTable("film_and_tv_info");

                 stop=this.createIndex(r1,ls,"1");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引 

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 //System.out.println("stop"+stop);

                 

            }while(!stop);

            

            stop=false;

            do{

                 r2=deal6SourceTable("music_and_mv_info");

                 stop=this.createIndex(r2,ls,"2");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

             

            stop=false;

            do{

                 r3=deal6SourceTable("e_book_info");

                 stop=this.createIndex(r3,ls,"3");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

             

            stop=false;

            do{

                 r4=deal6SourceTable("bt_file_info");

                 stop=this.createIndex(r4,ls,"4");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

             

            stop=false;

            do{

                 r5=deal6SourceTable("characteristic_software_info");

                 stop=this.createIndex(r5,ls,"5");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

             

            stop=false;

            do{

                 r6=deal6SourceTable("source_code_info");

                 stop=this.createIndex(r6,ls,"6");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

            stop=false;

            

     }

      

     public ResultSet deal2Share(String tableName) throws SQLException{

        String sql = "SELECT  distinct NAME,SKYDRIVER_NAME,USERNAME,SHARE_TIME,DESCRIB,TYPE_ID,ID,SHORTURL from "+tableName+" where STATUS=1  and FS_ID ='1' limit "+NUM; //利用FS_ID這個字段,沒什麼用處 

        ResultSet rs = (ResultSet) stmt.executeQuery(sql);

        return rs;

    }

     

    public ResultSet deal3Share(String tableName) throws SQLException{

        String sql = "SELECT  distinct title,channel,uid,ctime,description,port,id,shorturl from "+tableName+" where name ='1' limit "+NUM;  

        ResultSet rs = (ResultSet) stmt.executeQuery(sql);

        return rs;

    }

     

    public void Index3Data() throws SQLException{

            try {

                stmt = (Statement) conn.createStatement();

            } catch (SQLException e1) {

                e1.printStackTrace();

            }

             

            ResultSet r1=null;

            ResultSet r2=null;

            ResultSet r3=null;

             

            boolean stop=false;

            do{

                 r1=deal2Share("share1");

                 stop=this.createIndex(r1,ls,"7");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 //System.out.println("stop"+stop);

                 

            }while(!stop);

            

            stop=false;

            do{

                 r2=deal2Share("share2");

                 stop=this.createIndex(r2,ls,"8");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

             

            stop=false;

            do{

                 r3=deal3Share("share3");

                 stop=this.createIndex(r3,ls,"9");   //給數據庫建立索引,此處執行一次,不要每次運行都建立索引,之後數據有更新能夠後臺調用更新索引  

                 if(!stop){

                     ls.commit();//加個判斷條件

                 }

                 

            }while(!stop);

            stop=false;

        }

     

        public void update2ShareTable(String tableName) throws SQLException{

            Statement st = (Statement) conn.createStatement();

           String sql = "update "+tableName+" set FS_ID=0 where STATUS=1  and FS_ID ='1' limit "+NUM; //利用FS_ID這個字段,沒什麼用處 

           //System.out.println("update"+sql);

            try {

                st.executeUpdate(sql);

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

         

        public void update3ShareTable(String tableName) throws SQLException{

            Statement st = (Statement) conn.createStatement();

           String sql = "update "+tableName+" set name=0 where name ='1' limit "+NUM;  

           //System.out.println("update"+sql);

            try {

                st.executeUpdate(sql);

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

            

        public boolean createIndex(ResultSet rs,LuceneSearch ls,String mark) {

            try {

                String tableName=null;

                if(mark.equals("1")){

                    tableName="film_and_tv_info";

                }

                if(mark.equals("2")){

                    tableName="music_and_mv_info";

                }

                if(mark.equals("3")){

                    tableName="e_book_info";

                }

                if(mark.equals("4")){

                    tableName="bt_file_info";

                }

                if(mark.equals("5")){

                    tableName="characteristic_software_info";

                }

                if(mark.equals("6")){

                    tableName="source_code_info";

                }

                if(mark.equals("7")){

                    tableName="share1";

                }

                if(mark.equals("8")){

                    tableName="share2";

                }

                if(mark.equals("9")){

                    tableName="share3";

                }



                boolean isNull=rs.next();

                //System.out.println("hehe"+isNull);

                if(isNull==false){

                    return true;//處理完畢

                }

                while(isNull){

                    if(Integer.parseInt(mark)>=1&&Integer.parseInt(mark)<=8){

                        Document doc = new Document();  

                        //System.out.println("name"+rs.getString("NAME"));        

                        Field name = new Field("name",rs.getString("NAME"),Field.Store.YES,Field.Index.ANALYZED);

                        String skName=rs.getString("SKYDRIVER_NAME");

                        if(skName==null){

                            skName="百度";

                        }

                        Field skydirverName = new Field("skydirverName",skName, Field.Store.YES,Field.Index.NOT_ANALYZED);

                        Field username = new Field("username",rs.getString("USERNAME"),Field.Store.YES, Field.Index.ANALYZED);    

                        Field shareTime = new Field("shareTime",rs.getString("SHARE_TIME"), Field.Store.YES,Field.Index.NOT_ANALYZED);

                        String desb=rs.getString("DESCRIB");

                        if(desb==null){

                            desb="-1";

                        }

                        Field describ = new Field("describ",desb,Field.Store.NO,Field.Index.NOT_ANALYZED);     

                        Field typeId = new Field("typeId",rs.getString("TYPE_ID"), Field.Store.YES,Field.Index.NOT_ANALYZED); 

                        Field id = new Field("id",rs.getString("ID"),Field.Store.YES,Field.Index.NOT_ANALYZED);

                        Field url =null;

                        if(Integer.parseInt(mark)>=7&&Integer.parseInt(mark)<=8){

                            url = new Field("url",rs.getString("SHORTURL"), Field.Store.YES,Field.Index.ANALYZED); 

                        }

                        else{

                            url = new Field("url",rs.getString("URL"), Field.Store.YES,Field.Index.ANALYZED);  

                        }

                        doc.add(name);

                        doc.add(skydirverName);

                        doc.add(username);

                        doc.add(shareTime);

                        doc.add(describ);

                        doc.add(typeId);

                        doc.add(id);

                        doc.add(url);

                        ls.singleUpdate(doc);//用跟新更爲合適     

                        isNull=rs.next();

                    }

                    else{

                        Document doc = new Document();  

                        //System.out.println("title"+rs.getString("title"));        

                        Field name = new Field("name",rs.getString("title"),Field.Store.YES,Field.Index.ANALYZED);

                        String skName=rs.getString("channel");

                        Field skydirverName = new Field("skydirverName",skName, Field.Store.YES,Field.Index.NOT_ANALYZED);

                        Field username = new Field("username",rs.getString("uid"),Field.Store.YES, Field.Index.ANALYZED);     

                        Field shareTime = new Field("shareTime",rs.getString("ctime"), Field.Store.YES,Field.Index.NOT_ANALYZED);

                        String desb=rs.getString("description");

                        if(desb==null){

                            desb="-1";

                        }

                        Field describ = new Field("describ",desb,Field.Store.NO,Field.Index.NOT_ANALYZED);     

                        Field typeId = new Field("typeId",rs.getString("port"), Field.Store.YES,Field.Index.NOT_ANALYZED);

                        Field id = new Field("id",rs.getString("id"),Field.Store.YES,Field.Index.NOT_ANALYZED);    

                        Field url = new Field("url",rs.getString("shorturl"), Field.Store.YES,Field.Index.ANALYZED);  

                         

                        doc.add(name);

                        doc.add(skydirverName);

                        doc.add(username);

                        doc.add(shareTime);

                        doc.add(describ);

                        doc.add(typeId);

                        doc.add(id);

                        doc.add(url);

                        ls.singleUpdate(doc);//用跟新更爲合適     

                        isNull=rs.next();

                    }

                    count=count+1;

                }

                if(Integer.parseInt(mark)>=1&&Integer.parseInt(mark)<=6){

                    update6SourceTable(tableName);//處理完成後作標誌

                }

                else if(Integer.parseInt(mark)>=7&&Integer.parseInt(mark)<=8){

                    update2ShareTable(tableName);//處理完成後作標誌

                }

                else{

                    update3ShareTable(tableName);//處理完成後作標誌

                }

                System.out.println("Has index "+count+"條數據,數據來自表"+tableName);

                 

            } catch (Exception e) {

                e.printStackTrace();

            }

            return false;

        }

}

數據庫之類的請不要關心,看思路便可,你若是須要換成你的便可,這裏就很少說了。

看最後的部分:

package com.tray.indexData;



import java.sql.SQLException;







public class Application {

     

    public static void main(String[] args){

        /*IndexFile indexFile=new IndexFile();

        indexFile.indexInit();

        try {

            indexFile.Index6Data();

        } catch (SQLException e1) {

            e1.printStackTrace();

        }

        indexFile.indexEnd();*/

         

        IndexFile indexFile1=new IndexFile();

        indexFile1.indexInit();

        try {

            indexFile1.Index3Data();

        } catch (SQLException e1) {

            e1.printStackTrace();

        }

        indexFile1.indexEnd();

         

        LuceneSearch lch=new LuceneSearch();

        try {

            long a = System.currentTimeMillis();

            lch.highLightSearch("name", "flv", 1,3);

            long b = System.currentTimeMillis();

            long c = b - a;

            System.out.println("[高級檢索花費時間:" + c + "毫秒]");

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

你能夠在一個applicationic程序中開始索引,也能夠寫個定時器來定時索引,看需求。以上代碼是樓主幸苦的做品,轉載請不要改動,本人確保代碼徹底可用。

相關文章
相關標籤/搜索