You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中为MarkLogic数据库创建正确的通配符查询

解决MarkLogic中JSON数组字段的前缀匹配查询问题

问题背景

在Java项目中操作MarkLogic数据库,需求是筛选出JSON文档中icsList字符串列表字段里存在元素以指定字符串开头的文档。示例数据如下:

  • "icsList": ["11.040.40", "12.50.80"]
  • "icsList": ["12.50.60"]
  • "icsList": ["50.010.10"]

当前查询问题

  • 传入查询字符串"50"时,返回所有文档(包括不含"50"的)
  • 传入查询字符串"50."时,会匹配包含"50."的元素(如"12.50.60"),不符合开头匹配的要求
  • 希望无需在查询字符串末尾添加句点即可实现准确前缀匹配

解决方案

原代码使用word查询配合wildcarded选项时,MarkLogic的分词器会将.视为分隔符,导致匹配逻辑偏离预期。要实现数组元素的前缀精确匹配,需改用valueQuery结合通配符,同时确保字段的索引配置正确。

关键要点

  1. 配置字符串范围索引:MarkLogic需为icsList字段创建字符串范围索引,这是实现准确前缀匹配的前提。
  2. 替换查询方式:用StructuredQueryBuilder.value()替代word()方法,该方法会针对整个字符串进行通配符匹配,而非分词后的片段。

修改后的代码

StructuredQueryBuilder sqb = new StructuredQueryBuilder();
// 使用valueQuery实现整个字符串的前缀匹配,启用通配符支持
StructuredQueryDefinition queryDefinitionIcs = sqb.value(
    sqb.jsonProperty("icsList"),
    searchText + "*",
    "wildcarded"
);

StructuredQueryDefinition query = sqb.and(queryDefinitionIcs);
query.setCollections(DocumentCollection.ATTRIBUTES.getName());

try (DocumentPage search = jsonDocumentManager.search(query, 1L)) {
    JacksonHandle handle = new JacksonHandle();
    List<DocumentAttributes> documentAttributes = StreamSupport.stream(search.spliterator(), false)
            .map(v -> mapToDocumentAttributes(handle, v))
            .toList();
}
private DocumentAttributes mapToDocumentAttributes(JacksonHandle handle, DocumentRecord v) {
    try {
        var doc = objectMapper.treeToValue(v.getContent(handle).get(), DocumentAttributes.class);
        return doc;
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

补充说明

  • 若修改后查询仍不生效,需检查MarkLogic控制台的索引配置:
    进入Admin Console → 目标数据库 → Element Range Indexes,添加jsonProperty类型的索引,名称设为icsList,类型选择string并启用索引。
  • 传入"50"时,查询值变为"50*",会匹配所有以"50"开头的数组元素(如"50.010.10"),不会命中"12.50.60"这类中间包含"50"的元素。
  • 无需在查询字符串末尾添加句点,直接使用原始输入即可满足需求。

内容的提问来源于stack exchange,提问作者me_how

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:30:56