【问题标题】:Order by an element while fetching documents from a directory从目录中获取文档时按元素排序
【发布时间】:2016-08-30 10:40:54
【问题描述】:

我必须从一个目录中获取所有文档,并使用下面的查询来做同样的事情。现在我需要返回基于 EffectiveDate 元素排序的结果。我们可以使用 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>

【问题讨论】:

    标签: xquery marklogic


    【解决方案1】:

    如果您希望对结果进行有效排序,则需要在effectiveDate 上使用date 范围索引。在某些情况下,查询优化器可以使用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!

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 如果您的文档集很大,这可能会导致性能非常差。这不是一个坏/错误的方法,但是带有范围索引的 cts:search 仍然可以在大量文档上执行,随着目录中文档数量的增长,这会显着减慢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 2015-12-23
      • 2012-12-11
      • 2015-11-06
      • 1970-01-01
      • 2021-12-16
      • 2019-05-24
      相关资源
      最近更新 更多