【问题标题】:MarkLogic - Constrain results by fieldMarkLogic - 按字段约束结果
【发布时间】:2014-08-21 19:52:30
【问题描述】:

我有一个 MarkLogic 数据库,其中包含 NITF 格式的新闻文章,每篇文章都有以下信息。

<date.issue norm="20140321T000000" news:date="2014-03-21" news:time="00:00:00" news:origin="Generated"/>

我正在使用内置的 REST API 来访问这个数据库。我想在查询中按日期限制结果,即获取日期范围之间或特定日期之前或之后的所有文档。 MarkLogic 的 REST API 怎么可能做到这一点?

【问题讨论】:

    标签: database rest xquery marklogic


    【解决方案1】:

    分为三个步骤:

    1. 构建索引以支持查询
    2. 配置搜索选项
    3. 编写查询

    对于#1,您需要一个关于 news:date 的 xs:date 范围索引。范围索引允许 >、≥、=、≤、

    对于 #2,您需要使用 news:date 索引构建约束。它看起来像something like this

     <constraint name="news-date">
       <range type="xs:date">
         <element ns="news" name="date"/>
       </range>
     </constraint>
    

    注意 element/@ns 的值需要是完整的命名空间 URI,而不仅仅是前缀。此约束配置将成为your REST API configuration 的一部分。 (你也have the option to use QBE instead.

    对于#3,查询将在客户端构建并发送到服务器上的 REST API。查询可以表示为query stringstructured query。作为一个查询字符串,你可以这样做:

    news-date GT 2010-01-01 AND news-date LT 2010-12-31
    

    为此,请确保您的 REST API 配置包括 LT、GT 和 AND,它们显示在 the default query string grammar 中。

    结构化查询表示为 JSON 或 XML。这是与 JSON 相同的查询:

    {
      "query": {
        "queries": [
          { 
            "and-query": {
              "queries": [
                "range-constraint-query": {
                  "value": [ "2010-01-01" ],
                  "constraint-name": "news-date",
                  "range-operator": ">"
                },
                "range-constraint-query": {
                  "value": [ "2010-12-31" ],
                  "constraint-name": "news-date",
                  "range-operator": "<"
                }
              ]
            }
          }
        ]
      }
    }
    

    【讨论】:

    • 感谢 Dave 的详细回答。我尝试了这些步骤,但我收到以下错误,即使存在范围索引。 XDMP-ELEMATTRRIDXNOTFOUND: cts:element-attribute-values(fn:QName("http://iptc.org/std/NITF/2006-10-18", "date.issue"), fn:QName("http://news.com", "date"), (), ("type=date", "concurrent"), cts:and-query((cts:word-query("Geert", ("lang=en"), 1), cts:word-query("Wilders", ("lang=en"), 1)), ()), xs:double("1"), ()) -- No date element-attribute range index for fn:QName("http://iptc.org/std/NITF/2006-10-18", "date.issue") fn:QName("http://news.com", "date")
    • XDMP-ELEMATTRRIDXNOTFOUND 表示您尝试查询的范围索引不存在。仔细检查命名空间和本地名称,并检查以确保数据库已完成重新索引。
    • 谢谢。数据库尚未完成重新索引。此外,在使用 REST API 进行查询时,我究竟如何将此约束合并到查询字符串中?从上面给出的示例中看不太清楚,here 中似乎缺少范围约束示例。
    • 我上面的例子显示了一个使用约束的查询字符串:“news-date GT 2010-01-01 AND news-date LT 2010-12-31”。请参阅The Default String Query Grammar 了解 GT、LT 和 AND 的工作原理。要使用它,您需要向“/v1/search?q=news-date GT 2010-01-01 AND news-date LT 2010-12-31”发送一个 GET 请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2011-03-27
    相关资源
    最近更新 更多