Marklogic 在数组或对象中搜索并且 return 仅匹配数组项 (javascript)

Marklogic search inside array or objects and return only matching array items (javascript)

是否可以跨文档搜索但仅使用数组元素内的特定属性进行搜索?此外,结果是否可能只包含匹配的数组元素而不包含整个文档(因为同一文档中的其他数组元素可能不匹配)。

示例:给定一个 JSON Marklogic 文档 { "name": "aName", "children": [{ "name": "A", "target": { "min": 2, "max": 10 } },{ "name": "B", "target": { "min": 22, "max": 32 } },{ "name": "C", "target": { "min": 4, "max": 7 } }] } 我只想匹配 children where target.min < 5 and target.max > 5.

在这种情况下,只有 children[0]children[2] 会匹配。然后如何仅指定对 return 的查询: [ { "name": "A", "target": { "min": 2, "max": 10 } }, { "name": "C", "target": { "min": 4, "max": 7 } } ]

如何构造相关查询。 N.B。我更喜欢服务器端 javascript 或 Node.js 实现。

试试这个

var a = {
    "name": "aName",
    "children": [{
        "name": "A",
        "target": {
            "min": 2,
            "max": 10
        }
    },{
        "name": "B",
        "target": {
            "min": 22,
            "max": 32
        }
    },{
        "name": "C",
        "target": {
            "min": 4,
            "max": 7
        }
    }]
};

var b = [];

for(var i of a.children){
if(i.target.min < 5 && i.target.max > 5){
 b.push(i);
}

}

console.log(b);

对于 Node.js,请查看 queryBuilder.extract 或搜索响应转换,看看它们是否满足您的需求。这两个主题都在此处讨论:http://docs.marklogic.com/guide/node-dev/search#id_24160.

在 SJS 中,jsearch 映射器和缩减器挂钩提供了类似的功能。请参阅 "Transforming Results with Map and Reduce" 上的以下主题:http://docs.marklogic.com/guide/search-dev/javascript#id_49222

在 MarkLogic 9 中,您可以使用 TDE 为每个具有最小值和最大值的子项投影一行,并使用 Optic 查询进行比较。

目前,Node.js 无法使用 Optic,但这是计划中的。

在 MarkLogic 8 中,最好的方法是将每个子项建模为单独的文档,并在最小值和最大值上创建范围索引。

希望对您有所帮助,