2016-07-25 43 views
2

我的文档存储在JSON在这样marklogic搜索文件(我删除了我的情况下无用的属性):Marklogic(的NodeJS API) - 匹配对象数组2(或更多)的条件属性

{ 
    documentId: '', 
    languages: [{ 
    locale: 'en_UK', 
    content: { 
     translated: 'true', 
    } 
    }, { 
    locale: 'de_DE', 
    content: { 
     translated: 'false', 
    } 
    }, {...}], 
} 

编辑:看来我的'无用的'属性会导致一些问题。这里我详细的对象。

{ 
    documentId: '', 
    /* 4 attrs */, 
    languages: [{ 
     locale: 'en_UK', 
     attr: '', 
     content: { 
     /* 14 attrs */, 
     translated: true, 
     /* 2 or 4 attrs */, 
     } 
    }, { 
     locale: 'de_DE', 
     attr: '', 
     content: { 
     /* 14 attrs */, 
     translated: false, 
     /* 2 or 4 attrs */, 
     } 
    }, {...} 
    ], 
    /* 0 or 2 attrs */ 
} 

我试图找到在语言至少一个对象中的所有文件,其中区域= 'en_UK'content.translated =真

var query = 
    qb.where(
    qb.directory('myDocuments'), 
    qb.scope(
     qb.property('languages'), 
     qb.and(
     qb.scope(qb.property('code'), qb.term('en_UK')), 
     qb.scope(qb.property('translated'), qb.term('true')) 
    ), 
     qb.fragmentScope('properties') 
    ) 
); 

qb.where(
    qb.directory(myDocuments'), 
    qb.scope(qb.property('languages'), 
    qb.propertiesFragment(
     qb.value(
     qb.property('languages'), 
     qb.and(
      qb.scope(qb.property('code'), qb.term('en_UK')), 
      qb.scope(qb.property('translated'), qb.term('true')) 
     ) 
    ) 
    ) 
) 
) 

但在这两种情况下,查询返回文档在整个文档中匹配2个条件的nts,而不是在语言数组的每个对象中。

我阅读了文档,但是我还没有找到任何东西。你有什么想法,我怎么能做我的查询?

编辑:我尝试一个近查询,但它不起作用。它不符合好的文件。

qb.where(
    qb.directory(config.marklogicConfiguration.product), 
    qb.scope(qb.property('languages'), 
    qb.near(
     qb.and(
     qb.scope(qb.property('code'), qb.term('ja_JP')), 
     qb.scope(qb.property('translatedInTheLanguage'), qb.term('true')) 
    ), 
     1, 
     qb.weight(0), 
     qb.ordered(true) 
    ) 
) 
) 

我会问是否可以改变我的对象结构。

edit2:最后,我使用Xquery请求来获得正确的结果。

xdmp:directory("/product/direcory/")/languages[code eq "ja_JP" and content/translated eq "true"] ! root(.) 

就我而言,我使用情商内容/翻译的情形,因为我的布尔存储为一个字符串。 ! ():返回整个对象,不仅符合条件的语言对象[代码EQ“Ja_JP表示”作为内容/翻译EQ“真”]

+0

类似于edit2的XPath表达式将始终以过滤模式运行,并且可能不太好。 – grtjn

回答

2

尝试使用near-query,一个的Location Qualifiers available in structured queries。提供您的语言环境和翻译的查询,请指定distance: 1ordered: true。请注意,这是否有效取决于您删除的“无用属性”的位置。

如果这不起作用,您可能需要在结构中引入另一个图层。

{ 
    documentId: '', 
    languages: [{ 
    item: { 
     locale: 'en_UK', 
     content: { 
     translated: 'true', 
     } 
    } 
    }, { 
    item: { 
     locale: 'de_DE', 
     content: { 
     translated: 'false', 
     } 
    } 
    }, {...}], 
} 

这不是真的很漂亮,但它会让你运行一个container-query

+1

使用接近查询时,请确保启用相关的位置索引。 – grtjn