Marklogic (Nodejs API) - Search documents that match 2 (or more) conditions in object array attribute -
my documents stored in json in marklogic (i remove useless attributes case):
{ documentid: '', languages: [{ locale: 'en_uk', content: { translated: 'true', } }, { locale: 'de_de', content: { translated: 'false', } }, {...}], }
edit: seem 'useless' attributes cause problems. here detailed object.
{ 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 */ }
i try find documents have @ least 1 object in languages locale = 'en_uk' , content.translated = true with
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') ) );
and
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')) ) ) ) ) )
but in both cases, query return documents match 2 conditions in whole document, not in each object of languages array.
i read documentation haven't found anything. have ideas how can make query ?
edit: try near query doesn't work. doesn't match documents.
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) ) ) )
i ask if can change object structure.
edit2: finally, use xquery request correct result.
xdmp:directory("/product/direcory/")/languages[code eq "ja_jp" , content/translated eq "true"] ! root(.)
in case, use eq content/translated condition because boolean stored string. !root(.): return whole object, not language objects match condition [code eq "ja_jp" , content/translated eq "true"]
try using near-query, 1 of location qualifiers available in structured queries. provide locale , translated queries, specify distance: 1
, ordered: true
. note whether works depend on "useless attributes" removed were.
if doesn't work, you'll need introduce layer structure.
{ documentid: '', languages: [{ item: { locale: 'en_uk', content: { translated: 'true', } } }, { item: { locale: 'de_de', content: { translated: 'false', } } }, {...}], }
that's not real pretty, let run container-query.
Comments
Post a Comment