MarkLogic - XDMP:节点替换未按预期显示结果

时间:2017-10-20 19:44:09

标签: xquery marklogic

我正在尝试创建一个用于摄取内容的端点。

当我收到JSON文档时,我试图将其提交给语义分类服务,获取标记,并将其添加到文档中。

现在我可以收到该文件,将其发送到分类,获得标签就好了。打印日志显示$tags填充了

2017-10-20 15:29:40.333 Info: App-Services: {"value":"Greenville", "score":"1.00", "cskey":"43236", "tag_source":"smartlogic", "key":"43236", "original_key":"0", "name":"Location", "id":"5b1c5c25-f532-4fa9-a5c6-65eab0972032"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Philadelphia", "score":"0.83", "cskey":"415938", "tag_source":"smartlogic", "key":"415938", "original_key":"0", "name":"Location", "id":"6cadfcd3-6a91-476d-a313-ed6f6fab7b3b"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Russia", "score":"0.99", "cskey":"226313", "tag_source":"smartlogic", "key":"226313", "original_key":"0", "name":"Location", "id":"7fc9223e-e922-422d-b3fc-ed97b531eed6"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"South Carolina", "score":"0.69", "cskey":"299308", "tag_source":"smartlogic", "key":"299308", "original_key":"0", "name":"Location", "id":"82610e94-2f6e-4f4b-adde-c05dd9f4491c"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Spartanburg", "score":"1.00", "cskey":"300651", "tag_source":"smartlogic", "key":"300651", "original_key":"0", "name":"Location", "id":"b32a9141-83be-49b9-91e2-afdcece7e180"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Pennsylvania", "score":"0.84", "cskey":"248736", "tag_source":"smartlogic", "key":"248736", "original_key":"0", "name":"Location", "id":"b6697012-bb0d-4b52-8a16-157fed1f1658"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"White House", "score":"0.48", "cskey":"243472", "tag_source":"smartlogic", "key":"243472", "original_key":"0", "name":"Location", "id":"d69f639d-01a7-4273-9a92-835f6ce6bf18"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Charleston", "score":"1.00", "cskey":"48402", "tag_source":"smartlogic", "key":"48402", "original_key":"0", "name":"Location", "id":"da4c7ff4-edb2-412b-84e7-00b87531ef10"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Detroit", "score":"0.58", "cskey":"247983", "tag_source":"smartlogic", "key":"247983", "original_key":"0", "name":"Location", "id":"e957c0f1-4307-47b4-9e64-4bf9df40ce23"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Chaka Fattah", "score":"0.51", "cskey":"2447795", "tag_source":"smartlogic", "key":"2447795", "original_key":"0", "name":"Person", "id":"09d4c57e-8581-4851-b059-fa6a2c139fa3"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Hillary Clinton", "score":"0.61", "cskey":"4186582", "tag_source":"smartlogic", "key":"4186582", "original_key":"0", "name":"Person", "id":"3a2c01e0-e0c8-4619-ac91-6c214084d92b"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Martin Luther King Jr.", "score":"0.48", "cskey":"2973808", "tag_source":"smartlogic", "key":"2973808", "original_key":"0", "name":"Person", "id":"632e2dd0-3599-44eb-b03c-824098102da9"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Mitt Romney", "score":"0.49", "cskey":"2089044", "tag_source":"smartlogic", "key":"2089044", "original_key":"0", "name":"Person", "id":"714d7f31-0d84-42c0-9ab8-691bc37efece"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Thomas Davis", "score":"0.49", "cskey":"1948480", "tag_source":"smartlogic", "key":"1948480", "original_key":"0", "name":"Person", "id":"ae4022ce-735a-4b05-a5e4-d8b400fd69a4"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Jonathan Allen", "score":"0.48", "cskey":"2511948", "tag_source":"smartlogic", "key":"2511948", "original_key":"0", "name":"Person", "id":"aefda552-f582-4951-a053-824f4ff8fe78"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Jonathan Capehart", "score":"0.48", "cskey":"4741366", "tag_source":"smartlogic", "key":"4741366", "original_key":"0", "name":"Person", "id":"be35ab01-fd3f-49a8-a119-a9f7c1a1d692"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Howard Dean", "score":"0.49", "cskey":"3887714", "tag_source":"smartlogic", "key":"3887714", "original_key":"0", "name":"Person", "id":"c8672836-ef35-452c-a049-10079cb790bf"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Thomas Davis", "score":"0.49", "cskey":"1731508", "tag_source":"smartlogic", "key":"1731508", "original_key":"0", "name":"Person", "id":"cb53685e-8261-4ad4-a3ab-6da348b026f3"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Gun politics", "score":"0.42", "cskey":"5509249", "tag_source":"smartlogic", "key":"5509249", "original_key":"0", "name":"Subject", "id":"03df7c0d-d07d-47d7-9b75-ac9c2df5970e"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Gun control", "score":"0.44", "cskey":"5242478", "tag_source":"smartlogic", "key":"5242478", "original_key":"0", "name":"Subject", "id":"5534bc22-b2e5-44cd-a37e-4280e2fea604"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Gun violence", "score":"0.48", "cskey":"5529178", "tag_source":"smartlogic", "key":"5529178", "original_key":"0", "name":"Subject", "id":"60214456-6ebc-4ea5-92eb-89274bf02d6b"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Climate", "score":"0.41", "cskey":"5345616", "tag_source":"smartlogic", "key":"5345616", "original_key":"0", "name":"Subject", "id":"65e8b0ac-f853-43d5-a92c-842947f3fdd3"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Shootings", "score":"0.52", "cskey":"5470240", "tag_source":"smartlogic", "key":"5470240", "original_key":"0", "name":"Subject", "id":"b593e4f0-cf7a-4234-8ce9-3beba31a16bb"}
2017-10-20 15:29:40.333 Info: App-Services: {"value":"Violence", "score":"0.42", "cskey":"5428634", "tag_source":"smartlogic", "key":"5428634", "original_key":"0", "name":"Subject", "id":"f8eaed62-64b1-4125-8878-4088f1aef13d"}

