cts:search 和 search:search 在处理 json 文档时
cts:search and search:search while dealing with json docs
我的 json 文档如下所示:
{
"directions": ["Heat oil in heavy... "],
"rating": 5,
"title": "Mahi-Mahi in Tomato Olive Sauce",
"ingredients": [
"2 tablespoons extra-virgin olive oil",
"1 cup chopped onion",
"1 cup dry white wine",
"1 teaspoon anchovy paste",
],
"sodium": null
}
当我运行:
cts:search(fn:doc(),"anchovy")/title/string()
我得到:Mahi-Mahi in Tomato Olive Sauce
,这是需要的。
但是当我 运行:
search:search("anchovy", $options)/search:result/title/string()
我得到一个空序列。注意:我设置了 transform-results = "raw".
P.S 我观察到 search:search("anchovy", $options)/search:result
给出的文档似乎是文本格式而不是 json。
在这种情况下使用search:search
是否可以得到想要的结果?
这应该可以完成工作:
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
declare variable $options := <options xmlns="http://marklogic.com/appservices/search"><transform-results apply="raw" /><extract-metadata>
<json-property>title</json-property>
</extract-metadata></options>;
search:search("anchovy", $options)//title/text()
此处我们指定要从文档中提取并放入结果集中的 JSON 属性。
作为旁注,您可能需要查看使用 ServerSide JavaScript if you're working a lot with JSON documents, and in that case you could also utilise jsearch。
我的 json 文档如下所示:
{
"directions": ["Heat oil in heavy... "],
"rating": 5,
"title": "Mahi-Mahi in Tomato Olive Sauce",
"ingredients": [
"2 tablespoons extra-virgin olive oil",
"1 cup chopped onion",
"1 cup dry white wine",
"1 teaspoon anchovy paste",
],
"sodium": null
}
当我运行:
cts:search(fn:doc(),"anchovy")/title/string()
我得到:Mahi-Mahi in Tomato Olive Sauce
,这是需要的。
但是当我 运行:
search:search("anchovy", $options)/search:result/title/string()
我得到一个空序列。注意:我设置了 transform-results = "raw".
P.S 我观察到 search:search("anchovy", $options)/search:result
给出的文档似乎是文本格式而不是 json。
在这种情况下使用search:search
是否可以得到想要的结果?
这应该可以完成工作:
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
declare variable $options := <options xmlns="http://marklogic.com/appservices/search"><transform-results apply="raw" /><extract-metadata>
<json-property>title</json-property>
</extract-metadata></options>;
search:search("anchovy", $options)//title/text()
此处我们指定要从文档中提取并放入结果集中的 JSON 属性。
作为旁注,您可能需要查看使用 ServerSide JavaScript if you're working a lot with JSON documents, and in that case you could also utilise jsearch。