资讯专栏INFORMATION COLUMN

lucene简单入门

Ververica / 2624人阅读

摘要:序说是界的检索之王,当之无愧。近年来的火爆登场,包括之前的及,其底层都是。简单了解,对使用还是有点帮助的。是当前最流行的开源大数据内存计算框架,采用语言实现,由伯克利大学实验室开发并于年开源。

说lucene是Java界的检索之王,当之无愧。近年来elasticsearch的火爆登场,包括之前的solr及solr cloud,其底层都是lucene。简单了解lucene,对使用elasticsearch还是有点帮助的。本文就简单过一下其简单的api使用。

添加依赖

</>复制代码

  1. org.apache.lucene
  2. lucene-core
  3. 4.6.1
  4. org.apache.lucene
  5. lucene-analyzers-common
  6. 4.6.1
  7. org.apache.lucene
  8. lucene-queryparser
  9. 4.6.1
  10. org.apache.lucene
  11. lucene-codecs
  12. 4.6.1
索引与检索 创建索引

</>复制代码

  1. File indexDir = new File(this.getClass().getClassLoader().getResource("").getFile());
  2. @Test
  3. public void createIndex() throws IOException {
  4. // Directory index = new RAMDirectory();
  5. Directory index = FSDirectory.open(indexDir);
  6. // 0. Specify the analyzer for tokenizing text.
  7. // The same analyzer should be used for indexing and searching
  8. StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
  9. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
  10. // 1. create the index
  11. IndexWriter w = new IndexWriter(index, config);
  12. addDoc(w, "Lucene in Action", "193398817");
  13. addDoc(w, "Lucene for Dummies", "55320055Z");
  14. addDoc(w, "Managing Gigabytes", "55063554A");
  15. addDoc(w, "The Art of Computer Science", "9900333X");
  16. w.close();
  17. }
  18. private void addDoc(IndexWriter w, String title, String isbn) throws IOException {
  19. Document doc = new Document();
  20. doc.add(new TextField("title", title, Field.Store.YES));
  21. // use a string field for isbn because we don"t want it tokenized
  22. doc.add(new StringField("isbn", isbn, Field.Store.YES));
  23. w.addDocument(doc);
  24. }
检索

</>复制代码

  1. @Test
  2. public void search() throws IOException {
  3. // 2. query
  4. String querystr = "lucene";
  5. // the "title" arg specifies the default field to use
  6. // when no field is explicitly specified in the query.
  7. Query q = null;
  8. try {
  9. StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
  10. q = new QueryParser(Version.LUCENE_46,"title", analyzer).parse(querystr);
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. // 3. search
  15. int hitsPerPage = 10;
  16. Directory index = FSDirectory.open(indexDir);
  17. IndexReader reader = DirectoryReader.open(index);
  18. IndexSearcher searcher = new IndexSearcher(reader);
  19. TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
  20. searcher.search(q, collector);
  21. ScoreDoc[] hits = collector.topDocs().scoreDocs;
  22. // 4. display results
  23. System.out.println("Found " + hits.length + " hits.");
  24. for (int i = 0; i < hits.length; ++i) {
  25. int docId = hits[i].doc;
  26. Document d = searcher.doc(docId);
  27. System.out.println((i + 1) + ". " + d.get("isbn") + "
  28. " + d.get("title"));
  29. }
  30. // reader can only be closed when there
  31. // is no need to access the documents any more.
  32. reader.close();
  33. }
分词

对于搜索来说,分词出现在两个地方,一个是对用户输入的关键词进行分词,另一个是在索引文档时对文档内容的分词。两个分词最好一样,这样才可以更好地匹配出来。

</>复制代码

  1. @Test
  2. public void cutWords() throws IOException {
  3. // StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
  4. // CJKAnalyzer analyzer = new CJKAnalyzer(Version.LUCENE_46);
  5. SimpleAnalyzer analyzer = new SimpleAnalyzer();
  6. String text = "Spark是当前最流行的开源大数据内存计算框架,采用Scala语言实现,由UC伯克利大学AMPLab实验室开发并于2010年开源。";
  7. TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
  8. CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
  9. try {
  10. tokenStream.reset();
  11. while (tokenStream.incrementToken()) {
  12. System.out.println(charTermAttribute.toString());
  13. }
  14. tokenStream.end();
  15. } finally {
  16. tokenStream.close();
  17. analyzer.close();
  18. }
  19. }

输出

</>复制代码

  1. spark
  2. 当前
  3. 流行
  4. 开源
  5. 大数
  6. 内存
  7. 计算
  8. 框架
  9. 采用
  10. scala
  11. 语言
  12. 实现
  13. uc
  14. 伯克利
  15. 大学
  16. amplab
  17. 实验室
  18. 开发
  19. 并于
  20. 2010
  21. 开源

</>复制代码

  1. 本工程github

参考

lucenetutorial

helloLucene

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65464.html

相关文章

  • Lucene系列(一)快速入门

    摘要:系列文章系列一快速入门系列二使用及索引文档的基本操作系列三查询及高亮是什么在维基百科的定义是一套用于全文检索和搜索的开放源代码程序库,由软件基金会支持和提供。全面准确和快速是衡量全文检索系统的关键指标。结果列表有相关度排序。 系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 Lucene是什么? Luc...

    骞讳护 评论0 收藏0
  • Lucene系列(二)luke使用及索引文档的基本操作

    摘要:系列文章系列一快速入门系列二使用及索引文档的基本操作系列三查询及高亮入门简介地址下载地址是一个用于搜索引擎的,方便开发和诊断的可视化工具。使用作为其最低级别的搜索引擎基础。截止,上述代码所用的包皆为最新。 系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 luke入门 简介: github地址:http...

    hedzr 评论0 收藏0
  • Elasticsearch Lucene 数据写入原理 | ES 核心篇

    摘要:因为倒排索引打分机制全文检索原理分词原理等等,这些都是不会过时的技术。中,单个倒排索引文件称为。其中有一个文件,记录了所有的信息,称为文档新写入时,会生成新的。过程上个过程中在文件系统缓存中,会有意外故障文档丢失。写入次怕后,清空。 前言 最近 TL 分享了下 《Elasticsearch基础整理》,蹭着这个机会。写个小文巩固下,本文主要讲 ES -> Lucene的底层结构,然后详细...

    wums 评论0 收藏0
  • Spring Boot 2.x(十七):快速入门Elastic Search

    摘要:极速的查询速度通过有限状态转换器实现了用于全文检索的倒排索引,实现了用于存储数值数据和地理位置数据的树,以及用于分析的列存储。每个数据都被编入了索引。强大的弹性保障硬件故障。检测这些故障并确保集群和数据的安全性和可用性。 What —— Elasticsearch是什么? Elasticsearch是一个基于Lucene的搜索服务器,Elasticsearch也是使用Java编写的,它...

    yangrd 评论0 收藏0

发表评论

0条评论

Ververica

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<