我想要更新的文件是JSON。我正在尝试替换tags的节点,该节点将 Null 空Array-node []

根据xdmp:node-replace的码头,我正在尝试执行以下行

let $_ := xdmp:node-replace($doc//headers/tags, array-node { $tags } )

这似乎不适用于期望结果(带标签的更新文档) 我在代码末尾发出xdmp:document-insert以确保我们插入更新后的版本。

当前查询控制台

let $doc_uri := '/adocument.json'
let $doc := fn:doc($doc_uri)

(: content used for submission :)
let $content := $doc//description
let $title := $doc//title
let $smart_uri := 'http://10.203.10.229:5058'
let $body := fn:concat($title,'
',$content )
let $xml_payload := 
    <request op="CLASSIFY">
        <document>
            <body type="TEXT"> {$body}</body>
            <path/>
            <multiarticle/>
            <language>Default</language>
            <threshold>1</threshold>
            <clustering type="RMS" threshold="1"/>
            <operation_mode>CAT</operation_mode>
            <feedback/>
            <use_generated_keys/>
        </document>
    </request>

let $test := xdmp:quote($xml_payload)
let $test2 :=  fn:encode-for-uri($test)
let $response := xdmp:http-get(fn:concat($smart_uri,'?XML_INPUT=',$test2))
(: Response has the tag information :)
(: parse the response to look like the values I want :)

let $tags := (
  for $tag in $response//response/STRUCTUREDDOCUMENT/META
    let $tag_map := map:map()

    let $_ := 
    (: this one should not be stored :)
  if ($tag/@name = 'Type') then 
        ()
      else
        let $_ := map:put($tag_map, 'name', $tag/@name)
        let $_ := map:put($tag_map, 'value', $tag/@value)
        let $_ := map:put($tag_map, 'id', $tag/@id)
        let $_ := map:put($tag_map, 'score', $tag/@score)
        let $_ := map:put($tag_map, 'key', $tag/@key)
        let $_ := map:put($tag_map, 'original_key', $tag/@original_key)
        let $_ := map:put($tag_map, 'cskey', $tag/@cskey)
        let $_ := map:put($tag_map, 'tag_source', 'smartlogic')

         return $_
    let $ret :=
          (: Only send back maps that are populated :)
      if (map:count($tag_map) > 0) then
         $tag_map
       else ()

    return $ret
)

(: some testing :)
let $doc_as_json := xdmp:to-json($doc)
let $test_map := map:map()
let $_ := map:put($test_map, 'tags', xdmp:to-json($tags))

let $_ := xdmp:log($tags)
let $_ := xdmp:node-replace($doc//headers/tags, array-node { $tags  }  )
let $_ := xdmp:log($doc)
(: return the updated $doc :)
return xdmp:to-json($doc)

在生产Scneario中,文档将通过 PUT 请求接收,并且不会已存在于数据库中

1 个答案:

答案 0 :(得分:3)

一些事情:1)xdmp:node-replace替换数据库节点,而不是内存节点。 2)通过从数据库或内存节点读取,在事务期间进行的数据库更新不可见。

听起来您可能正在使用xdmp:node-replace正确更新代码节点,但最后会使用xdmp:document-insert覆盖所有内容。如果没有看到更多代码,我无法确定,但请先尝试从查询中删除最终插入内容。