MarkLogic Node.js按“最后修改"排序 [英] MarkLogic Node.js Sort on "last-modified"
问题描述
我想按元数据属性"最后修改"对文档进行排序.我已经在"last-modified"上创建了element-range-index,并在管理面板中为数据库启用了选项"保持上次修改".但是,当我在Node.js中运行以下语句时,
I want to sort the documents by metadata property "last-modified". I have already created element-range-index on "last-modified" and also enabled option "maintain last modified" in Admin Panel for the database.But, when I run below statement in Node.js
return db.documents.query(
qb.where().orderBy(qb.sort("last-modified")).slice(from, length)).result();
我遇到了错误.请帮忙吗?
I am getting below error. Please help?
Error { [Error: query documents: response with invalid 400 status]
message: 'query documents: response with invalid 400 status',
statusCode: 400,
body:
{ errorResponse:
{ statusCode: 400,
status: 'Bad Request',
messageCode: 'SEARCH-BADORDERBY',
message: 'SEARCH-BADORDERBY: (err:FOER0000) Indexes are required to supp
ort element, element-attribute, json-property, or field sort specifications.' }
} }
推荐答案
更新:阐明了片段说明;固定示例
last-modified
属性(由maintain last modified
设置控制)不是存储在文档本身中,而是存储在文档属性,这是与每个文档关联的元数据片段.在MarkLogic Node.js API中,您可以使用 qb.documentFragment()
或 qb.propertiesFragment()
.
The last-modified
property (controlled by the maintain last modified
setting) is not stored in the document itself, but in the document properties, which is a metadata fragment associated with each document. In the MarkLogic Node.js API, you can constrain queries to the document or the document properties with qb.documentFragment()
or qb.propertiesFragment()
.
但是,您只能按返回数据的各个方面进行排序:默认情况下,文档本身.您可以使用 qb.fragmentScope()
来指定查询返回documents
还是properties
.
However, you can only sort by aspects of the data you are returning: by default, the document itself. You can specify that your query return either documents
or properties
using qb.fragmentScope()
.
注意:文档属性以XML格式存储在http://marklogic.com/xdmp/property
命名空间中,因此您的element-range-index也必须使用该命名空间.
Note: Document properties are stored as XML in the http://marklogic.com/xdmp/property
namespace, so your element-range-index must also use that namespace.
以下是获取10个最新文档属性的方法:
Here's how you would get the 10 most-recent document properties:
return db.documents.query(
qb.where(
qb.fragmentScope('properties')
).orderBy(
qb.sort(
qb.element(qb.qname('http://marklogic.com/xdmp/property', 'last-modified')),
'descending'
)
).slice(1, 10))
.result();
您可以通过将文档本身与qb.documentFragment()
匹配的任何查询来进一步限制这些结果:
You could further restrict these results by any query matching the document itself with qb.documentFragment()
:
假定test
上的int
范围索引:
assuming an int
range index on test
:
return db.documents.query(
qb.where(
qb.fragmentScope('properties'),
qb.documentFragment(
qb.range( qb.element('test'), '<', 20 )
)
).orderBy(
qb.sort(
qb.element(qb.qname('http://marklogic.com/xdmp/property', 'last-modified')),
'descending'
)
).slice(1, 10))
.result();
最后,如果要获取文档本身而不是其属性,则可以使用查询所检索的文档URI向db.documents.read()
发出请求:
Finally, if you want to then get the documents themselves, and not their properties, you could make a request to db.documents.read()
, with the document URIs retrieved by the query:
注意:以下示例使用lodash/下划线
return db.documents.query(
qb.where(
qb.fragmentScope('properties'),
qb.documentFragment(
qb.range( qb.element('test'), '<', 20 )
)
).orderBy(
qb.sort(
qb.element(qb.qname('http://marklogic.com/xdmp/property', 'last-modified')),
'descending'
)
).slice(1, 10))
.result(function(documents) {
db.documents.read(
_.map(documents, 'uri')
).result(function(documents) {
_.each(documents, function(doc) { console.log(doc.content); })
});
});
或者,您的应用程序可以直接在文档中维护last-modified
属性,在这种情况下,您的查询会简单得多:
Alternately, your application could maintain a last-modified
property directly in your documents, in which case your query can be much simpler:
return db.documents.query(
qb.where(
qb.range( qb.element('test'), '<', 20 )
).orderBy(
qb.sort( qb.element('last-modified'), 'descending' )
).slice(1, 10))
.result(function(documents) {
_.each(documents, function(doc) { console.log(doc.content); })
});
这篇关于MarkLogic Node.js按“最后修改"排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!