zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Lucene5学习之NumericRangeQuery使用

学习 使用
2023-09-14 08:59:37 时间

  说到NumericRangeQuery查询,你们肯定觉得很简单,不就是数字范围查询吗?用户提供一个上限值和一个下限值,底层API里直接 min, max,真的是这样吗?其实在Lucene里只能对字符串String建立索引,那么数字怎么转成String,你肯定又会想当然的认为toString()一下就OK啦?OK,假如真的是这样的,那字符串"3" "26"问题怎么解决?OK,可以通过在数字前面加前导零解决,“03” "26"是没错,可是前导零加几位没法确定,加多了浪费硬盘空间,加少了支持索引的数字位数受限。即使你解决了位数受限问题,但Lucene里的范围查询本质还是通过BooleanQuery进行条件连接起来的,term条件太多还是会出现too many boolean Clause异常的。其实Lucene内部是把数字(int,long,float,double)转成十六进制的数字来处理的。具体怎么转成的请参看NumericUtils这个工具类的源码,

 


   * Converts a  code float /code  value to a sortable signed  code int /code .     * The value is converted by getting their IEEE 754 floating-point "float format"     * bit layout and then some bits are swapped, to be able to compare the result as int.     * By this the precision is not reduced, but the value can easily used as an int.     * @see #sortableIntToFloat     */     public static int floatToSortableInt(float val) {       int f = Float.floatToRawIntBits(val);       if (f 0) f ^= 0x7fffffff;       return f;     }  

 上面贴的就是把float转成十六进制的数字的代码,里面尽是位运算,看的人晕晕的,要完全搞懂,不是一件容易的事情。

 

   为了减少BooleanQuery条件太多的问题,采用了Trie树结构来存储Term,这又涉及到Trie树算法,又是一道坎儿,不懂算法,内部实现又看不懂,心塞塞啊!

 


/** This helper does the splitting for both 32 and 64 bit. */     private static void splitRange(       final Object builder, final int valSize,       final int precisionStep, long minBound, long maxBound     ) {       if (precisionStep   1)         throw new IllegalArgumentException("precisionStep must be  =1");       if (minBound   maxBound) return;       for (int shift=0; ; shift += precisionStep) {         // calculate new bounds for inner precision         final long diff = 1L   (shift+precisionStep),           mask = ((1L precisionStep) - 1L)   shift;         final boolean           hasLower = (minBound   mask) != 0L,           hasUpper = (maxBound   mask) != mask;         final long           nextMinBound = (hasLower ? (minBound + diff) : minBound)   ~mask,           nextMaxBound = (hasUpper ? (maxBound - diff) : maxBound)   ~mask;         final boolean           lowerWrapped = nextMinBound   minBound,           upperWrapped = nextMaxBound   maxBound;                  if (shift+precisionStep =valSize || nextMinBound nextMaxBound || lowerWrapped || upperWrapped) {           // We are in the lowest precision or the next precision is not available.           addRange(builder, valSize, minBound, maxBound, shift);           // exit the split recursion loop           break;         }                  if (hasLower)           addRange(builder, valSize, minBound, minBound | mask, shift);         if (hasUpper)           addRange(builder, valSize, maxBound   ~mask, maxBound, shift);                  // recurse to next precision         minBound = nextMinBound;         maxBound = nextMaxBound;       }     }  

 说实话,我还没有完全参透这段源码,留着以后有空研究算法的时候再来啃这块骨头吧。

 

   上面说了一大堆废话,都是涉及底层数字范围查询设计原理的东西,只说了个大概,具体实现涉及的算法和原理我也还没参透,表示很抱歉,如果你对这方面算法很了解,麻烦请告知我,谢谢!

    NumericRangeQuery原理理解起来很难,但使用起来却是非常简单: 

 


 

    不过要注意的是NumericRangeQuery只对IntField,LongField,FloatField,DoubleField等这些表示数字的Field域有效,NumericRangeQuery还有一个比较重要的设置就是Precision Step,何为Precision Step呢?翻译过来就是精度步长,还是不够直观无法理解,对不对?说通俗一点就是拿多大一个长度来截取Term,因为你的数字转成十六进制的字符串后,可能很长,需要按照一定的步长截取成多个Term进行索引的,比如“1111101111111011”,如果你的Precision Step值为16的话(不同数据类型的步长默认值不同,都定义在NumericUtils工具类里),那最终只有1个term,如果Precision Step值为8,那最终索引中就会有2个Term,这就是为什么官方API里说percisionStep值越小会越占硬盘空间但搜索速度越快了。Term多了肯定越占硬盘空间了。 NumericRangeQuery就说到这儿了,Thanks all.

    如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,

或者加裙
一起交流学习!

转载:http://iamyida.iteye.com/blog/2194799