We have a search application using MarkLogic node.js. We use parsedQuery like this:
qb.parsedFrom(prop.search,
qb.parseBindings(
qb.word('name', qb.bind('name')),
qb.word('birthdate', qb.bind('birthdate')),
qb.range('count', qb.datatype('float'), qb.bind('count'))
)
)
The above currently supports search syntax like "count GT 50", etc. We would like to support searching using a derived value such as age. That is, we want to support a search syntax like "age GT 10", where the age value is not stored in the documents in the database but rather needs to be computed from the birthdate on the fly. We can't store the age in the documents since the age changes depending on the current date.
Is this possible and if so, how? If it matters, we are using ML8
答案 0 :(得分:2)
执行此操作的方法是使用custom constraint。解析函数根据参数构造cts查询。为了支持像“年龄GT 10”这样的东西,你需要构建像
这样的东西cts:element-range-query(
xs:QName("date-of-birth"), "<=",
fn:current-date() - xs:yearMonthDuration("P10Y")
)
您需要date-of-birth
上的日期范围索引。
构建自定义约束后,可以从MarkLogic Node.js客户端API调用它。见Using a Custom Constraint Parser。 Node Developer's Guide中也有一个example。
答案 1 :(得分:0)
You should be able to change that binding in this way:
qb.word('birthdate', qb.bind('age'))
But I assume you can't change qb.word to qb.value() as the birthdate has a different format than what you'd like to search on (1990-01-01 vs 20 for example). Can you please confirm?