跳到主要內容

Lucene(一):Lucene4.3.1、Tika、IKAnalyzer中文分詞

Lucene是一個很強大的搜尋引擎(Lucene wiki),重點是Open Source,還有很多子專案很好用喔,這次來介紹一下Tika這個子專案,它是一個文件擷取內容及文件metadata的函式庫,支援的檔案格式可以參考一下這裡,加上Lucene Core及中文IKAnalyzer就可以組合文件的中文全文檢索喔。

先下載
Lucene 4.3.1
Tika
IKAnalyzer



Lucene 4.3.1

  • 下載後解壓縮,分別複製以下的jar
    • lucene-queryparser-4.3.1.jar
    • lucene-queries-4.3.1.jar
    • lucene-core-4.3.1.jar
    • lucene-analyzers-common-4.3.1.jar
Tika 1.4
  • 請下載Source檔案,另外請在下載maven,因為Tika建議重新編譯一份出來,編譯方式參這裡,分別複製以下的jar
    • tika-xmp-1.4.jar
    • tika-server-1.4.jar
    • tika-parsers-1.4.jar
    • tika-core-1.4.jar
    • tika-bundle-1.4.jar
    • tika-app-1.4.jar
    • original-tika-app-1.4.jar
IKAnalyzer
  • 下載後解壓縮,複製以下的jar
    • IKAnalyzer2012FF_ul.jar

TvDocumentVo.java:儲存 Tika解析文件內容vo。
public class TvDocumentVo { 
   private Metadata metadata;
    private String content;
    public Metadata getMetadata() {
        return metadata;
    }
    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
TvDocmentExtract.jar:使用Tika將解析出來的文件內容轉成List。P.S這裡需要調整當檔案數量及大小過大時。

public class TvDocmentExtract {
    public List parseAllFilesInDirectory(File directory) throws IOException, SAXException, TikaException {
        List result = new ArrayList();
        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                parseAllFilesInDirectory(file);
            } else {
                Parser parser = new AutoDetectParser();
                Metadata metadata = new Metadata();
                ParseContext parseContext = new ParseContext();
                ContentHandler handler = new BodyContentHandler(100*100*1024);//若是檔案過大可以放大這裡的參數
                parser.parse(new FileInputStream(file), handler, metadata, parseContext);
                TvDocumentVo vo = new TvDocumentVo();
                metadata.set("filename",file.getAbsolutePath());//增加檔案絕對路徑屬性
                vo.setMetadata(metadata);
                vo.setContent(handler.toString());
                result.add(vo);
            }
        }
        return result;
    }
}
TvIndexManagement.java:建立索引

public class TvIndexManagement {
    
    public static void main(String[] arg) throws ParseException, IOException{
        TvIndexManagement tim = new TvIndexManagement();
        tim.createIndex("D:\\index", "D:\\document");//參數一為建立索引目錄,參數二為要進行文件擷取目錄
       
    }
    
    public void createIndex(String indexDir,String filesPath){
        this.ikanalyzerIndex(indexDir, filesPath);
    }
    public RAMDirectory readfsIndexToRam(String indexDir) throws IOException{
        Directory fsDir = FSDirectory.open(new File(indexDir));
         IOContext ioContext = new IOContext(Context.DEFAULT);
        return new RAMDirectory(fsDir, ioContext);
    }
    
