首页 > 解决方案 > Lucene StandardQueryParser 错误地解析日期字符串

问题描述

我创建了一个 StandardQueryParser 来解析我的过滤器查询以进行搜索。解析器完美解析数值范围查询,搜索提供预期结果。

但是如果我想查询一个 DateTime 字符串,解析器会返回一个意外的值,除了年份之外的所有内容。示例:date:["2021-06-04T18:00:00Z" TO "2021-06-04T19:00:00Z"] -> date:[2021 TO 2021]其中第一个是客户端上的查询,第二个是查询解析器解析查询后的查询。
预期的查询将被date:[1622822400 TO 1622826000]转换为正确的纪元时间戳。

对于 PointsConfigMap 的配置,我使用了以下代码迭代模式并提取数字类型。

private void buildQueryParser(SolrCore core) {
    standardQueryParser = new StandardQueryParser(contextFilterQueryAnalyzer);
    Map<String, PointsConfig> pointsConfigMap = new HashMap<>();
    for(var entry : core.getLatestSchema().getFields().entrySet()) {
        var type = entry.getValue().getType();
        if (type.isPointField()) {
            PointsConfig pointsConfig = extractPointsConfig(type);
            if (pointsConfig != null) {
                pointsConfigMap.put(entry.getKey(), pointsConfig);
            }
        }
    }
    standardQueryParser.setPointsConfigMap(pointsConfigMap);
}

private PointsConfig extractPointsConfig(FieldType type) {
    switch (type.getNumberType()) {
        case DATE:
        case LONG:
            return new PointsConfig(new DecimalFormat(), Long.class);
        case INTEGER:
            return new PointsConfig(new DecimalFormat(), Integer.class);
        case FLOAT:
            return new PointsConfig(new DecimalFormat(), Float.class);
        case DOUBLE:
            return new PointsConfig(new DecimalFormat(), Double.class);
    }
    return null;
}

contextFilterQueryAnalyzer初始化为:new TokenizerChain(new KeywordTokenizerFactory(Collections.EMPTY_MAP), null)

如何正确配置 StandardQueryParser 以将 DateTime 字符串解析为时间戳?

标签: javasolrlucene

解决方案


推荐阅读