索引的檢索相關代碼

來源:互聯網
上載者:User

標籤:style   blog   ar   java   sp   for   檔案   on   2014   

常用類簡介
Directory 指定索引所在目錄
FSDirectory 存放於磁碟上的檔案系統
RAMDirectory 存放於記憶體中的目錄,用於測試等用途
如Directory directory=FSDirectory.open(new File("filePath"));

IndexReader 讀索引,依靠Directory類初始化
如 IndexReader reader=DirectoryReader.open(directory);

IndexSearcher 用於索引檢索,依靠IndexReader類初始化。依靠Query類進行搜尋。
如IndexSearcher searcher=new IndexSearcher(reader);

Analyzer 分詞器
如Analyzer analyzer=new StandardAnalyzer();

Term 描述要尋找單詞所在的域(Field)與內容(content)。
如Term term=new Term("field","content");

Query 查詢類,抽象類別
TermQuery 最簡單、最基本的Query,用來查詢不切分的單詞。依靠Term類初始化
如Query query=new TermQuery(term);

TopDocs 存放IndexSearcher類的查詢結果
如TopDocs topDocs=searcher.search(query,10);

ScoreDoc ScoreDoc[]數組用來存放查詢結果中的文檔資訊
如ScoreDoc[] scoreDocs = topdocs.scoreDocs;

ScoreDoc 中的score屬性工作表示相關程度,取值範圍[0,1],越大越相關。

概覽:

Directory directory=FSDirectory.open(new File("filePath"));IndexReader reader=DirectoryReader.open(directory);IndexSearcher searcher=new IndexSearcher(reader);Analyzer analyzer=new StandardAnalyzer();Term term=new Term("field","content");Query query=new TermQuery(term);TopDocs topDocs=searcher.search(query, 10);ScoreDoc[] hits=topDocs.scoreDocs;for(ScoreDoc hit :hits){System.out.println(hit.doc+hit.score);}


索引的檢索相關代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.