zoukankan      html  css  js  c++  java
  • lucene&solr学习——创建和查询索引(代码篇)

    1. Lucene的下载

    Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压。

    官网:http://lucene.apache.org/

    版本:lucene7.7.0 (学习上没必要最新的,因为企业中也不会升级太快)

    Jdk要求:1.7以上

    2.使用的jar包

    核心包

    其他:

     3. 创建索引库

    (1) 实现步骤 (程序的编写步骤与之前分析的理论步骤是颠倒过来的)

    第一步:创建java工程,并导入jar包

    第二步:创建一个indexwriter对象(创建索引)

      1.指定索引库的存放位置Directory对象

      2.指定一个分析器,对文档内容进行分析

    第三步:创建document对象 (构建文档对象)

    第四步:创建field对象,将field添加到document

    第五步:使用indexwriter对象将document对象写到索引库,此过程进行索引创建。并将索引和document对象写入索引库。

    第六步:关闭IndexWriter对象

    (2) Field域的属性

    是否分析:是否对域的内容进行分词处理。前提是我们要对域的内容进行查询。

    是否索引:将Field分析后的词或整个Field值进行索引,只有索引方可搜索到。

    比如:商品名称,商品简介分析后进行索引,订单号,身份证号不用分析但也要索引,这些将来都要作为查询条件

    是否存储:将Field值存储在文档中,存储在文档中的Field才可以从Document中获取。

    比如:商品名称,订单号,凡是将来要从Document中获取的Field都要存储

    是否存储的标准:是否将内容展示给用户

    测试代码:

    将下面的文件,创建成索引

     代码:

    public class FirstLucene {
        
        @Test
        public void textIndex() throws Exception {
    //        第一步:创建java工程,并导入jar包
    //        第二步:创建一个indexwriter对象(创建索引)
    //          1.指定索引库的存放位置Directory对象
            Directory directory = FSDirectory.open(Paths.get("E:\temp\index"));  //文件系统目录 file system directory
    //          2.指定一个分析器,对文档内容进行分析
            Analyzer analyzer = new StandardAnalyzer();//官方推荐分词器
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
            File f = new File("E:\searchSource");
            File[] listFiles = f.listFiles();
            for (File file : listFiles) {
    //            第三步:创建document对象 (构建文档对象)
                Document document = new Document();
    //            第四步:创建field对象,将field添加到document
                //文件名称
                String file_name = file.getName();
                Field fieldNameField = new TextField("fileName", file_name, Store.YES);
                //文件大小
                long file_size = FileUtils.sizeOf(file);
                Field fileSizeField = new LongPoint("fileSize", file_size);
                Field fileSizeFieldStore = new StoredField("fileSize", file_size);
                //文件路径
                String file_path = file.getPath();
                Field filePathField = new StoredField("filePath", file_path);
                //文件内容
                String file_content = FileUtils.readFileToString(file);
                Field fileContentField = new TextField("fileContent", file_content, Store.NO);
            
                document.add(fieldNameField);
                document.add(fileSizeField);
                document.add(fileSizeFieldStore);
                document.add(filePathField);
                document.add(fileContentField);
                
    //            第五步:使用indexwriter对象将document对象写到索引库,此过程进行索引创建。并将索引和document对象写入索引库。
                indexWriter.addDocument(document);
            }
    //        第六步:关闭IndexWriter对象
            indexWriter.close();
            
        }
    }

    结果:

    4.查询索引

    (1) 实现步骤:

      第一步:创建一个Directory对象,也就是索引库存放的位置

      第二步:创建一个indexReader对象,需要制定Directory对象

      第三步:创建一个indexsearcher对象,需要指定IndexReader对象

      第四步:创建一个TermQuery对象,制定查询的域和查询的关键词

      第五步:执行查询。

      第六步:返回查询结果,便利查询结果并输出

      第七步:关闭IndexReader对象。

    (2) IndexSearcher搜索方法

    代码:

    @Test
        public void testSearch() throws Exception {
    //        第一步:创建一个Directory对象,也就是索引库存放的位置
            Directory directory = FSDirectory.open(Paths.get("E:\temp\index"));
    //        第二步:创建一个indexReader对象,需要指定Directory对象
            IndexReader indexReader =DirectoryReader.open(directory);
    //        第三步:创建一个indexsearcher对象,需要指定IndexReader对象
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //        第四步:创建一个TermQuery对象,制定查询的域和查询的关键词
            Query query = new TermQuery(new Term("fileName", "spring"));
    //        第五步:执行查询。
            TopDocs topDocs = indexSearcher.search(query, 2);
    //        第六步:返回查询结果,遍历查询结果并输出
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            for (ScoreDoc scoreDoc : scoreDocs) {
                int doc = scoreDoc.doc;
                Document document = indexSearcher.doc(doc);
                //文件名称
                String fileName = document.get("fileName");
                System.out.println(fileName);
                //文件内容
                String fileContent = document.get("fileContent");
                System.out.println(fileContent);
                //文件路径
                String fileSize = document.get("fileSize");
                System.out.println(fileSize);
                //文件大小
                String filePath = document.get("filePath");
                System.out.println(filePath);
                System.out.println("---------");
            }
    //        第七步:关闭IndexReader对象。
            indexReader.close();
        }

    结果:

  • 相关阅读:
    C++预备知识
    C++求最小公倍数
    c++编写函数,递归删除字符串中的子串,求删除次数
    ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails
    django快速搭建blog
    北邮 北理 人大经验
    C、C++、Java语言中异常处理机制浅析
    学习git部署
    各种符号的使用情况说明以及区别
    【转】通过fio工具,测试SATA,SAS,SSD 读写性能
  • 原文地址:https://www.cnblogs.com/FanJava/p/10383558.html
Copyright © 2011-2022 走看看