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

来源:互联网 发布:无人机数据 编辑:程序博客网 时间:2024/05/16 04:59
IntPoint查询
整型的查询方式:这种查询的方式,是将设置域中的值表明为整型,通过代码进行分析说明

创建整形索引

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

域名为 age 值为15
查询的有以下几种方式:
1、匹配精确
//整型精确数值查询 这里要注意的是,创建索引时,也必须是IntPoint 类型的
@Testpublic 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、范围匹配(开区间或闭区间)
//整型范围数值查询
@Testpublic 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();}
//整型范围数组值查询
@Testpublic 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的方式 精确匹配。

@Testpublic 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 的查询方式 集合查询的方式
@Testpublic 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即可。创建索引时要注意值类型的一致性。查询时 也是相同的方式
.

0 0
原创粉丝点击