lucene查询索引之Query子类查询——(七)

0.文档名字:(根据名字索引查询文档)

1. 提取获取InsexSearch 与 处理结果的公共代码

//IndexReader  IndexSearcher
    public IndexSearcher getIndexSearcher() throws Exception{
        // 第一步:创建一个Directory对象,也就是索引库存放的位置。
        Directory directory = FSDirectory.open(new File("D:\\temp\\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.精准查询:(入门程序的查询索引)

   查询名字索引中含有Java的文件(解析语法:    fileName:java)

fileName:java// 精准查询
    @Test
    public void testTermQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。
        Query query = new TermQuery(new Term("fileName", "java"));
        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:

java struts - .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 struts - .txt
------------
java struts.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 struts.txt
------------
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
------------
java struts  springmvc.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 struts  springmvc.txt
------------
java struts spring.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 struts spring.txt
------------

 

 3.查询所有

   解析语法  *:*

// 查询所有
    @Test
    public void testMatchAllDocsQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        Query query = new MatchAllDocsQuery();
        System.out.println(query);
        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:

*:*
1javaweb .txt
this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring
53
E:\lucene&solr\searchfiles\1javaweb .txt
------------
2.javeSpring .txt
my family, because I have a happy family. My father is an English teacher. His name is Jacky. He is thirty-eight. He likes playing basketball. What??s my mother job? Is she a teacher? Yes, you??re right! My mother is very kind and nice, she is thirty-seven. My mother is always laborious work. I love my parents! On Saturday and Sunday, I often go to the library and play the piano, My father go to play basketball. Sometimes, we watch TV and listen to music at home. I love my family. Because I??m very happy to live
518
E:\lucene&solr\searchfiles\2.javeSpring .txt
------------
2.springMVC.txt
is is my room. Near the window there is a desk. I often do my homework at it. You can see some books, some flowers in a vase, a ruler and a pen. On the wall near the desk there is a picture of a cat. There is a clock above the end of my bed. I usually put my shoe under my bed. Of course
287
E:\lucene&solr\searchfiles\2.springMVC.txt
------------
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
------------
java struts  springmvc.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 struts  springmvc.txt
------------
java struts - .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 struts - .txt
------------
java struts spring.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 struts spring.txt
------------
java struts.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 struts.txt
------------
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
------------

4.根据范围值查询

  解析语法:    fileSize:{47 TO 200]

  NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true);

  根据范围查询,第一个是域名字,第二个是最小值,第三个参数是最大值,第四个参数是是否包含最小值,第五个参数是是否包含最大值。

  Long型后面要加L,不加L默认是int,int转为long是安全的,所以会自动转,能编译通过
  浮点数不加F默认是double类型,double转float可能损失精度,因为不会自动转,编译是通不过的

// 根据数值范围查询
    @Test
    public void testNumericRangeQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        Query query = NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true);
        System.out.println(query);
        printResult(indexSearcher, query);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:

fileSize:{47 TO 200]
1javaweb .txt
this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring
53
E:\lucene&solr\searchfiles\1javaweb .txt
------------

5.组合条件查询(重点)

(1)查找名字中java必有,struts可有可无的

  解析语法:     +fileName:java fileName:struts

// 可以组合查询条件
    @Test
    public void testBooleanQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        BooleanQuery booleanQuery = new BooleanQuery();

        Query query1 = new TermQuery(new Term("fileName", "java"));
        Query query2 = new TermQuery(new Term("fileName", "struts"));
        // select * from user where id =1 or name = ‘safdsa‘
        booleanQuery.add(query1, Occur.MUST);
        booleanQuery.add(query2, Occur.SHOULD);
        System.out.println(booleanQuery);
        printResult(indexSearcher, booleanQuery);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:

+fileName:java fileName:struts
java struts - .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 struts - .txt
------------
java struts.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 struts.txt
------------
java struts  springmvc.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 struts  springmvc.txt
------------
java struts spring.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 struts spring.txt
------------
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
------------

(2) 查找java与struts都必须有的

  解析语法  +fileName:java +fileName:struts

@Test
    public void testBooleanQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        BooleanQuery booleanQuery = new BooleanQuery();

        Query query1 = new TermQuery(new Term("fileName", "java"));
        Query query2 = new TermQuery(new Term("fileName", "struts"));
        // select * from user where id =1 or name = ‘safdsa‘
        booleanQuery.add(query1, Occur.MUST);
        booleanQuery.add(query2, Occur.MUST);
        System.out.println(booleanQuery);
        printResult(indexSearcher, booleanQuery);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果;(下面的+表示查询条件必须有要求的分词,可有可无不带+也不带-)

+fileName:java +fileName:struts
java struts - .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 struts - .txt
------------
java struts.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 struts.txt
------------
java struts  springmvc.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 struts  springmvc.txt
------------
java struts spring.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 struts spring.txt
------------

(3)查询必有java,必须没有struts的文档

  解析语法  +fileName:java -fileName:struts

@Test
    public void testBooleanQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        BooleanQuery booleanQuery = new BooleanQuery();

        Query query1 = new TermQuery(new Term("fileName", "java"));
        Query query2 = new TermQuery(new Term("fileName", "struts"));
        // select * from user where id =1 or name = ‘safdsa‘
        booleanQuery.add(query1, Occur.MUST);
        booleanQuery.add(query2, Occur.MUST_NOT);
        System.out.println(booleanQuery);
        printResult(indexSearcher, booleanQuery);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:(-表示查询条件中必须没有包含的分词)

+fileName:java -fileName:struts
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
------------

(4)查询必有java,必须没有struts的文档且大小必须在4400字节的文档

  解析语法   +fileName:java -fileName:struts +fileSize:{47 TO 400]

@Test
    public void testBooleanQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();

        BooleanQuery booleanQuery = new BooleanQuery();

        Query query1 = new TermQuery(new Term("fileName", "java"));
        Query query2 = new TermQuery(new Term("fileName", "struts"));
        Query query3 = NumericRangeQuery.newLongRange("fileSize", 47L, 400L, false, true);

        // select * from user where id =1 or name = ‘safdsa‘
        booleanQuery.add(query1, Occur.MUST);
        booleanQuery.add(query2, Occur.MUST_NOT);
        booleanQuery.add(query3, Occur.MUST);
        System.out.println(booleanQuery);
        printResult(indexSearcher, booleanQuery);
        // 关闭资源
        indexSearcher.getIndexReader().close();
    }

