lucene查询索引之QueryParser解析查询——(八)

0.语法介绍:

 

 

 

1.公共部分代码同七中一样

 

// IndexReader IndexSearcher
    public IndexSearcher getIndexSearcher() throws Exception {
        // 第一步:创建一个Directory对象,也就是索引库存放的位置。
        Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁盘
        // 第二步:创建一个indexReader对象,需要指定Directory对象。
        IndexReader indexReader = DirectoryReader.open(directory);
        // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
        return new IndexSearcher(indexReader);
    }

    // 执行查询的结果
    public void printResult(IndexSearcher indexSearcher, Query query) throws Exception {
        // 第五步:执行查询。
        TopDocs topDocs = indexSearcher.search(query, 10);
        // 第六步:返回查询结果。遍历查询结果并输出。
        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("------------");
        }
    }

 

2.查询所有:(分析器会对查询条件进行分词)

   语法:   *:*

// 条件解释的对象查询
    @Test
    public void testQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        // 参数1: 默认查询的域
        // 参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("* : *");

        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

 

 

3.使用默认查询的域

  查询名字带有computer索引的文档

@Test
    public void testQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        // 参数1: 默认查询的域
        // 参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("computer");

        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

 

结果:

加载扩展词典:ext.dic
加载扩展停止词典:stopword.dic
computer.txt
��Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don��t need to leave home to borrow books from a library or to do shopping in a supermarke
336
E:\lucene&solr\searchfiles\computer.txt
------------

 

 

4.范围查询

  不支持范围查询

5.组合查询(组合查询只用修改语法,+表示必须,-表示必须没有,啥也没有表示可有可无)

  查询fileName必须带有Java,且必须不带struts的文档。

  语法  +fileName:java -fileName:struts

// 条件解释的对象查询
    @Test
    public void testQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        // 参数1: 默认查询的域
        // 参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("+fileName:java -fileName:struts");

        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

 

结果:

加载扩展词典:ext.dic
加载扩展停止词典:stopword.dic
java 基础.txt
 think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit
309
E:\lucene&solr\searchfiles\java 基础.txt
------------
java高级.txt
����java�߼��̳̣���������������
32
E:\lucene&solr\searchfiles\java高级.txt
------------

 

 

6.复杂查询

  查询 fileName 带有javaweb 或者 computer的,或者是fileContent带有javaweb 或者 computer的

  IKAnalyzer分析器先对java is apach进行分词,然后对javaweb is computer进行分词,然后根据条件进行查询带有分析器分词后的词的文档。
// 条件解释的对象查询
    @Test
    public void testQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        // 参数1: 默认查询的域
        // 参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("fileName:javaweb is computer OR fileContent:javaweb is computer ");

        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

 

 


结果:

加载扩展词典:ext.dic
加载扩展停止词典:stopword.dic
computer.txt
��Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don��t need to leave home to borrow books from a library or to do shopping in a supermarke
336
E:\lucene&solr\searchfiles\computer.txt
------------
1javaweb .txt
this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring 
53
E:\lucene&solr\searchfiles\1javaweb .txt
------------

 

 

 --------------------条件解析的对象查询 多个默念域-----------------------

7.解析器用 MultiFieldQueryParser

  查询 fileName 带有javaweb 或者 computer的,或者是fileContent带有javaweb 或者 computer的(与6的作用等价)

  作用不大,在查询条件中一用上查询域默认查询域就废了。

// 条件解析的对象查询 多个默念域
    @Test
    public void testMultiFieldQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        String[] fields = { "fileName", "fileContent" };
        // 参数1: 默认查询的域
        // 参数2:采用的分析器
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("javaweb is computer");

        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

 

 

 

 

总结:

  这个可以将查询条件设置为句子,IKAnalyzer分析器会对句子进行分词处理,然后进行查询索引。

  

 // 参数1: 默认查询的域
        // 参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
        // *:* 域:值
        Query query = queryParser.parse("fileName:java is apache OR fileContent:Computers are changing our life ");

 

posted @ 2017-08-05 21:58  QiaoZhi  阅读(749)  评论(0编辑  收藏  举报