从目录中获取文档时按元素排序 [英] Order by an element while fetching documents from a directory
问题描述
我必须从目录中获取所有文档,并且正在使用以下查询来执行相同操作.现在,我需要返回根据有效日期元素排序的结果.我们可以在此代码中使用order by吗?
let $news :=xdmp:directory("/news/","1")
for $d in $news
return $d
-- Result -----
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">868e8a3a-058d-4b2d-8d69-0696f75ec97f</uuid>
<headLine>HeadLine 4</headLine>
<contributor>User 4</contributor>
<effectiveDate>2016-08-31</effectiveDate>
</NewsEntity>
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">311eeede-2560-4142-b882-b666ab08c9f8</uuid>
<headLine>HeadLine 3</headLine>
<contributor>User 3</contributor>
<effectiveDate>2016-08-28</effectiveDate>
</NewsEntity>
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">9bb67977-a217-425f-82e4-b4366e80d7c4</uuid>
<headLine>HeadLine 2</headLine>
<contributor>User 2</contributor>
<effectiveDate>2016-08-30</effectiveDate>
</NewsEntity>
如果要对结果进行有效排序,则需要在effectiveDate
上使用date
范围索引.在某些情况下,查询优化器可以使用order by
子句来利用该索引,但是使用 cts:search
.像这样:
cts:search(collection(),
cts:directory-query('/news', 1),
cts:index-order(
cts:element-reference(
fn:QName("http://jnj.com/news", "effectiveDate")
"type=date"
)
)
)
更多详细信息,请参见性能指南,在使用范围索引对搜索进行排序" 下.>
HTH!
I have to fetch all documents from a directory and am using below query to do same. Now i need to return the result which is sorted based on the effectiveDate element.Can we use order by along with this code
let $news :=xdmp:directory("/news/","1")
for $d in $news
return $d
-- Result -----
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">868e8a3a-058d-4b2d-8d69-0696f75ec97f</uuid>
<headLine>HeadLine 4</headLine>
<contributor>User 4</contributor>
<effectiveDate>2016-08-31</effectiveDate>
</NewsEntity>
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">311eeede-2560-4142-b882-b666ab08c9f8</uuid>
<headLine>HeadLine 3</headLine>
<contributor>User 3</contributor>
<effectiveDate>2016-08-28</effectiveDate>
</NewsEntity>
<?xml version="1.0" encoding="UTF-8"?>
<NewsEntity xmlns="http://jnj.com/news">
<uuid xmlns="">9bb67977-a217-425f-82e4-b4366e80d7c4</uuid>
<headLine>HeadLine 2</headLine>
<contributor>User 2</contributor>
<effectiveDate>2016-08-30</effectiveDate>
</NewsEntity>
If you want results to be sorted efficiently, you will need a date
range index on effectiveDate
. Under certain conditions the query optimizer can leverage that index using order by
clauses, but it might be more straight-forward to use cts:index-order
with a cts:search
. Something like:
cts:search(collection(),
cts:directory-query('/news', 1),
cts:index-order(
cts:element-reference(
fn:QName("http://jnj.com/news", "effectiveDate")
"type=date"
)
)
)
More details can be found in the Performance Guide under "Sorting Searches Using Range Indexes"..
HTH!
这篇关于从目录中获取文档时按元素排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!