zoukankan      html  css  js  c++  java
  • 搜索引擎学习(七)解析查询

    QueryParser【解析查询】

    定义:QueryParser的解析查询是用Lucene语法进行查询,而上一篇说到的子类查则是使用java对象来进行查询条件的封装。

    使用方式:使用QueryParser也可以创建Query,QueryParser提供了一个parse方法,此方法可以直接根据查询语法来查询。

    代码展示:

        /**
         * 条件解析对象查询
         * PS:lucene里面不支持对于字符串类型的范围查询,但是solr支持~
         *
         * 各种条件语句解析:
         * fileName:java【精准查询】
         * fileSize:{1 TO 200}【范围查询】
         * +fileName:java fileName:lunece【组合查询】
         * PS:组合查询不同的条件空格隔开,其中域前面的“+”号代表“条件必须满足”,“-”号代表“条件必须不满足”,没有符号代表“条件可选”,
         * PS:也可以用 AND、OR、NOT来表示上面的符号(详细解析见总结的图)
         * @throws Exception
         */
        @Test
        public void testQueryParser() throws Exception {
            //获取IndexSearcher(索引搜索)对象
            IndexSearcher indexSearcher = getIndexSearcher();
            //参数1:默认查询的域
            //参数2:采用的分析器
            QueryParser queryParser = new QueryParser("fileName", new StandardAnalyzer());
            //构建lucene查询条件的语法(第一个*是域,第二个*是域值)
            Query query = queryParser.parse("fileName:java");//*:*   域名:域值
            //打印查询的语法
            System.out.println(query);
            //执行查询并打印结果
            printResult(query, indexSearcher);
        }

    MultiFieldQueryParser【指定多个默认搜索域的解析查询】

    定义:在QueryParser的解析查询基础上扩展,支持多个默认域的查询。

     代码展示:

        /**
         * 多默认域解析查询
         * @throws Exception
         */
        @Test
        public void testMultiFieldQueryParser() throws Exception {
            //获取IndexSearcher(索引搜索)对象
            IndexSearcher indexSearcher = getIndexSearcher();
    
    
            //参数1:设置多个默认查询的域
            //参数2:采用的分析器
            String[] fieldNames = {"fileName", "fileContent"};//指定默认查询的多个域
            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fieldNames, new StandardAnalyzer());
            //构建lucene查询条件的语法(第一个*是域,第二个*是域值)
            Query query = queryParser.parse("java");//*:*   域名:域值
    
    
            //打印查询的语法
            System.out.println(query);
            //执行查询并打印结果
            printResult(query, indexSearcher);
        }
  • 相关阅读:
    mysql access denied for user root Mysql用户无权限
    远程链接调用sql脚本
    CuteEditor使用详解
    如何设置release模式
    ShardingJDBC不分库,只分表例子
    SpringCloud Stream整合RocketMQ实现消息发送与接收
    Spring Cloud Gateway的PrefixPath及StripPrefix功能
    使用MongoDB的Spring Boot和MongoTemplate教程
    ShardingJDBC读写分离案例
    SpringBoot那些好用的连接池HikariCP
  • 原文地址:https://www.cnblogs.com/riches/p/11479072.html
Copyright © 2011-2022 走看看