全文检索Lucene (1)

简介: Lucene是apache开源的一个全文检索框架,很是出名。今天先来分享一个类似于HelloWorld级别的使用。工作流程依赖我们要想使用Lucene,那就得先引用人家的jar包了。

Lucene是apache开源的一个全文检索框架,很是出名。今天先来分享一个类似于HelloWorld级别的使用。


工作流程

Lucene工作流程

依赖

我们要想使用Lucene,那就得先引用人家的jar包了。下面列举一下我使用到的jars.

  • lucene-analyzers-common-6.1.0.jar : 分析器支持
  • lucene-core-6.1.0.jar : 全文检索核心支持
  • lucene-highlighter-6.1.0.jar : 检索到的目标词的高亮显示
  • lucene-memory-6.1.0.jar : 索引存储相关的支持
  • lucene-queries-6.1.0.jar : 查询支持
  • lucene-queryparser-6.1.0.jar : 查询器支持

Lucene HelloWorld

下面就着手实现一个级别为HelloWorld的小例子。实现一个基于文章内容的查询。

Article.java

/**
 * @Date 2016年8月1日
 *
 * @author Administrator
 */
package domain;

/**
 * @author 郭瑞彪
 *
 */
public class Article {

    private Integer id;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Article [id=" + id + ", title=" + title + ", content=" + content + "]";
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

创建索引库

@Test
    public void createIndex() throws Exception {

        // 模拟一条文章数据
        Article a = new Article();
        a.setId(1);
        a.setTitle("全文检索");
        a.setContent("我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索");

        // 建立索引
        Directory dir = FSDirectory.open(Paths.get("./indexDir/"));
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        IndexWriter indexWriter = new IndexWriter(dir, indexWriterConfig);

        Document doc = new Document();
        doc.add(new StringField("id", a.getId().toString(), Field.Store.YES));
        doc.add(new TextField("title", a.getTitle(), Field.Store.YES));
        doc.add(new TextField("content", a.getContent(), Field.Store.YES));

        indexWriter.addDocument(doc);
        indexWriter.close();
    }

从索引库中获取查询结果

@Test
    public void search() throws Exception {

        String queryString = "资源";
        Analyzer analyzer = new StandardAnalyzer();
        analyzer.setVersion(Version.LUCENE_6_1_0);
        QueryParser queryParser = new QueryParser("content", analyzer);
        Query query = queryParser.parse(queryString);
        // IndexReader indexReader =
        // DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
        DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
        IndexReader indexReader = directoryReader;
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

        List<Article> articles = new ArrayList<Article>();
        for (int i = 0; i < scoreDocs.length; i++) {
            ScoreDoc scoreDoc = scoreDocs[i];
            Document doc = indexSearcher.doc(scoreDoc.doc);
            Article a = new Article();
            a.setId(Integer.parseInt(doc.get("id")));
            a.setTitle(doc.get("title"));
            a.setContent(doc.get("content"));
            System.out.println(a.toString());
            articles.add(a);
        }
        // 显示结果
        System.out.println("总的记录数为: " + topDocs.totalHits);
        System.out.println(articles.toString());
        for (Article a : articles) {
            System.out.println("-----------搜索结果如下-----------------");
            System.out.println(">>>id: " + a.getId());
            System.out.println(">>>title:" + a.getTitle());
            System.out.println(">>>content:" + a.getContent());
        }
        indexReader.close();
        analyzer.close();

    }

查询结果

总的记录数为: 4

-----------搜索结果如下-----------------
>>>id: 1
>>>title:全文检索
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索
-----------搜索结果如下-----------------
>>>id: 2
>>>title:全文检索2
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索,hahahahahhaha

总结

Lucene全文检索的功能可以这么简单的实现,但是里面有更多的用法等着我们去挖掘。

目录
相关文章
|
6月前
|
数据采集 存储 Java
02Lucene实现全文检索的流程
02Lucene实现全文检索的流程
17 0
|
SQL 存储 搜索推荐
什么是全文检索
全文检索技术被广泛的应用于搜索引擎,查询检索等领域。我们在网络上的大部分搜索服务都用到了全文检索技术。 对于数据量大、数据结构不固定的数据可采用全文检索方式搜索,比如百度、Google等搜索引擎、论坛站内搜索、电商网站站内搜索等。
538 0
什么是全文检索
|
SQL 数据采集 自然语言处理
Lucene就是这么简单(一)
Lucene是apache软件基金会发布的一个开放源代码的全文检索引擎工具包,由资深全文检索专家Doug Cutting所撰写,它是一个全文检索引擎的架构,提供了完整的创建索引和查询索引,以及部分文本分析的引擎,Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎,Lucene在全文检索领域是一个经典的祖先,现在很多检索引擎都是在其基础上创建的,思想是相通的。
133 0
Lucene就是这么简单(一)
|
SQL 自然语言处理 算法
Lucene就是这么简单(三)
Lucene是apache软件基金会发布的一个开放源代码的全文检索引擎工具包,由资深全文检索专家Doug Cutting所撰写,它是一个全文检索引擎的架构,提供了完整的创建索引和查询索引,以及部分文本分析的引擎,Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎,Lucene在全文检索领域是一个经典的祖先,现在很多检索引擎都是在其基础上创建的,思想是相通的。
117 0
Lucene就是这么简单(三)
|
存储 自然语言处理 数据库
5分钟了解lucene全文索引
本文通俗地介绍了Lucene全文检索的内容及工作原理,以及索引的结构,旨在让以前未了解过Lucene的读者在能在短时间内对Lucene有简单认知,未介绍具体代码,读完本文可知道Lucene是什么,有哪些具体应用,我们一直说的索引是什么。
|
SQL 自然语言处理 算法
Lucene就是这么简单
什么是Lucene?? Lucene是apache软件基金会发布的一个开放源代码的全文检索引擎工具包,由资深全文检索专家Doug Cutting所撰写,它是一个全文检索引擎的架构,提供了完整的创建索引和查询索引,以及部分文本分析的引擎,Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎,Lucene在全文检索领域是一个经典的祖先,现在很多检索引擎都是在其基础上创建的,思想是相通的。
1233 0
|
存储 自然语言处理 程序员
|
Java Apache 索引