从目录中获取文档时按元素排序

时间:2016-08-30 10:40:54

标签: xquery marklogic

我必须从目录中获取所有文档,并使用下面的查询来执行相同操作。现在我需要返回基于effectiveDate元素排序的结果。我们可以使用order 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>

2 个答案:

答案 0 :(得分:7)

如果您希望有效地对结果进行排序,则date上需要effectiveDate范围索引。在某些情况下,查询优化器可以使用order by子句来利用该索引,但将cts:index-ordercts:search一起使用可能更为直截了当。类似的东西:

cts:search(collection(),
  cts:directory-query('/news', 1),
  cts:index-order(
    cts:element-reference(
      fn:QName("http://jnj.com/news", "effectiveDate")
      "type=date"
    )
  )
)

更多详情可在Performance Guide under "Sorting Searches Using Range Indexes" ..

中找到

HTH!

答案 1 :(得分:1)

根据您的使用条件,您还可以使用更简单的“order by”表达式,这听起来就像您想要做的那样。在这种情况下,您必须确保正确指向effectiveDate元素,因为它位于命名空间中。

for $d in xdmp:directory("/news/","1")
order by $d/*:effectiveDate
return $d