摘要:序说是界的检索之王,当之无愧。近年来的火爆登场,包括之前的及,其底层都是。简单了解,对使用还是有点帮助的。是当前最流行的开源大数据内存计算框架,采用语言实现,由伯克利大学实验室开发并于年开源。
序
说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
索引与检索
创建索引
</>复制代码
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();
}
分词
对于搜索来说,分词出现在两个地方,一个是对用户输入的关键词进行分词,另一个是在索引文档时对文档内容的分词。两个分词最好一样,这样才可以更好地匹配出来。
</>复制代码
@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是当前最流行的开源大数据内存计算框架,采用Scala语言实现,由UC伯克利大学AMPLab实验室开发并于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
是
当前
最
流行
的
开源
大数
据
内存
计算
框架
采用
scala
语言
实现
由
uc
伯克利
大学
amplab
实验室
开发
并于
2010
年
开源
参考</>复制代码
本工程github
lucenetutorial
helloLucene
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65464.html
摘要:系列文章系列一快速入门系列二使用及索引文档的基本操作系列三查询及高亮是什么在维基百科的定义是一套用于全文检索和搜索的开放源代码程序库,由软件基金会支持和提供。全面准确和快速是衡量全文检索系统的关键指标。结果列表有相关度排序。 系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 Lucene是什么? Luc...
摘要:系列文章系列一快速入门系列二使用及索引文档的基本操作系列三查询及高亮入门简介地址下载地址是一个用于搜索引擎的,方便开发和诊断的可视化工具。使用作为其最低级别的搜索引擎基础。截止,上述代码所用的包皆为最新。 系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 luke入门 简介: github地址:http...
摘要:因为倒排索引打分机制全文检索原理分词原理等等,这些都是不会过时的技术。中,单个倒排索引文件称为。其中有一个文件,记录了所有的信息,称为文档新写入时,会生成新的。过程上个过程中在文件系统缓存中,会有意外故障文档丢失。写入次怕后,清空。 前言 最近 TL 分享了下 《Elasticsearch基础整理》,蹭着这个机会。写个小文巩固下,本文主要讲 ES -> Lucene的底层结构,然后详细...
摘要:极速的查询速度通过有限状态转换器实现了用于全文检索的倒排索引,实现了用于存储数值数据和地理位置数据的树,以及用于分析的列存储。每个数据都被编入了索引。强大的弹性保障硬件故障。检测这些故障并确保集群和数据的安全性和可用性。 What —— Elasticsearch是什么? Elasticsearch是一个基于Lucene的搜索服务器,Elasticsearch也是使用Java编写的,它...
阅读 1705·2021-11-23 09:51
阅读 1242·2019-08-30 13:57
阅读 2343·2019-08-29 13:12
阅读 2082·2019-08-26 13:57
阅读 1268·2019-08-26 11:32
阅读 1049·2019-08-23 15:08
阅读 800·2019-08-23 14:42
阅读 3148·2019-08-23 11:41