8、索引库的查询二之:数值类型索引查询

IntPoint查询
整型的查询方式:这种查询的方式,是将设置域中的值表明为整型,通过代码进行分析说明

创建整形索引

Document document = new Document();
Field intPoint = new IntPoint("age", 15);
document.add(intPoint);

域名为 age 值为15
查询的有以下几种方式:
1、匹配精确
//整型精确数值查询 这里要注意的是,创建索引时,也必须是IntPoint 类型的
@Test
public void testIntPoint() throws Exception {
    //以读的方式打开索引库
    Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
    //创建一个IndexReader
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建一个IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建一个查询对象
    //参数:1、要搜索的域 整型值
    Query query = IntPoint.newExactQuery("age", 10);
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        //取document对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("age"));
    }
    indexSearcher.getIndexReader().close();
}
2、范围匹配(开区间或闭区间)
//整型范围数值查询
@Test
public void testIntPoint2() throws Exception {
    //以读的方式打开索引库
    Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
    //创建一个IndexReader
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建一个IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建一个查询对象
    //参数:1、要搜索的域 2、整型值闭区间 3、整型值闭区间
    /**
     * 注: 如何能做到 左开又闭 或者是右开又闭 或者是 全开的区间呢,其实很简单,我们将整型的这两个参数 以Math.addExact 的方式替换即可
     * IntPoint.newRangeQuery("age", Math.addExact(25, 1), Math.addExact(40, -1));  表示为 (25,40)之前的整数,不包含 25和40.
     * 可能会出现的异常  IllegalArgumentException - if field is null.  ifeld是空的
     */
    Query query = IntPoint.newRangeQuery("age", 25,40);
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        //取document对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("age"));
    }
    indexSearcher.getIndexReader().close();
}
//整型范围数组值查询
@Test
public void testIntPoint3() throws Exception {
    //以读的方式打开索引库
    Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
    //创建一个IndexReader
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建一个IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建一个查询对象
    //参数:1、要搜索的域 2、整型值闭区间 3、整型值闭区间
    /**
     *int[] is1={1,2,5,80};
     *int[] is2={5,55,98,32};
     *通过设置lowerValue [i] = Integer.MIN_VALUE或upperValue [i] = Integer.MAX_VALUE,可以具有半开范围(实际上是</≤或> /≥查询)。
     */
    int[] is1={0,1,4,5};
    int[] is2={10,30,40,80};
    Query query = IntPoint.newRangeQuery("ages", is1,is2);
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        //取document对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("age"));
    }
    indexSearcher.getIndexReader().close();
}
3、集合匹配 一值或多值
//整型setQuery 的查询方式 创建与任何指定的值匹配的查询。这是等于TermsQuery的方式 精确匹配。

@Test
public void testIntPoint4() throws Exception {
    //以读的方式打开索引库
    Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
    //创建一个IndexReader
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建一个IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建一个查询对象
    //参数:1、要搜索的域 2、整型值闭区间
    Query query = IntPoint.newSetQuery("age", 120);
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        //取document对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("age"));
    }
    indexSearcher.getIndexReader().close();
}
//整型setQuery 的查询方式 集合查询的方式
@Test
public void testIntPoint5() throws Exception {
    //以读的方式打开索引库
    Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
    //创建一个IndexReader
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建一个IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建一个查询对象
    //参数:1、要搜索的域 2、整型值闭区间
    List list=new ArrayList();
    list.add(11);
    list.add(120);
    Query query = IntPoint.newSetQuery("age", list);//注:该功能是将集合中的所有元素进行去索引的匹配
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        //取document对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("age"));
    }
    indexSearcher.getIndexReader().close();
}
将IntPoint的查询索引方式说明完成之后,下面的三种查询也是大同小异的,区别在与 创建索引时用到的参数类型 和查询时所用的到的参数类型 保持一致
LongPoint查询
FloatPoint查询
DoublePoint查询
简单的说明一下用法,将上面的例子 中的IntPoint换成是LongPoint、FloatPoint、DoublePoint即可。创建索引时要注意值类型的一致性。查询时 也是相同的方式

下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的

----请看下方↓↓↓↓↓↓↓

百度搜索 Drools从入门到精通:可下载开源全套Drools教程

深度Drools教程不段更新中:


更多Drools实战陆续发布中………

扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值