在Xquery Marklogic中如何动态分类?
在Xquery Marklogic中如何动态分类?
let $sortelement := 'Salary'
for $doc in collection('employee')
order by $doc/$sortelement
return $doc
PS:排序将根据用户输入(例如数据,名称代替薪水)进行更改。
In XQuery Marklogic how to sort dynamically?
let $sortelement := 'Salary'
for $doc in collection('employee')
order by $doc/$sortelement
return $doc
PS: Sorting will change based on user input, like data, name in place of salary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
薪金
是该元素的名称,则您可以更一般地选择XPath中的任何元素,然后使用*
,然后应用谓词过滤器来测试是否local -name()
与所选元素值的变量匹配$ sortElement
:对集合中的所有项目进行排序的方式可能与较少的文档一起使用,但是如果您正在使用数百个文档在数千或数百万的文档中,您可能会发现,退回所有文档都是慢或吹出扩展的树缓存。
一个更有效的解决方案是在您打算排序的元素上创建范围索引,然后可以执行搜索,并使用指定的选项进行搜索,以订购结果
例如:
If
Salary
is the name of the element, then you could more generically select any element in the XPath with*
and then apply a predicate filter to test whether thelocal-name()
matches the variable for the selected element value$sortelement
:This manner of sorting all items in the collection may work with smaller number of documents, but if you are working with hundreds of thousands or millions of documents, you may find that pulling back all docs is either slow or blows out the Expanded Tree Cache.
A more efficient solution would be to create range indexes on the elements that you intend to sort on, and could then perform a search with options specified to order the results by
cts:index-order
with an appropriate reference to the indexed item, such ascts:element-reference()
,cts:json-property-reference()
,cts:field-reference()
.For example:
不推荐,因为引入安全问题,运行时崩溃和仅仅是“不良结果”的机会更高且难以控制 -
但可以作为最后的度假胜地。
所有Xquery都可以作为字符串动态创建,然后使用XDMP进行评估:est
更好地遵循MAD的指导,使用搜索API而不是Xquery Flowr表达式 - 请注意,这些API实际上“编译”到数据结构。这就是“ CTS构造仪”的操作: https://docs.marklogic.com/cts/cts/constructors < /a>
我发现将CTS搜索视为由 data 描述的结构化搜索 - CTS:XXX只是创建数据结构的助手功能。
(他们实际上没有进行任何搜索,他们会构建用于进行搜索的数据结构),
如果您查看搜索源:XXX API:您可以看到如何完成。
Not recommended because the chances of introducing security issues, runtime crashes and just 'bad results' is much higher and more difficult to control --
BUT available as a last resort.
ALL XQuery can be dynamically created as a string then evaluated using xdmp:eval
Much better to follow the guidance of Mads, and use the search apis instead of xquery FLOWR expressions -- note that these APIs actually 'compile down' to a data structure. This is what the 'cts constructors' do : https://docs.marklogic.com/cts/constructors
I find it helps to think of cts searches as a structured search described by data -- which the cts:xxx are simply helper functions to create the data structure.
(they dont actually do any searching, they build up a data structure that is used to do the searching)
If you look at the source to the search:xxx apis you can see how this is done.