zoukankan      html  css  js  c++  java
  • 打通es及lucene应用,lucene应用es Query,适配6.8.0方案至7.10.2

            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.10.2</version>
                <scope>provided</scope>
            </dependency>
    

    直接升级pom依赖打包,出错

    需要适配,思路和6.8.0一致,过程就不放了,只放区别部分

    Screen Shot 2021-03-05 at 10.27.42 PM

    相比6.8.0

    ServiceHolder 的构造方法多了参数boolean registerType

            ServiceHolder(Settings nodeSettings,
                          Settings indexSettings,
                          Collection<Class<? extends Plugin>> plugins,
                          long nowInMillis,
                          AbstractBuilderTestCase testCase,
                          boolean registerType)
    

    设registerType为false 先跳过

    Screen Shot 2021-03-05 at 10.48.03 PM

    测试文本类型正常,但非文本类的range出错

    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
    
    java.lang.IllegalStateException: Rewrite first
    
    	at org.elasticsearch.index.query.RangeQueryBuilder.doToQuery(RangeQueryBuilder.java:494)
    	at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:103)
    	at com.github.cclient.elasticsearch.format.EsToLuceneTest.rangeQuery(EsToLuceneTest.java:35)
    

    对比6.8.0和7.10.2的RangeQueryBuilder类

    6.8.0

    protected Query doToQuery(QueryShardContext context) throws IOException {
            if (this.from == null && this.to == null) {
                FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldType)context.getMapperService().fullName("_field_names");
                if (fieldNamesFieldType == null) {
                    return new MatchNoDocsQuery("No mappings yet");
                }
    
                if (fieldNamesFieldType.isEnabled()) {
                    return ExistsQueryBuilder.newFilter(context, this.fieldName);
                }
            }
    
            Query query = null;
            MappedFieldType mapper = context.fieldMapper(this.fieldName);
            if (mapper != null) {
                DateMathParser forcedDateParser = this.getForceDateParser();
                query = mapper.rangeQuery(this.from, this.to, this.includeLower, this.includeUpper, this.relation, this.timeZone, forcedDateParser, context);
            } else if (this.timeZone != null) {
                throw new QueryShardException(context, "[range] time_zone can not be applied to non unmapped field [" + this.fieldName + "]", new Object[0]);
            }
    
            if (query == null) {
                query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(this.from), BytesRefs.toBytesRef(this.to), this.includeLower, this.includeUpper);
            }
    
            return (Query)query;
        }
    

    7.10.2

        protected Query doToQuery(QueryShardContext context) throws IOException {
            if (this.from == null && this.to == null) {
                FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldType)context.getMapperService().fieldType("_field_names");
                if (fieldNamesFieldType == null) {
                    return new MatchNoDocsQuery("No mappings yet");
                }
    
                if (fieldNamesFieldType.isEnabled()) {
                    return ExistsQueryBuilder.newFilter(context, this.fieldName, false);
                }
            }
    
            MappedFieldType mapper = context.fieldMapper(this.fieldName);
            if (mapper == null) {
                throw new IllegalStateException("Rewrite first");
            } else {
                DateMathParser forcedDateParser = this.getForceDateParser();
                return mapper.rangeQuery(this.from, this.to, this.includeLower, this.includeUpper, this.relation, this.timeZone, forcedDateParser, context);
            }
        }
    

    因为方案没有mapping存在,因此MappedFieldType mapper = context.fieldMapper(this.fieldName); mapper都为null

    6.8.0 始使mapper为null ,依然会构造query

    if (query == null) {
                query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(this.from), BytesRefs.toBytesRef(this.to), this.includeLower, this.includeUpper);
            }
    

    7.10.2 mapper 直接报错了

      if (mapper == null) {
                throw new IllegalStateException("Rewrite first");
            }
    

    只更改RangeQueryBuilder方法简单,但要变更所有和非文本类就太折腾了,而且可能有隐患

    因此 按6.8.0的方案升级7.10.2只能做到对文本字段的查询处理,非文本类型,目前放弃

    对7.10.2的非文本类字段,只有向QueryShardContext context 补全mapping信息一种方法了,待解决

  • 相关阅读:
    如何用redis/memcache做Mysql缓存层?
    孤儿进程和僵尸进程总结
    二叉树的遍历(非递归)
    Linux进程分配内存的两种方式--brk() 和mmap()
    Hbase
    cgroup 分析之CPU和内存部分
    十道海量数据处理面试题与十个方法大总结
    快速定位性能瓶颈,检查出所有资源(CPU、内存、磁盘IO等)的利用率(utilization)、饱和度(saturation)和错误(error)度量,即USE方法
    红黑树
    tcp 两个重要窗口:滑动窗口 和 拥塞窗口
  • 原文地址:https://www.cnblogs.com/zihunqingxin/p/14488961.html
Copyright © 2011-2022 走看看