zoukankan      html  css  js  c++  java
  • Lucene查询索引

    Query子类:
               1.TermQuery:根据域和关键词进行搜索

    private IndexSearcher indexSearcher;
        private IndexReader indexReader;
        @Before
        public void getSearcher() throws IOException {
            //1.创建Directory对象,指定索引库位置
            Directory directory = FSDirectory.open(new File("E:\Y2学习\Lucene\索引库\Index").toPath());
            //2.创建IndexReader对象,读取索引库内容
            indexReader= DirectoryReader.open(directory);
            //3.创建IndexSearcher对象
            indexSearcher=new IndexSearcher(indexReader);
        }
        /**
         * termQuery根据域和关键词进行搜索
         * @throws IOException
         */
        @Test
        public void termQuery() throws IOException {
            //创建查询条件
            Query query=new TermQuery(new Term("fieldName","new"));
            //执行查询
            TopDocs topDocs = indexSearcher.search(query, 10);
            System.out.println("返回的文档个数:"+topDocs.totalHits);
    
            //获取到文档集合
            ScoreDoc [] scoreDocs=topDocs.scoreDocs;
            for (ScoreDoc doc:scoreDocs) {
                //获取到文档
                Document document = indexSearcher.doc(doc.doc);
                //获取到文档域中数据
                System.out.println("fieldName:"+document.get("fieldName"));
                System.out.println("fieldPath:"+document.get("fieldPath"));
                System.out.println("fieldSize:"+document.get("fieldSize"));
                System.out.println("fieldContent:"+document.get("fieldContent"));
                System.out.println("==============================================================");
            }
            //关闭
            indexReader.close();
        }

    2.RangeQuery:范围搜索

    前提:创建文档时保存范围
                例:

     document.add(new LongPoint("fieldSize",456));
     document.add(new StoredField("fieldSize",456));
    /**
                     * RangeQuery范围搜素
                     * @throws IOException
                     */
                    @Test
                    public void RangeQuery() throws IOException {
                        //设置范围搜索的条件 参数一范围所在的域
                        Query query=LongPoint.newRangeQuery("fieldSize",0,50);
                        //查询
                        TopDocs topDocs = indexSearcher.search(query, 10);
                        System.out.println("返回的文档个数:"+topDocs.totalHits);
    
                        //获取到文档集合
                        ScoreDoc [] scoreDocs=topDocs.scoreDocs;
                        for (ScoreDoc doc:scoreDocs) {
                            //获取到文档
                            Document document = indexSearcher.doc(doc.doc);
                            //获取到文档域中数据
                            System.out.println("fieldName:"+document.get("fieldName"));
                            System.out.println("fieldPath:"+document.get("fieldPath"));
                            System.out.println("fieldSize:"+document.get("fieldSize"));
                            System.out.println("fieldContent:"+document.get("fieldContent"));
                            System.out.println("==============================================================");
                        }
    
                        //关闭
                        indexReader.close();
                    }

    3.QueryParser:匹配一行数据,这一行数据会自动进行分词

    例如查询:Lucene是一个开源的基于Java的搜索库  Lucene    一个   开源   基于......

    会先将搜索内容分词

    再根据分词查询
                使用方案:

        1.导入依赖,如果当前没有QueryParser依赖则需要导入依赖

        

    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
                        <dependency>
                            <groupId>org.apache.lucene</groupId>
                            <artifactId>lucene-queryparser</artifactId>
                            <version>7.4.0</version>
                        </dependency>

        2.测试编码:

    /**
         * queryparser搜素,会将搜索条件分词
         * @throws IOException
         */
        @Test
        public void queryparser() throws IOException, ParseException {
            //创建一个QueryParser对象 参数一:查询的域  参数二:使用哪种分析器
            QueryParser parser=new QueryParser("fieldContent",new IKAnalyzer());
            //设置匹配的数据条件
            Query query = parser.parse("Lucene是一个开源的基于Java的搜索库");
            //查询
            TopDocs topDocs = indexSearcher.search(query, 10);
            System.out.println("返回的文档个数:"+topDocs.totalHits);
    
            //获取到文档集合
            ScoreDoc [] scoreDocs=topDocs.scoreDocs;
            for (ScoreDoc doc:scoreDocs) {
                //获取到文档
                Document document = indexSearcher.doc(doc.doc);
                //获取到文档域中数据
                System.out.println("fieldName:"+document.get("fieldName"));
                System.out.println("fieldPath:"+document.get("fieldPath"));
                System.out.println("fieldSize:"+document.get("fieldSize"));
                System.out.println("fieldContent:"+document.get("fieldContent"));
                System.out.println("==============================================================");
            }
    
    
            //关闭
            indexReader.close();
        }
    }

  • 相关阅读:
    JObject提取Json字符串中某字段的值
    将DataTable导出为Excel文件的方法
    剑指offer-面试题39-数组中出现次数超过一半的数字-快速排序
    剑指offer-拓展训练-N皇后的问题-全排列
    剑指offer-拓展训练-字符的所有组合-全组合
    剑指offer-面试题38-字符串的排列-全排列
    剑指offer-面试题36-二叉搜索树与双向链表-中序遍历
    java多线程技能-使用多线程-继承Thread类
    剑指offer-面试题35-复杂链表的复制-链表
    剑指offer-面试题34-二叉树中和为某一值的路径-二叉树遍历
  • 原文地址:https://www.cnblogs.com/chx9832/p/12363449.html
Copyright © 2011-2022 走看看