如何在TDE中设置动态日期过滤器?以及如何在TDE的context中使用日期函数筛选MarkLogic中近10天摄入文档的三元组?
Hey there! Let's tackle your two TDE-related questions and get that template working correctly for retaining only triples from documents ingested in the last 10 days.
1. How to Configure a Dynamic Date Filter in TDE
To set up a dynamic date filter in TDE, you’ll use conditional logic directly in the <context> node to target only documents that meet your date threshold. The core idea is to leverage MarkLogic’s built-in XQuery date functions to calculate a dynamic cutoff date, then compare it against either a date field in your document or system metadata for ingestion time.
2. Correct Usage of Date Functions in the <context> Node
First, let’s fix the issues in your provided template, then walk through the standard implementation tailored to your requirement:
Issues in Your Current Template
Your existing <context> expression has two critical problems:
- Syntax error: Missing closing parentheses and bracket. The expression cuts off before properly closing the date calculation and predicate.
- Potential timezone mismatch:
fn:currentDateTime()returns a timezone-aware value. If yourcreateDatefield is a non-timezone date (e.g.,xs:date), this could lead to unexpected comparison results.
Standard Implementation Based on Your Requirement
Your goal is to retain triples only from documents ingested in the last 10 days. Choose the scenario that matches your use case:
Scenario 1: Filtering on the Document's Own createDate Field
If your MedlineCitation documents have a createDate field reflecting when the content was created (not necessarily when it was ingested into MarkLogic), fix the template like this:
<template xmlns="http://marklogic.com/xdmp/tde"> <!-- Fixed context with proper syntax and timezone-aware comparison --> <context>/MedlineCitation[createDate > (fn:currentDateTime() - xs:dayTimeDuration('P10D'))]</context> <triples> <triple> <subject> <val>sem:iri(concat(ForeName,' ',LastName))</val> </subject> <predicate> <val>sem:iri('authored')</val> </predicate> <object> <val>xs:string(../../ArticleTitle)</val> </object> </triple> </triples> </template>
- Key fixes: Added the missing
)and]to close the date calculation and predicate. - Note: If
createDateis anxs:date(notxs:dateTime), switch tofn:currentDate()to avoid timezone conflicts:<context>/MedlineCitation[createDate > (fn:currentDate() - xs:dayTimeDuration('P10D'))]</context>
Scenario 2: Filtering on the Document's Ingestion Time (System Metadata)
If you want to filter based on when the document was actually inserted into MarkLogic (not the content’s internal createDate), use the system metadata function xdmp:document-inserted-dateTime() instead. This ensures you’re targeting documents ingested in the last 10 days, regardless of their internal date fields:
<template xmlns="http://marklogic.com/xdmp/tde"> <context>/MedlineCitation[xdmp:document-inserted-dateTime(.) > (fn:currentDateTime() - xs:dayTimeDuration('P10D'))]</context> <triples> <triple> <subject> <val>sem:iri(concat(ForeName,' ',LastName))</val> </subject> <predicate> <val>sem:iri('authored')</val> </predicate> <object> <val>xs:string(../../ArticleTitle)</val> </object> </triple> </triples> </template>
- How this works:
xdmp:document-inserted-dateTime(.)retrieves the timestamp when the current document was inserted into MarkLogic. Comparing this to the 10-day-old dynamic cutoff ensures only recently ingested documents generate triples.
Critical Notes for Date Functions in <context>
- Allowed functions: You can use any MarkLogic-supported XQuery date/time functions (e.g.,
fn:currentDateTime(),fn:currentDate(),xs:dayTimeDuration(),xdmp:document-inserted-dateTime()) directly in the<context>predicate. - Type consistency: Always ensure the date/time types of your document field and the calculated cutoff match (e.g., don’t compare an
xs:dateto anxs:dateTimewithout conversion). - Performance: For large datasets, consider indexing the date field (or system ingestion time) to speed up TDE evaluation.
内容的提问来源于stack exchange,提问作者AkG

