Lucene 6.5.0 入门Demo

摘要:
Lucene6.5.0要求jdk1.81.目录结构;2.数据库环境;privateintid;privateStringname;privatefloatprice;privateStringpic;privateStringdescription3.Lucene是Apache的一个全文检索引擎工具包,它不能独立运行,不能单独对外提供服务。/***Createdbyon2017/4/25.*/pu

Lucene 6.5.0 要求jdk 1.8

1.目录结构;

Lucene 6.5.0 入门Demo第1张

2.数据库环境;

Lucene 6.5.0 入门Demo第2张

private int id;
private String name;
private float price;
private String pic;
private String description

3.

LuceneApache的一个全文检索引擎工具包,它不能独立运行不能单独对外提供服务

Lucene 6.5.0 入门Demo第3张

/**
 * Created by  on 2017/4/25.
 */
public class IndexManager {
    @Test
    public void createIndex() throws Exception {
        // 采集数据
        BookDao dao = new BookDaoImpl();
        List<Book> list = dao.queryBooks();

        // 将采集到的数据封装到Document对象中
        List<Document> docList = new ArrayList<Document>();
        Document document;
        for (Book book : list) {
            document = new Document();
            // store:如果是yes,则说明存储到文档域中
            // 图书ID
            // Field id = new TextField("id", book.getId().toString(), Store.YES);

            Field id = new TextField("id", Integer.toString(book.getId()), Field.Store.YES);
            // 图书名称
            Field name = new TextField("name", book.getName(), Field.Store.YES);
            // 图书价格
            Field price = new TextField("price", Float.toString(book.getPrice()), Field.Store.YES);
            // 图书图片地址
            Field pic = new TextField("pic", book.getPic(), Field.Store.YES);
            // 图书描述
            Field description = new TextField("description", book.getDescription(), Field.Store.YES);

            // 将field域设置到Document对象中
            document.add(id);
            document.add(name);
            document.add(price);
            document.add(pic);
            document.add(description);

            docList.add(document);
        }
        //JDK 1.7以后 open只能接收Path/////////////////////////////////////////////////////

        // 创建分词器,标准分词器
        Analyzer analyzer = new StandardAnalyzer();

        // 创建IndexWriter
        // IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_6_5_0,analyzer);
        IndexWriterConfig cfg = new IndexWriterConfig(analyzer);

        // 指定索引库的地址
//         File indexFile = new File("D:\LaEclipse\lecencedemo\");
//         Directory directory = FSDirectory.open(indexFile);
        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\Lpj\JetBrains\lucenceIndex1\"));

        IndexWriter writer = new IndexWriter(directory, cfg);
      writer.deleteAll(); //清除以前的index
        // 通过IndexWriter对象将Document写入到索引库中
        for (Document doc : docList) {
            writer.addDocument(doc);
        }

        // 关闭writer
        writer.close();
    }

}
/**
 * Created by  on 2017/4/25.
 */
public class IndexSearch {
    private void doSearch(Query query) {
        // 创建IndexSearcher
        // 指定索引库的地址
        try {
//			File indexFile = new File("D:\Lpj\Eclipse\lecencedemo\");
//			Directory directory = FSDirectory.open(indexFile);
            // 1、创建Directory
            //JDK 1.7以后 open只能接收Path
            Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\Lpj\JetBrains\lucenceIndex1\"));
            IndexReader reader = DirectoryReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            // 通过searcher来搜索索引库
            // 第二个参数:指定需要显示的顶部记录的N条
            TopDocs topDocs = searcher.search(query, 10);

            // 根据查询条件匹配出的记录总数
            int count = topDocs.totalHits;
            System.out.println("匹配出的记录总数:" + count);
            // 根据查询条件匹配出的记录
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;

            for (ScoreDoc scoreDoc : scoreDocs) {
                // 获取文档的ID
                int docId = scoreDoc.doc;

                // 通过ID获取文档
                Document doc = searcher.doc(docId);
                System.out.println("商品ID:" + doc.get("id"));
                System.out.println("商品名称:" + doc.get("name"));
                System.out.println("商品价格:" + doc.get("price"));
                System.out.println("商品图片地址:" + doc.get("pic"));
                System.out.println("==========================");
                // System.out.println("商品描述:" + doc.get("description"));
            }
            // 关闭资源
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void indexSearch() throws Exception {
        // 创建query对象
        Analyzer analyzer = new StandardAnalyzer();
        // 使用QueryParser搜索时,需要指定分词器,搜索时的分词器要和索引时的分词器一致
        // 第一个参数:默认搜索的域的名称
        QueryParser parser = new QueryParser("description", analyzer);

        // 通过queryparser来创建query对象
        // 参数:输入的lucene的查询语句(关键字一定要大写)
        Query query = parser.parse("description:java AND lucene");

        doSearch(query);

    }

Lucene 6.5.0 入门Demo第4张

免责声明:文章转载自《Lucene 6.5.0 入门Demo》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python Selenium+phantomjs 小技巧黄聪:google搜索代替wordpress的搜索功能下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用TinyPNG提供的API,对图片进行压缩(C#)

项目需要,经常需要手动压缩图片,流程太过麻烦,效率低下。所以写了一个小程序,以提高工作效率 using System; using System.Net; using System.Text; using System.IO; classProgram { staticvoidMain() { Console.WriteLine("请输入TinyPn...

Oracle 11g数据库创建表空间、用户、目录、以及给用户授权

以下操作皆是Windows下操作: 一、创建表空间 创建表空间,是管理员级别的操作,当你装了一个数据库,这个数据库的表空间可以不止一个,可以根据实际情况来对应的增加、创建 打开命令提示符的窗口:可以直接Windows键+R,也可以如下图所示打开,用管理员身份登录sqlplus ,登录成功之后就可以输入以下命令: create tablespace MYSP...

Android Studio- 把项目提交到SVN中操作方法

第一步 下载SVN,下载完成之后,需要吧command line client tools点击修改安装 然后Crash Reporter点击选择取消安装 如果不进行该操作,则可能在C:Program FilesTortoiseSVNin找不到svn.exe执行文件。 注意如果 你不选择 全部,比如第二条的common line client to...

用python做一个搜索引擎(Pylucene)

什么是搜索引擎? 搜索引擎是“对网络信息资源进行搜集整理并提供信息查询服务的系统,包括信息搜集、信息整理和用户查询三部分”。如图1是搜索引擎的一般结构,信息搜集模块从网络采集信息到网络信息库之中(一般使用爬虫);然后信息整理模块对采集的信息进行分词、去停用词、赋权重等操作后建立索引表(一般是倒排索引)构成索引库;最后用户查询模块就可以识别用户的检索需求并提...

lucene.net 详解

转自:http://www.360doc.com/content/09/0216/17/32573_2562131.shtml 1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应用产品。因此它并不像www.baidu.com 或者google Desktop那么拿来就能用,它只是提供了一种工具让你能实现这些产品。1.2 ...

Java实现文件夹下文件实时监控

一、commons-io方法 1、使用Commons-io的monitor下的相关类可以处理对文件进行监控,它采用的是观察者模式来实现的 (1)可以监控文件夹的创建、删除和修改 (2)可以监控文件的创建、删除和修改 (3)采用的是观察者模式来实现的 (4)采用线程去定时去刷新检测文件的变化情况 2、引入commons-io包,需要2.0以上。 ?...