打通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信息一种方法了,待解决

原文地址:https://www.cnblogs.com/zihunqingxin/p/14488961.html