[lucene] lucene的HelloWorld程序

一.全文检索
1.1全文检索的定义:全文检索,即全文搜索,是对文本数据进行索引搜索。
1.2全文检索的特点:做了索引;对关键字做了高亮显示;摘要截取;搜索效果更加准确;只关注文本,不关注语意。
1.3使用场景:替换数据库的模糊查询,提高查询效率;全文检索是搜索引擎的基础;垂直搜索;在word,pdf等格式的内容
    中检索内容;用在各种输入法中。
二.全文检索的核心
2.1创建索引:建立单词与句子之间的对应关系,以便通过单词搜索到对应句子编号。
            分词-->语法处理-->排序--.>去重
2.2搜索索引:通过关键字到索引中搜多,找到对应句子的编号。
            输入搜索关键字-->关键字分词-->搜索得到具体的编号-->通过编号获取句子-->封装成对象传到前台展示

三.lucene入门
3.1.lucene是什么:lucene是全文检索的一种实现,是用java写的一种工具包。
3.2.lucene的核心API: 
    增删改:IndexWriter(索引写入器)
    查: IndexSearcher(索引搜索器)
3.3入门步骤:
    1)下载Lucene
    2)导入jar包
    3)测试:
        创建索引:
            创建IndexWriter;
            把要创建索引的文本数据放入Document的字段中;
            通过IndexWriter把document进行写入
        搜索索引:
            创建IndexSearcher;
            创建Query对象--把特定格式字符串解析得到
            使用IndexSearcher传入Query进行搜索
            从结果中获取documentId,再通过它获取document
            把document转换为我们想要的对象进行返回

四.创建索引的代码实现:

package practice;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.nio.file.Paths;

/**
 * lucene创建索引
 *
 * @author he
 * @date 2018/9/20
 */
public class Writer {
    private static final String PATH = "H:/JAVAEE/ideaCode/lucene/src/main/resources/index";


    public static void main(String[] args) throws Exception {
        String doc1 = "hello world";
        String doc2 = "hello java world";
        String doc3 = "hello lucene world";

        // 创建IndexWriter
        Directory d = FSDirectory.open(Paths.get(PATH));
        IndexWriterConfig conf = new IndexWriterConfig(new SimpleAnalyzer());
        IndexWriter indexWriter = new IndexWriter(d, conf);

        // 把要创建的索引的文本数据放入Document中
        Document ducument1 = new Document();
        ducument1.add(new TextField("id", "1", Field.Store.YES));
        ducument1.add(new TextField("title", "doc1", Field.Store.YES));
        ducument1.add(new TextField("content", doc1, Field.Store.YES));
        Document ducument2 = new Document();
        ducument2.add(new TextField("id", "2", Field.Store.YES));
        ducument2.add(new TextField("title", "doc2", Field.Store.YES));
        ducument2.add(new TextField("content", doc2, Field.Store.YES));
        Document ducument3 = new Document();
        ducument3.add(new TextField("id", "3", Field.Store.YES));
        ducument3.add(new TextField("title", "doc3", Field.Store.YES));
        ducument3.add(new TextField("content", doc3, Field.Store.YES));

        // 通过IndexWriter把Document写入
        indexWriter.addDocument(ducument1);
        indexWriter.addDocument(ducument2);
        indexWriter.addDocument(ducument3);

        indexWriter.commit();
        indexWriter.close();
    }
}

代码结果:

d538e371c9c5f1920e61873fdc8452ae8ec.jpg

五.查询的代码实现:

package practice;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.nio.file.Paths;

/**
 * lucene搜索索引
 *
 * @author he
 * @date 2018/9/20
 */
public class Searcher {
    private static final String PATH = "H:/JAVAEE/ideaCode/lucene/src/main/resources/index";

    public static void main(String[] args) throws Exception {
        // 创建IndexSearcher
        Directory directory = FSDirectory.open(Paths.get(PATH));
        IndexReader r = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(r);

        String parStr = "content:java";

        // 创建搜索解析器
        String defaultField = "content";
        Analyzer analyzer = new SimpleAnalyzer();
        QueryParser queryParser = new QueryParser(defaultField, analyzer);

        // 解析搜索
        Query query = queryParser.parse(parStr);
        TopDocs topDocs = indexSearcher.search(query, 10000);
        System.out.println("总命中数: " + topDocs.totalHits);

        //读取搜索到的内容
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println("id -> " + document.get("id"));
            System.out.println("title -> " + document.get("title"));
            System.out.println("content -> " + document.get("content"));
        }
    }
}

代码结果:

7cbd702ef9618a5687d526576cb91c5b77b.jpg

转载于:https://my.oschina.net/u/3782515/blog/2086026

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值