Lucene(一)——基础
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 【Lucene(一)——基础】refer:http://www.jianshu.com/p/c8793a06f5ae
推荐阅读
- 一个人的旅行,三亚
- 急于表达——往往欲速则不达
- 慢慢的美丽
- 一个小故事,我的思考。
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 开学第一天(下)
- 一个人的碎碎念
- 2018年11月19日|2018年11月19日 星期一 亲子日记第144篇
- 2019-02-13——今天谈梦想()
- 遇到一哭二闹三打滚的孩子,怎么办┃山伯教育