【问题标题】:MarkLogic Query By Example - underscore in JSON key not working?MarkLogic 查询示例 - JSON 键中的下划线不起作用?
【发布时间】:2015-02-04 13:48:42
【问题描述】:

我在 MarkLogic 中有以下 JSON 格式的条目:

{
   "identifier":"user1",
   "attributesList": [
      {
        "firstName": "James",
        "address_1": "Farcity"
      }
   ]
 }

如果,我将使用以下格式查询:

{
 $query:
 {
   "identifier":"user1",
   "attributesList": [
      {
        "firstName": "James"
      }
   ]
 }
}

这将匹配并返回计数为 1 的预期结果,因为“firstName”等于“James”。

但是,如果我执行以下操作:

{
 $query:
 {
   "identifier":"user1",
   "attributesList": [
      {
        "address_1": "Farcity"
      }
   ]
 }
}

即使“address_1”与“Farcity”完全匹配,它也不会返回任何结果。我已经在其他 JSON 键上尝试过这个,除了键中有下划线的键之外,它适用于所有键。这是保留字符吗?如果是这样,有没有办法避免这种情况,以便仍然可以匹配键“address_1”或“county_state”?

【问题讨论】:

  • 您使用的是什么版本的 MarkLogic?虽然目前还没有普遍可用,但我在 MarkLogic 8 中尝试了这个并且它工作正常(得到了 address_1 的结果)。这表明问题与 MarkLogic 6 和 7 中使用的 JSON 外观有关。
  • 我使用的是 7.0-2.3,但它无法正常工作。你可能是对的,因为我在 7.0-4 版本中尝试过这个,它工作正常..

标签: java marklogic


【解决方案1】:

看起来 Json 对象将下划线转换为双下划线

运行这个:

xquery version "1.0-ml";

import module namespace json="http://marklogic.com/xdmp/json"
 at "/MarkLogic/json/json.xqy";

let $j :=
 '{
   "identifier":"user1",
   "attributesList": [
      {
        "firstName": "James",
        "address_1": "Farcity"
      }
   ]
}'
return 
 json:transform-from-json( $j)

你会得到这个的

<json type="object" xmlns="http://marklogic.com/xdmp/json/basic">
  <identifier type="string">user1</identifier>
  <attributesList type="array">
    <json type="object">
      <firstName type="string">James</firstName>
      <address__1 type="string">Farcity</address__1>
    </json>
  </attributesList>
</json>

所以尝试使用双下划线进行查询。此外,如果您使用 Marklogic 6 或 7,它会将 Json 转换为 xml。因此您可以尝试使用 XML 格式通过示例进行查询。

【讨论】:

  • 使用双下划线查询仍然无效。对我来说,将格式从 JSON 更改为 XML 很困难,因为客户端就是这样使用它来传递 JSON QBE 的服务...
【解决方案2】:

有关用于将 JSON 字段名称映射到 QNames 的确切算法和函数,请参阅 http://docs.marklogic.com/xdmp:encode-for-NCName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2018-08-18
    • 1970-01-01
    • 2019-04-21
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多