亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

lucene簡單入門

Ververica / 2627人閱讀

摘要:序說是界的檢索之王,當之無愧。近年來的火爆登場,包括之前的及,其底層都是。簡單了解,對使用還是有點幫助的。是當前最流行的開源大數(shù)據(jù)內(nèi)存計算框架,采用語言實現(xiàn),由伯克利大學實驗室開發(fā)并于年開源。

說lucene是Java界的檢索之王,當之無愧。近年來elasticsearch的火爆登場,包括之前的solr及solr cloud,其底層都是lucene。簡單了解lucene,對使用elasticsearch還是有點幫助的。本文就簡單過一下其簡單的api使用。

添加依賴
        
            org.apache.lucene
            lucene-core
            4.6.1
        
        
            org.apache.lucene
            lucene-analyzers-common
            4.6.1
        
        
            org.apache.lucene
            lucene-queryparser
            4.6.1
        
        
            org.apache.lucene
            lucene-codecs
            4.6.1
        
索引與檢索 創(chuàng)建索引
File indexDir = new File(this.getClass().getClassLoader().getResource("").getFile());

    @Test
    public void createIndex() throws IOException {
//        Directory index = new RAMDirectory();
        Directory index = FSDirectory.open(indexDir);
        // 0. Specify the analyzer for tokenizing text.
        //    The same analyzer should be used for indexing and searching
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);

        // 1. create the index
        IndexWriter w = new IndexWriter(index, config);
        addDoc(w, "Lucene in Action", "193398817");
        addDoc(w, "Lucene for Dummies", "55320055Z");
        addDoc(w, "Managing Gigabytes", "55063554A");
        addDoc(w, "The Art of Computer Science", "9900333X");
        w.close();
    }

    private void addDoc(IndexWriter w, String title, String isbn) throws IOException {
        Document doc = new Document();
        doc.add(new TextField("title", title, Field.Store.YES));
        // use a string field for isbn because we don"t want it tokenized
        doc.add(new StringField("isbn", isbn, Field.Store.YES));
        w.addDocument(doc);
    }
檢索
 @Test
    public void search() throws IOException {
        // 2. query
        String querystr = "lucene";

        // the "title" arg specifies the default field to use
        // when no field is explicitly specified in the query.
        Query q = null;
        try {
            StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
            q = new QueryParser(Version.LUCENE_46,"title", analyzer).parse(querystr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 3. search
        int hitsPerPage = 10;
        Directory index = FSDirectory.open(indexDir);
        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;

        // 4. display results
        System.out.println("Found " + hits.length + " hits.");
        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("isbn") + "	" + d.get("title"));
        }

        // reader can only be closed when there
        // is no need to access the documents any more.
        reader.close();
    }
分詞

對于搜索來說,分詞出現(xiàn)在兩個地方,一個是對用戶輸入的關(guān)鍵詞進行分詞,另一個是在索引文檔時對文檔內(nèi)容的分詞。兩個分詞最好一樣,這樣才可以更好地匹配出來。

    @Test
    public void cutWords() throws IOException {
//        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
//        CJKAnalyzer analyzer = new CJKAnalyzer(Version.LUCENE_46);
        SimpleAnalyzer analyzer = new SimpleAnalyzer();
        String text = "Spark是當前最流行的開源大數(shù)據(jù)內(nèi)存計算框架,采用Scala語言實現(xiàn),由UC伯克利大學AMPLab實驗室開發(fā)并于2010年開源。";
        TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        try {
            tokenStream.reset();
            while (tokenStream.incrementToken()) {
                System.out.println(charTermAttribute.toString());
            }
            tokenStream.end();
        } finally {
            tokenStream.close();
            analyzer.close();
        }
    }

輸出

spark
是
當前
最
流行
的
開源
大數(shù)
據(jù)
內(nèi)存
計算
框架
采用
scala
語言
實現(xiàn)
由
uc
伯克利
大學
amplab
實驗室
開發(fā)
并于
2010
年
開源

本工程github

參考

lucenetutorial

helloLucene

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/65464.html

相關(guān)文章

  • Lucene系列(一)快速入門

    摘要:系列文章系列一快速入門系列二使用及索引文檔的基本操作系列三查詢及高亮是什么在維基百科的定義是一套用于全文檢索和搜索的開放源代碼程序庫,由軟件基金會支持和提供。全面準確和快速是衡量全文檢索系統(tǒng)的關(guān)鍵指標。結(jié)果列表有相關(guān)度排序。 系列文章: Lucene系列(一)快速入門 Lucene系列(二)luke使用及索引文檔的基本操作 Lucene系列(三)查詢及高亮 Lucene是什么? Luc...

    騫諱護 評論0 收藏0
  • Lucene系列(二)luke使用及索引文檔的基本操作

    摘要:系列文章系列一快速入門系列二使用及索引文檔的基本操作系列三查詢及高亮入門簡介地址下載地址是一個用于搜索引擎的,方便開發(fā)和診斷的可視化工具。使用作為其最低級別的搜索引擎基礎。截止,上述代碼所用的包皆為最新。 系列文章: Lucene系列(一)快速入門 Lucene系列(二)luke使用及索引文檔的基本操作 Lucene系列(三)查詢及高亮 luke入門 簡介: github地址:http...

    hedzr 評論0 收藏0
  • Elasticsearch Lucene 數(shù)據(jù)寫入原理 | ES 核心篇

    摘要:因為倒排索引打分機制全文檢索原理分詞原理等等,這些都是不會過時的技術(shù)。中,單個倒排索引文件稱為。其中有一個文件,記錄了所有的信息,稱為文檔新寫入時,會生成新的。過程上個過程中在文件系統(tǒng)緩存中,會有意外故障文檔丟失。寫入次怕后,清空。 前言 最近 TL 分享了下 《Elasticsearch基礎整理》,蹭著這個機會。寫個小文鞏固下,本文主要講 ES -> Lucene的底層結(jié)構(gòu),然后詳細...

    wums 評論0 收藏0
  • Spring Boot 2.x(十七):快速入門Elastic Search

    摘要:極速的查詢速度通過有限狀態(tài)轉(zhuǎn)換器實現(xiàn)了用于全文檢索的倒排索引,實現(xiàn)了用于存儲數(shù)值數(shù)據(jù)和地理位置數(shù)據(jù)的樹,以及用于分析的列存儲。每個數(shù)據(jù)都被編入了索引。強大的彈性保障硬件故障。檢測這些故障并確保集群和數(shù)據(jù)的安全性和可用性。 What —— Elasticsearch是什么? Elasticsearch是一個基于Lucene的搜索服務器,Elasticsearch也是使用Java編寫的,它...

    yangrd 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<