• Lucene自定义规则范围查询


    0x00

      level: 0x04

    0x01

      code:

    package org.apache.lucene.demo.big.query;
    
    import java.io.IOException;
    import java.util.Comparator;
    
    import org.apache.lucene.index.FilteredTermsEnum;
    import org.apache.lucene.index.Terms;
    import org.apache.lucene.index.TermsEnum;
    import org.apache.lucene.search.MultiTermQuery;
    import org.apache.lucene.util.AttributeSource;
    import org.apache.lucene.util.BytesRef;
    
    public class IpRangeQuery extends MultiTermQuery {
    
        private String    minIp, maxIp;
    
        public IpRangeQuery(String field, String minIp, String maxIp) {
            super(field);
            this.minIp = minIp;
            this.maxIp = maxIp;
        }
    
        @Override
        protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOException {
            return new IpRangeTermsEnum(terms.iterator(null));
        }
    
        private final class IpRangeTermsEnum extends FilteredTermsEnum {
            private Comparator<String>    termComp    = new Comparator<String>() {
                                                                                            @Override
                                                                                            public int compare(String ip1, String ip2) {
                                                                                                if (ip1.equals(ip2))
                                                                                                    return 0;
                                                                                                return getIpLong(ip1) - getIpLong(ip2) > 0 ? 1 : -1;
                                                                                            }
                                                                                        };
    
            private long getIpLong(String ip) {
                long v = 0;
                int startIndex = 0;
                int postion = 0;
                int vBase = 1;
                String ipByte = "";
    
                for (int i = 0; i < 4; i++) {
    
                    if (i == 3) {
                        ipByte = ip.substring(startIndex, ip.length());
                    } else {
                        postion = ip.indexOf(".", startIndex);
                        ipByte = ip.substring(startIndex, postion);
                        startIndex = postion + 1;
                    }
    
                    switch (i) {
                    case 0:
                        vBase = 1 * 1000 * 1000 * 1000;
                        break;
                    case 1:
                        vBase = 1 * 1000 * 1000;
                        break;
                    case 2:
                        vBase = 1 * 1000;
                        break;
                    default:
                        vBase = 1;
                        break;
                    }
    
                    v += Long.valueOf(ipByte) * vBase;
                }
    
                return v;
            }
    
            public IpRangeTermsEnum(TermsEnum tenum) {
                super(tenum);
            }
    
            @Override
            protected final BytesRef nextSeekTerm(BytesRef term) {
                String termStr = term == null ? null : term.utf8ToString();
    
                if (termStr != null && termComp.compare(termStr, maxIp) > 0)
                    return null;
    
                if (termStr != null && termComp.compare(minIp, termStr) > 0)
                    return null;
    
                return term == null ? new BytesRef(minIp) : term;
            }
    
            @Override
            protected final AcceptStatus accept(BytesRef term) {
                String termStr = term.utf8ToString();
    
                if (termComp.compare(termStr, minIp) < 0)
                    return AcceptStatus.NO;
    
                if (termComp.compare(maxIp, termStr) < 0)
                    return AcceptStatus.NO;
    
                return AcceptStatus.YES;
    
            }
        }
    
        @Override
        public String toString(String field) {
            return this.field + ":[" + this.minIp + " TO " + this.maxIp + "]";
        }
    
    }

    0x02

      参考: TermRangeQuery 和 NumericRangeQuery

  • 相关阅读:
    深入浅出之正则表达式(一)
    Windows平台下构建 Android 开发环境( Android SDK 下载及安装教程)
    Eclipse 安装配置总结
    JDK安装配置教程
    关于磁力链接(Magnet URI)的简单介绍
    C# ASP.NET里的@妙用,字符串换行
    颜色中英文对照表 CSS
    使用git 工具下载android.jar Source Code
    ASP.NET表单提交之Get和Post的区别
    深入浅出之正则表达式(二)
  • 原文地址:https://www.cnblogs.com/feng2015/p/4313812.html
Copyright © 2020-2023  润新知