zoukankan      html  css  js  c++  java
  • LuceneTest


    /**
    * Created by mhm on 2019/6/24.
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    public class LuceneTest {

    @Test
    public void test1() throws IOException {
    /**
    * 1.准备数据 文档对象
    * Document 封装数据 要创建索引的数据必须先放着Document
    *
    * id 1
    * title 背影
    * author 朱自清
    * content 你站在这里不要动,我去给你买几个橘子
    *
    * Field 封装数据 实体类中一个属性数据封装在一个Field对象中
    * 八种基本类型 StringField 数据不会做分词处理
    * TextField 文本Field 数据会分词
    *
    * 参数1:name 属性名
    * 参数2:value 属性值
    * 参数3:Field.Store.YES
    */
    Document document = new Document();
    document.add(new IntField("id",1, Field.Store.YES));
    document.add(new TextField("title","背影",Field.Store.YES));
    document.add(new StringField("author","朱自清",Field.Store.YES));
    document.add(new TextField("content","你站在这里不要动,我去给你买几个橘子",Field.Store.YES));

    /**
    * 2.扫描数据 创建索引
    *
    * IndexWriter 索引写出对象
    * 作用
    * 1.将索引持久化 实际上就是流
    * 2.定义分词规则 分词器(别人封装好的分词规则)
    *
    * 参数1:Directory 指明索引保存的位置 这个文件夹的位置就是索引库
    * 参数2:IndexWriterConfig 索引写出配置对象 定义分词规则
    * IndexWriterConfig
    * 参数1:Lucene的版本
    * 参数2:分词器对象
    */

    FSDirectory fsDirectory = FSDirectory.open(new File("E://Lucene"));
    /**
    * StandardAnalyzer 标准分词器
    */
    StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_44);
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,standardAnalyzer);
    IndexWriter indexWriter = new IndexWriter(fsDirectory,indexWriterConfig);

    /**
    * 3.addDocument 把原始数据给写出对象
    * indexWriter 会把原始数据写出到索引库 在写出的同时indexWriter会自动的扫描原始数据创建索引
    */
    indexWriter.addDocument(document);
    /**
    * 4.将索引提交到索引库
    */
    indexWriter.commit();
    /**
    * 5.释放资源
    */
    indexWriter.close();
    }
    /**
    * 查询
    */
    @Test
    public void test2() throws ParseException, IOException {
    /**
    * 1. 准备关键词
    */
    String keyword = "你站在这里不要动";
    /**
    * 2. 处理关键词(拆分) Query对象(封装了拆分好的关键词)
    * MultiFieldQueryParser 关键词处理对象 可以将关键词处理为Query对象
    * 参数1:版本
    * 参数2:属性名的数组(被查询的属性)
    * 参数3:分词器对象 拆分词语 必须和创建的索引时候的分词器一致
    *
    * parse 方法处理关键词
    */
    String[] fileds = {"title","content"};
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44,fileds,new StandardAnalyzer(Version.LUCENE_44));
    Query query = queryParser.parse(keyword);
    /**
    * 3. 用处理好的关键词 查索引
    *
    * 把索引库读取到JVM里面(内存)
    * IndexSearcher 索引查询对象
    * 参数1:索引读取对象
    *
    * DirectoryReader.open() 读取索引库
    * 参数1:FSDirectory File System 文件系统 指的就是磁盘
    *
    */
    FSDirectory directory = FSDirectory.open(new File("E://Lucene"));
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
    /**
    * 4. 通过查到的索引中的位置信息 获取数据
    *
    * search()
    * 参数1:处理好的关键词
    * 参数2:期望结果条数 你想要查到最多多少条信息
    *
    * 返回值 TopDocs 封装了结果的信息(索引 分数等待)
    *
    */
    TopDocs topDocs = indexSearcher.search(query, 10);
    /**
    * ScoreDoc 分数和文档对象的数组
    * 封装了分数和Document的docId(Lucene在创建索引的时候自动生成的 就是位置信息)
    *
    * Lucene在创建索引库的时候,不仅创建了索引,还把原始数据Document对象也放在了索引库中
    */
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    System.out.println(Arrays.toString(scoreDocs));
    /**
    * 5.通过位置信息docId去索引库中获取原始数据
    */
    for (int i = 0; i < scoreDocs.length; i++) {
    ScoreDoc scoreDoc = scoreDocs[i];
    // 获取docId
    int docId = scoreDoc.doc;
    // 通过docId获取索引库中对应的Document对象(对象中封装了原始数据)
    Document document = indexSearcher.doc(docId);
    System.out.println(document);
    String id = document.get("id");
    String title = document.get("title");
    String author = document.get("author");
    String content = document.get("content");
    System.out.println(id+" "+title+" "+author+" "+content);
    }
    }
    }
  • 相关阅读:
    蚂蚁的难题(一) http://acm.nyist.net/JudgeOnline/status.php?pid=744
    快速查找素数 http://acm.nyist.net/JudgeOnline/problem.php?pid=187
    我排第几个 http://acm.nyist.net/JudgeOnline/problem.php?pid=139
    求余数 http://acm.nyist.net/JudgeOnline/problem.php?pid=205
    九的余数 http://acm.nyist.net/JudgeOnline/problem.php?pid=105
    次方求模 http://acm.nyist.net/JudgeOnline/problem.php?pid=102
    汉诺塔(一)http://acm.nyist.net/JudgeOnline/problem.php?pid=88
    换博客了,欢迎访问
    linux下使用kpartx挂载虚拟文件系统
    编译linux内核时出错
  • 原文地址:https://www.cnblogs.com/mikemhm/p/11079794.html
Copyright © 2011-2022 走看看