搜索唯一元素对并将排序结果转换为 JSON
search unique element pairs and transform sorted result to JSON
我有以下示例输入:(它们是单独的文档)
<performance>
<year>2016</year>
<industry>Financials</industry>
<benchmark>Healthcare</benchmark>
</performance>
<performance>
<year>2017</year>
<industry>Technology</industry>
<benchmark>Financials</benchmark>
</performance>
<performance>
<year>2018</year>
<industry>Technology</industry>
<benchmark>Financials</benchmark>
</performance>
<performance>
<year>2019</year>
<industry>Financials</industry>
<benchmark>Materials</benchmark>
</performance>
<performance>
<year>2020</year>
<industry>Technology</industry>
<benchmark>Materials</benchmark>
</performance>
<performance>
<year>2021</year>
<industry>Technology</industry>
<benchmark>Healthcare</benchmark>
</performance>
我需要找到行业和基准对,按年对结果文档进行排序,最后将这些对转换为 JSON。我想使用Marklogic的索引来加速搜索和转换。预期输出为:
{
"Financials": [
"Materials",
"Healthcare"
],
"Technology": [
"Healthcare",
"Materials",
"Financials"
]
}
我的 Xquery 代码:
let $keys := ('Financials', 'Technology')
let $map := map:map()
let $_ :=
for $key in $keys
let $query := cts:path-range-query("/performance/industry", "=", $key)
let $v := cts:values(cts:path-reference('/performance/benchmark'), (), (), $query)
return map:put($map, $key, $v)
return xdmp:to-json($map)
意外输出:
{
"Financials":[
"Healthcare",
"Materials"
],
"Technology":[
"Financials",
"Healthcare",
"Materials"
]
}
我是否以错误的方式使用 Xquery 或误解了 Marklogic 索引的工作原理?我怎样才能得到正确的输出?我可以使用 Javascript 或 Xquery。
似乎当您在地图中放置一系列值时,它们最终会被排序。
使用此解决方案,我为每个 industry
获取所有 benchmark
和 year
,按 year
对 descending
中的值进行排序,使用 distinct-values()
进行去重,然后将它们放入 array-object()
以保持排序顺序。
let $map := map:map()
let $industries := ('Financials', 'Technology')
let $_ :=
for $industry in $industries
let $query := cts:path-range-query("/performance/industry", "=", $industry)
let $tuples := cts:value-tuples((cts:path-reference('/performance/benchmark'), cts:path-reference('/performance/year')), (), $query)
let $sorted-values :=
for $tuple in $tuples
let $benchmark := $tuple[1]
let $year := $tuple[2]
order by $year descending
return $benchmark
return map:put($map, $industry, array-node{ distinct-values($sorted-values) })
return xdmp:to-json($map)
我有以下示例输入:(它们是单独的文档)
<performance>
<year>2016</year>
<industry>Financials</industry>
<benchmark>Healthcare</benchmark>
</performance>
<performance>
<year>2017</year>
<industry>Technology</industry>
<benchmark>Financials</benchmark>
</performance>
<performance>
<year>2018</year>
<industry>Technology</industry>
<benchmark>Financials</benchmark>
</performance>
<performance>
<year>2019</year>
<industry>Financials</industry>
<benchmark>Materials</benchmark>
</performance>
<performance>
<year>2020</year>
<industry>Technology</industry>
<benchmark>Materials</benchmark>
</performance>
<performance>
<year>2021</year>
<industry>Technology</industry>
<benchmark>Healthcare</benchmark>
</performance>
我需要找到行业和基准对,按年对结果文档进行排序,最后将这些对转换为 JSON。我想使用Marklogic的索引来加速搜索和转换。预期输出为:
{
"Financials": [
"Materials",
"Healthcare"
],
"Technology": [
"Healthcare",
"Materials",
"Financials"
]
}
我的 Xquery 代码:
let $keys := ('Financials', 'Technology')
let $map := map:map()
let $_ :=
for $key in $keys
let $query := cts:path-range-query("/performance/industry", "=", $key)
let $v := cts:values(cts:path-reference('/performance/benchmark'), (), (), $query)
return map:put($map, $key, $v)
return xdmp:to-json($map)
意外输出:
{
"Financials":[
"Healthcare",
"Materials"
],
"Technology":[
"Financials",
"Healthcare",
"Materials"
]
}
我是否以错误的方式使用 Xquery 或误解了 Marklogic 索引的工作原理?我怎样才能得到正确的输出?我可以使用 Javascript 或 Xquery。
似乎当您在地图中放置一系列值时,它们最终会被排序。
使用此解决方案,我为每个 industry
获取所有 benchmark
和 year
,按 year
对 descending
中的值进行排序,使用 distinct-values()
进行去重,然后将它们放入 array-object()
以保持排序顺序。
let $map := map:map()
let $industries := ('Financials', 'Technology')
let $_ :=
for $industry in $industries
let $query := cts:path-range-query("/performance/industry", "=", $industry)
let $tuples := cts:value-tuples((cts:path-reference('/performance/benchmark'), cts:path-reference('/performance/year')), (), $query)
let $sorted-values :=
for $tuple in $tuples
let $benchmark := $tuple[1]
let $year := $tuple[2]
order by $year descending
return $benchmark
return map:put($map, $industry, array-node{ distinct-values($sorted-values) })
return xdmp:to-json($map)