黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

【Lucene3.0 初窺】Lucene體系結(jié)構(gòu)概述

系統(tǒng) 2313 0

Lucene 的基本原理與《 全文檢索的基本原理 》是差不多的。

?

Lucene 的源碼主要有 7 個子包,每個包完成特定的功能:

?

包名

功能描述

org.apache.lucene.analysis

語言分析器,主要用于的切詞,支持中文主要是擴展此類

org.apache.lucene.document

索引存儲時的文檔結(jié)構(gòu)管理,類似于關(guān)系型數(shù)據(jù)庫的表結(jié)構(gòu)

org.apache.lucene.index

索引管理,包括索引建立、刪除等

org.apache.lucene.queryParser

查詢分析器,實現(xiàn)查詢關(guān)鍵詞間的運算,如與、或、非等

org.apache.lucene.search

檢索管理,根據(jù)查詢條件,檢索得到結(jié)果

org.apache.lucene.store

數(shù)據(jù)存儲管理,主要包括一些底層的 I/O 操作

org.apache.lucene.util

一些公用類

?

?

? ??? 另外: Lucene 3.0 還有一個 org.apache.lucene.messages 包,這個包增加了本地語言支持 NLS 和軟件系統(tǒng)國際化。

?

?

?

???? 上面的圖可以很明顯的看出 Lucene 的兩大主要的功能: 建立索引 ( 紅色箭頭: Index), 檢索索引 ( 藍色箭頭: Search) 。

  • analysis 模塊主要負責詞法分析及語言處理而形成 Term( ) 。 具體參見文章《 Lucene分析器—Analyzer
  • index 模塊主要負責索引的創(chuàng)建,里面有 IndexWriter
  • store 模塊主要負責索引的讀寫。
  • queryParser 主要負責語法分析。
  • search 模塊主要負責對索引的搜索 ( 其中 similarity 就是相關(guān)性打分 ) 。

講到這里基本上對全文檢索工具包Lucene的原理和結(jié)構(gòu)已經(jīng)有了大致的了解了,下面給出Lucene3.0.1建立索引和檢索索引的基本代碼,關(guān)于Lucene的細節(jié)探討將在后續(xù)文章中展開。

    import java.io.File;  
import java.io.FileReader;  
import java.io.IOException;  
  
import org.apache.lucene.analysis.standard.StandardAnalyzer;  
import org.apache.lucene.document.DateTools;  
import org.apache.lucene.document.Document;  
import org.apache.lucene.document.Field;  
import org.apache.lucene.index.IndexWriter;  
import org.apache.lucene.store.FSDirectory;  
import org.apache.lucene.util.Version;  

public class IndexFiles {
   // 主要代碼 索引docDir文件夾下文檔,索引文件在INDEX_DIR文件夾中  
   public static void main(String[] args) {  
		
	File indexDir=new File("e:\\實驗\\index");
	File docDir = new File("e:\\實驗\\content"); 
	    
	try {  
               //索引器
      	       IndexWriter standardWriter = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);            
               //不建立復合式索引文件,默認的情況下是復合式的索引文件
               standardWriter.setUseCompoundFile(false);
	       String[] files = docDir.list(); 
	       for (String fileStr : files) {  
	           File file = new File(docDir, fileStr);  
	           if (!file.isDirectory()) {         	
	              Document doc = new Document();  
	              //文件名稱,可查詢,不分詞
	              String fileName=file.getName().substring(0,file.getName().indexOf("."));
	              doc.add(new Field("name",fileName, Field.Store.YES, Field.Index.NOT_ANALYZED));    	    
	              //文件路徑,可查詢,不分詞
	              String filePath=file.getPath();
	              doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED));   
	              //文件內(nèi)容,需要檢索
	              doc.add(new Field("content", new FileReader(file)));  
	              standardWriter.addDocument(doc);  
	           }  
	       }  
	       standardWriter.optimize();
               //關(guān)閉索引器
               ?standardWriter.close();  
	 } catch (IOException e) {  
	       System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());  
         }  
     }   
}

  

?

    import java.io.BufferedReader;  
import java.io.File;  
import java.io.IOException;  
import java.io.InputStreamReader;  
  
import org.apache.lucene.analysis.Analyzer;  
import org.apache.lucene.analysis.standard.StandardAnalyzer;  
import org.apache.lucene.document.Document;  
import org.apache.lucene.index.IndexReader;  
import org.apache.lucene.queryParser.QueryParser;  
import org.apache.lucene.search.IndexSearcher;  
import org.apache.lucene.search.Query;  
import org.apache.lucene.search.ScoreDoc;  
import org.apache.lucene.search.Searcher;  
import org.apache.lucene.search.TopScoreDocCollector;  
import org.apache.lucene.store.FSDirectory;  
import org.apache.lucene.util.Version;  
/**
  * 檢索索引
  */  
public class SearchFiles {  
  
    /** Simple command-line based search demo. */  
    public static void main(String[] args) throws Exception {  
  
        String index = "E:\\實驗\\index";  
        String field = "content";  
        String queries = null;  
        boolean raw = false;  
        // 要顯示條數(shù)  
        int hitsPerPage = 10;  
  
        // searching, so read-only=true  
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true); // only  
  
        Searcher searcher = new IndexSearcher(reader);  
        Analyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);  

  
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
        QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,field, standardAnalyzer);  
        while (true) {  
            if (queries == null) // prompt the user  
                System.out.println("Enter query: ");  
  
            String line = in.readLine();  
  
            if (line == null || line.length() == -1)  
                break;  
  
            line = line.trim();  
            if (line.length() == 0)  
                break;  
  
            Query query = parser.parse(line);  
            System.out.println("Searching for: " + query.toString(field));  
  
            doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null);  
        }  
        reader.close();  
    }  
  
    public static void doPagingSearch(BufferedReader in, Searcher searcher, Query query, int hitsPerPage, boolean raw,  
            boolean interactive) throws IOException {  
  
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, false);  
        searcher.search(query, collector);  
        ScoreDoc[] hits = collector.topDocs().scoreDocs;  
  
        int end, numTotalHits = collector.getTotalHits();  
        System.out.println(numTotalHits + " total matching documents");  
  
        int start = 0;  
  
        end = Math.min(hits.length, start + hitsPerPage);  
  
        for (int i = start; i < end; i++) {  
            Document doc = searcher.doc(hits[i].doc);  
            String path = doc.get("path");  
            if (path != null) {  
                System.out.println((i + 1) + ". " + path);    
            } else {  
                System.out.println((i + 1) + ". " + "No path for this document");  
            }  
          }  
      }  
  }  
  

?

?

?

?

【Lucene3.0 初窺】Lucene體系結(jié)構(gòu)概述


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論