12. 倒排索引(inverted index)In computer science, an inverted index (also referred to as postings file or inverted file) is an index data structure storing a mapping from content, such as words or numbers, to its locations in a database file, or in a document or a set of documents. The purpose of an inverted index is to allow fast full text searches, at a cost of increased processing when a document is added to the database. The inverted file may be the database file itself, rather than its index. It is the most popular data structure used in document retrieval systems,[1] used on a large scale for example in search engines. Several significant general-purpose mainframe-based database management systems have used inverted list architectures, including ADABAS,DATACOM/DB, and Model 204.
倒排索引(英语:Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。
13. 倒排索引(inverted index)例如,下面是要被索引的文本:
T0 = "it is what it is"
T1 = "what is it"
T2 = "it is a banana"
我们可以得到下面的完全反向文件索引(full inverted index) :
"a": {(2, 2)}
"banana": {(2, 3)}
"is": {(0, 1), (0, 4), (1, 1), (2, 1)}
"it": {(0, 0), (0, 3), (1, 2), (2, 0)}
"what": {(0, 2), (1, 0)}
如果我们执行短语搜索"what is it" 我们得到这个短语的全部单词各自的结果所在文档为文档0和文档1。但是这个短语检索的连续的条件仅仅在文档1得到。
15. Lucene中的重要概念(一)索引核心类
Analyzer 文本文件被索引之前,需要经过Analyzer处理,有IndexWriter的构造方法指定,负责从被索引文本文件中提取token,并剔除余下的词语
Document 是用来描述文档的,这里的文档可以指一个 HTML 页面,一封电子邮件,或者是一个文本文件。一个 Document 对象由多个 Field 对象组成的。可以把一个 Document 对象想象成数据库中的一个记录,而每个 Field 对象就是记录的一个字段
Directory 这个类代表了 Lucene 的索引的存储的位置,这是一个抽象类,它目前有两个实现,第一个是 FSDirectory,它表示一个存储在文件系统中的索引的位置。第二个是 RAMDirectory,它表示一个存储在内存当中的索引的位置
Field 用来描述一个文档的某个属性的,比如一封电子邮件的标题和内容可以用两个 Field 对象分别描述
IndexWriter 是 Lucene 用来创建索引的一个核心的类,他的作用是把一个个的 Document 对象加到索引中来
16. Analyzer的工作机制
17. Field(String name, String value, Field.Store store, Field.Index index, Field.TermVector termVector)
Field.Store 原始字段是否存储
Field.Store.YES: 存储字段值(未分词前的字段值) Field.Store.NO: 不存储,存储与索引没有关系
Field.Index 原始字段是否以及如何被索引
Field.Index.ANALYZED:分词建索引 Field.Index.ANALYZED_NO_NORMS:分词建索引,但是Field的值不像通常那样被保存,而是只取一个byte,这样节约存储空间 Field.Index.NOT_ANALYZED:不分词且索引 Field.Index.NOT_ANALYZED_NO_NORMS:不分词建索引,Field的值去一个byte保存
Field.Index.No:不分词不索引
No norms means that index-time field and document boosting and field length normalization are disabled. The benefit is less memory usage as norms take up one byte of RAM per indexed field for every document in the index
Field.TermVector 表示文档的条目(由一个Document和Field定位)和它们在当前文档中所出现的次数