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

Sij*_*esh 2 xquery marklogic

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

grt*_*tjn 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"
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅"使用范围索引对搜索进行排序"下的"性能指南".

HTH!