Lucene(一)——基础

作者: 康康不遛猫 | 来源:发表于2018-07-23 19:59 被阅读13次

1、倒排索引

根据词条查询文档。
名词概念:
◦文档(Document):索引库中的每一条原始数据。
◦词条(Term):原始数据按照算法进行分词,得到的每一个词语。
◦文档列表:Lucene 对原始文档进行编号(DocID),形成的列表就是文档列表。
倒排索引建立过程:
1、创建文档列表:Lucene 首先对原始文档数据进行编号(DocId),形成文档列表


Paste_Image.png

2、创建倒排索引列表:
对文档中数据进行分词,得到 词条(Term)。对词条添加编号并创建索引,并在词条中记录包含该词条的所有文档编号及其他信息。


Paste_Image.png
3搜索过程:
.获得用户搜索内容,对搜索内容进行分词,得到用户搜索的所有词条。
.将词条在倒排索引列表中进行匹配,得到包含该词条的所有文档编号。

Lucene 全文检索就是对文档中全部内容进行分词,然后对所有单词建立倒排索引的过程。

2、lucene基础

概念

Paste_Image.png
Lucene创建索引流程图
流程说明:
创建文档对象(Document),并添加索引Field字段(Field)
创建目录对象(Directory)并指定索引在硬盘中存储位置
创建分词器对象(Analyzer)
创建索引写出器配置对象(IndexWriterConfig)(指定Analyzer和Version等)
创建索引写出器(IndexWriter)
索引写出器,添加文档对象
提交并关闭索引写出器

代码简单示例:

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class PhraseQueryTest {

    public static void main(String[] args) throws Exception {
        //在内存中建立索引
        Directory directory = new RAMDirectory();
        createIndex(directory);
        queryIndex(directory);
    }

    private static Document createDocument(String id, String content) {
        Document doc = new Document();
        doc.add(new Field("id", id, StringField.TYPE_STORED));
        doc.add(new Field("contents", content, TextField.TYPE_STORED));
        return doc;
    }
    
    private static void createIndex(Directory directory) throws Exception {
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48);//分词器
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48,analyzer);
        IndexWriter writer = new IndexWriter(directory, iwc);
        // 索引一些文档
        writer.addDocument(createDocument("1", "foo bar baz"));
        writer.addDocument(createDocument("2", "red green blue"));
        writer.addDocument(createDocument("3", "test foo bar test"));
        writer.close();
    }

    private static void queryIndex(Directory directory) throws Exception {
        // 查找包含"foo bar"这个短语的文档
        String inputStr = "foo bar";
        IndexReader reader = DirectoryReader.open(directory);

        // 根据IndexReader创建IndexSearcher
        IndexSearcher searcher = new IndexSearcher(reader);
        PhraseQuery query = new PhraseQuery();
        String[] words = inputStr.split(" ");
        for (String word : words) {
            query.add(new Term("contents", word));
        }

        // 显示搜索结果
        TopDocs topDocs = searcher.search(query, 10);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document doc = searcher.doc(scoreDoc.doc);
            System.out.println(doc.getField("contents").stringValue());
        }
    }
}

3、luence知识图谱

Paste_Image.png

refer:http://www.jianshu.com/p/c8793a06f5ae

相关文章

  • Lucene(一)——基础

    1、倒排索引 根据词条查询文档。名词概念:◦文档(Document):索引库中的每一条原始数据。◦词条(Term)...

  • Lucene基础

    简介 Lucene是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构...

  • 搜索学习入门--使用LuceneHighlighter高亮显示L

    在上一篇文章搜索学习入门--Lucene初体验(Lucene索引的增删改查)的基础上,我们进行对Lucene检索结...

  • Lucene基础篇-初识Lucene

    初识Lucene 一般来说,在学习一样东西的时候,总是遵循一定的过程:是什么、为什么、怎么做。 作为Lucene的...

  • Lucene总结

    1 Lucene基础2 Lucene建索引和搜索3 域选项4 各种Query5 Lucene分词器6 近实时索引7...

  • Lucene基础入门.

    一 什么是Lucene Lucene是apache下的一个开放源代码的全文检索引擎工具包。提供了完整的查询引擎和索...

  • Elasticsearch routing功能

    Elasticsearch是一个搭建在Lucene搜索引擎库基础之上的搜索服务平台。它在单机的Lucene搜索引擎...

  • ES、Kibana的安装、配置和运行--Windows

    基础 面向文档 基于Apache Lucene Windows安装ES Windows安装Kibana When ...

  • lucene入门基础2

    【存储域选项】 Field.Store.Yes or NO 【存储域选项】设置为YES表示会将域中的内容完全存储到...

  • lucene入门基础1

    各版本luke下载地址:https://github.com/DmitryKey/luke/releases 各版...

网友评论

    本文标题:Lucene(一)——基础

    本文链接:https://www.haomeiwen.com/subject/hnlqfxtx.html