结果:

+fileName:java -fileName:struts +fileSize:{47 TO 400]
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
------------
时间: 2024-08-23 23:11:53

lucene查询索引之Query子类查询——(七)的相关文章

Lucene 查询(Query)子类

QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);// 查询字符串 Query query = parser.parse("key"); MultiFieldQueryParser(多域查询) MultiFieldQueryParser p

lucene 建立索引与查询

Lucene 简介 Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能.Lucene 目前是 Apache Jakarta 家族中的一个开源项目.也是目前最为流行的基于 Java 开源全文检索工具包. 目前已经有很多应用程序的搜索功能是基于 Lucene 的,比如 Eclipse 的帮助系统的搜索功能.Lucene 能够为文本类型的数据建立索引,所以你只要能把你要索引的数据格式转化的文本的,Lucene 就能对你的文档进行

lucene入门查询索引——(三)

1.用户接口(lucene不提供) 2.创建查询 3.执行查询 4.渲染结果: 5.过程分析 根据关键字查询索引库中的内容: 1)  创建IndexSearcher对象 2)  创建QueryParser对象 3)  创建Query对象来封装关键字 4)  用IndexSearcher对象去索引库中查询符合条件的前100条记录,不足100条记录的以实际为准 5)  获取符合条件的编号 6)  用indexSearcher对象去索引库中查询编号对应的Document对象 7)  将Document

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

0.语法介绍: 1.公共部分代码同七中一样 // IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception { // 第一步:创建一个Directory对象,也就是索引库存放的位置. Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁盘 // 第二步:创建一个ind

一步一步跟我学习lucene(18)---lucene索引时join和查询时join使用示例

了解sql的朋友都知道,我们在查询的时候可以采用join查询,即对有一定关联关系的对象进行联合查询来对多维的数据进行整理.这个联合查询的方式挺方便的,跟我们现实生活中的托人找关系类似,我们想要完成一件事,先找自己的熟人,然后通过熟人在一次找到其他,最终通过这种手段找到想要联系到的人.有点类似于"世间万物皆有联系"的感觉. lucene的join包提供了索引时join和查询时join的功能: Index-time join 大意是索引时join提供了查询时join的支持,且IndexWr

Lucene查询索引

索引创建 以新闻文档为例,每条新闻是一个document,新闻有news_id.news_title.news_source.news_url.news_abstract.news_keywords这6个域,添加两个news document到索引中,下面再贴一下创建索引的代码: package ucas.ir.lucene; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.An

[lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能

首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检索了). 在写代码之前,我们要先引入一下lucene包,就类似于C语言里的include.如图: 点击之后看到如下窗口,选择"Add External JARs" 然后找到C:\Lucene-6.2.1目录下(如果是按上一篇文章配置的话应该是在这个目录里)的三个包(这里我们暂时只用到这三个

Lucene实现索引和查询

0引言 随着万维网的发展和大数据时代的到来,每天都有大量的数字化信息在生产.存储.传递和转化,如何从大量的信息中以一定的方式找到满足自己需求的信息,使之有序化并加以利用成为一大难题.全文检索技术是现如今最普遍的信息查询应用,生活中利用搜索引擎,在博客论坛中查找信息,这些搜索的核心原理就是本文要实现的全文检索技术.随着文档信息数字化的实现,将信息有效存储并及时准确的提取是每一个公司.企业和单位要做好的基础.针对英文的全文检索已经有很多成熟的理论和方法,开放源代码的全文检索引擎Lucene 是Apa

lucene-5.1.0 索引的创建与查询 demo

lucene以及solr作为索引工具已经被广泛使用,以前项目中也有用到过lucene4.x,如今lucene版本已经到5.1了,再次了解一下,来写个demo! 首先附一下文档及下载地址: a:下载地址 Lucene下载 b:文档地址 lucene API 所需jar包(只附lucene相关jar): lucene-analyzers-common-5.1.0.jar lucene-core-5.1.0.jar lucene-queries-5.1.0.jar lucene-queryparser