ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Lucene全文检索

2020-01-13 20:05:04  阅读:270  来源: 互联网

标签:name Field jar Lucene 全文检索 new document 数据


1.2 数据库like查询和全文检索的区别
1.2.1 结构化数据和非结构化数据

数据库中存储的数据是结构化数据,即行数据java,可以用二维表结构来逻辑表达实现的数据。不方便用数据库二维逻辑表来表现的数据即称为非结构化数据,包括所有格式的办公文档、文本、图片、标准通用标记语言下的子集XML、HTML、各类报表、图像和音频/视频信息等等。
? 结构化数据:指具有固定格式或有限长度的数据,如数据库元数据等。
? 非结构化数据:指不定长或无固定格式的数据,如邮件,word文档等。
? 半结构数据:就是介于完全结构化数据(如关系型数据库、面向对象数据库中的数据)和完全无结构的数据(如声音、图像文件等)之间的数据,HTML、XML文档就属于半结构化数据,数据的结构和内容混在一起,没有明显的区分。

1.入门
导入jar包
lucene-core-7.4.0.jar
lucene-analyzers-common-7.4.0.jar
IK-Analyzer-1.0-SNAPSHOT.jar 中文分析器
commons-io-2.6.jar 读取文件的工具类
lucene-queryparser-7.4.0.jar queryParser需要的jar包
2.创建索引

   //选择索引库存放位置  FSDirectory 存储在硬盘中 RAMDirectory存储在内存中
        Directory directory = FSDirectory.open(new File("").toPath());
        //创建索引库配置 并且使用IKAnalyzer 中文分析器
        IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
        //创建索引库
        IndexWriter writer = new IndexWriter(directory,config);

        //读取文件信息
        File f = new File("");
        for (File file : f.listFiles()) {
            //文件路径
            String path = file.getPath();
            //文件名
            String name = file.getName();
            //文件大小
            long size = FileUtils.sizeOf(file);
            //读取文件里面的内容,utf-8编码
            String content = FileUtils.readFileToString(file, "utf-8");
            //将获取的信息放入域中
            Field fieldName = new TextField("name",name,Field.Store.YES);
            Field fieldPath = new TextField("path",path,Field.Store.YES);
            Field fieldSizeValue = new LongPoint("size",size);
            Field fieldSizeStore = new StoredField("size",size);
            Field fieldContent = new TextField("content",content,Field.Store.YES);
           //将域存入到document中
            Document document = new Document();
            document.add(fieldName);
            document.add(fieldPath);
            document.add(fieldSizeValue);
            document.add(fieldSizeStore);
            document.add(fieldContent);
        }
        //关闭流
        writer.close();

在这里插入图片描述
3.查询索引

  //读取索引库
        IndexReader reader = DirectoryReader.open(directory);
        //创建indexSearcher对象
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        /**
         * 三种查询方式
         * 1.通过term 关键字查询  TermQuery
         * 2.通过LongPoint进行范围查询
         * 3.通过QueryParser查询 
         */
        //Query query = new TermQuery(new Term("name","spring"));
        //Query query = LongPoint.newRangeQuery("size",0L,100L);
        QueryParser queryParser = new QueryParser("name",new IKAnalyzer());
        Query query = queryParser.parse("spring,");
        //第一个放query对象,第二个参数是查询多少个
        TopDocs topDocs = indexSearcher.search(query, 10);
        //topDocs.totalHits 总结果数
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            //获取document的id查询document对象
            Document document = reader.document(scoreDoc.doc);
            //查询对应域的值
            System.out.println(document.get("name"));
        }
        //关流
        reader.close();

默认分析器的使用,中文分析器IKAnalyzer

  //默认的分析器
        Analyzer analyzer = new StandardAnalyzer();
        TokenStream tokenStream = analyzer.tokenStream("name", "spring mybatis springmvc");
        //像TokenStream中设置一个引用,相当于指针
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            System.out.println(charTermAttribute);
        }
        analyzer.close();
骑着猪区见上帝 发布了23 篇原创文章 · 获赞 0 · 访问量 305 私信 关注

标签:name,Field,jar,Lucene,全文检索,new,document,数据
来源: https://blog.csdn.net/qq_40959641/article/details/103962931

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有