    private void ikanalyzerIndex(String indexDir,String filesPath){
        Analyzer analyzer = new IKAnalyzer(true);//可參IKAnalyzer說明
        try {
            Directory index = FSDirectory.open(new File(indexDir));
            IOContext ioContext = new IOContext(Context.DEFAULT);
           // Directory index = new RAMDirectory(fsDir, ioContext);
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43,analyzer);
            IndexWriter w = new IndexWriter(index, config);
            config.setMaxBufferedDocs(1000);//純粹測試效能調教
            config.setRAMBufferSizeMB(40);//純粹測試效能調教
           TvDocmentExtract tde = new TvDocmentExtract();
            try {
                List lists=tde.parseAllFilesInDirectory(new File(filesPath));
                for(TvDocumentVo vo :lists){
                    this.addDocument(w,vo.getMetadata().get("filename"),vo.getContent());
                }
                w.close();
            } catch (SAXException ex) {
                Logger.getLogger(TvIndexManagement.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TikaException ex) {
                Logger.getLogger(TvIndexManagement.class.getName()).log(Level.SEVERE, null, ex);
            }            
        } catch (IOException ex) {
            Logger.getLogger(TvIndexManagement.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
    private void addDocument(IndexWriter writer,String filename,String content) throws IOException{
        Document doc = new Document();
        //System.out.println(filename);
        doc.add(new TextField("filename", filename, Field.Store.YES));
         doc.add(new TextField("content", content, Field.Store.YES));
         writer.addDocument(doc);
    }    
}
全文檢索範例程式碼

  String querystr = "合法";
        Directory index = FSDirectory.open(new File("D:\\index"));
        Analyzer analyzer = new IKAnalyzer(true);
        QueryParser qp = new QueryParser(Version.LUCENE_43, "content", analyzer);
        qp.setDefaultOperator(QueryParser.Operator.AND);
        Query q = qp.parse(querystr);
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        collector.getTotalHits();
           System.out.println("Found " + hits.length + " hits ==>" + collector.getTotalHits());
        for (int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            System.out.println((i + 1) + ". " + d.get("filename"));
        }
        reader.close();
目前正在試試看,不使用Hadoop方式加快建立索引時間,基本上會用Queue方式進行,下次再介紹囉。

留言

這個網誌中的熱門文章

IReport中的條碼類別BcImage

最近有一個繳費單的專案,需要列印條碼,因為IReport本身就有提供列印條碼的功能,所以就用IReport設計繳費單然後再用Jasperreport API寫批次程式去產生PDF,資料量大概3000多筆(頁)拆成幾個檔案,可是就發生了一個問題居然發生部分繳費單的條碼有問題,看了一下程式研判是Race Condition因為程式採用的是多執行緒,若是依序個別產生是不會有問題,但是同時執行的時候就會亂掉,而且都錯在條碼,令我覺得很奇怪,後來我查了一下發現it.businesslogic.ireport.barcode.BcImage 類別我猜是這裡的問題,就先加上synchronized重新編譯在去執行,嘿嘿條碼就對了,做了一下壓測也正常,程式碼如下,不過這樣事不是對了可能還要仔細查查看。 public class BcImage { private static net.sourceforge.barbecue.Barcode bc = null; public synchronized static net.sourceforge.barbecue.Barcode getBarcode() { return bc; } public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum) { return getBarcodeImage(type, aText, showText, checkSum,"",0,0); } public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum, String applicationIdentifier, int width, int height) { // 2of7, 3of9, Bookland, Codabar, Code128,...

IBM MQ 5.3 如何匯出Qmgr

使用IBM MQ探險家也沒有匯出的功能,這樣其實在一些備份或是Cluster時,滿困擾的,不過你也知道IBM就是喜歡用SupportPacs等等的方式,ms03這個SupportPacs就是可以匯出Qmgr中所有的資訊也可以選擇想要的,以下是他的連結 http://www-1.ibm.com/support/docview.wss?rs=171&q1=mA1J&uid=swg24000673&loc=en_US&cs=utf-8&lang=en 若是Window的版本可以直接使用,若是unix或是linux系列的就請重新編譯了﹝makeFile﹞我有試過在RHEL編譯使用是可以的。 ※請注意一定要在有安裝MQ的Server方可執行喔 使用方式如下: saveqmgr -m xxx -v 53 -o xxx.cfg -m 指定需要匯出的Qmgr名稱 -v 版本 -o 匯出後檔案名稱 以下是相關參數的列表 Usage is: ./saveqmgr. [options], where [options] are one or more of the following optional switches -h | -? : gives help (this) -v version : determines which version of MQSC to generate and can be '2','5','51','52','53' or '6' The default is to generate mqsc at the version of the connected queue manager -m lqmgr : is the name of the local qmgr to connect (MQCONN) -r rqmgr : is the name of the remote qmgr (XMITQ name) -f [file] : allows the output file to be named, if -f is not specified, the outpu...

Ext-Js Grid + DWR

Ext-Js中有Grid的sample,想說試試看加上DWR的效果如何?感覺上還不錯,以下是我參考Ext-Js附的grid array sample,加上DWR調整一下的code,我想可能還要加上資料在Loading的效果會比較好。 array-grid.js Ext.onReady(function(){ Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); // example of custom renderer function function change(val){ if(val > 0){ return ' ' + val + ' '; }else if(val ' + val + ' '; } return val; } // example of custom renderer function function pctChange(val){ if(val > 0){ return ' ' + val + '% '; }else if(val ' + val + '% '; } return val; } //要設定Dwr傳回的Map的對應格式 var recordType = Ext.data.Record.create( [ {name:"reportid",mapping:"reportid",type:"string"}, {name:"reportName",mapping:"reportName",type:"string"} ] ); var myReader = new Ext.data.JsonReader( { totalProperty:"totalSize", root:"data" },recordType ); // create the data store //這裡是很重要的,這裡還可以加上listener等等的屬性喔 var store = new Ext.da...