Marklogic(Nodejs API)-搜索与对象数组属性中的2个(或更多)条件匹配的文档 [英] Marklogic (Nodejs API) - Search documents that match 2 (or more) conditions in object array attribute

查看:87
本文介绍了Marklogic(Nodejs API)-搜索与对象数组属性中的2个(或更多)条件匹配的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档像这样在marklogic中存储在JSON中(我删除了我的案例中无用的属性):

My documents are stored in JSON in marklogic like this (I remove useless attributes for my case):

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

编辑:似乎我的无用"属性引起了一些问题.这是我的详细对象.

edit: It seem that my 'useless' attributes cause some problems. Here my 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 */
}

我尝试用语言查找所有至少包含一个对象的文档,其中 locale ='en_UK' content.translated = true

I try to find all documents that have at least one object in languages where locale = 'en_UK' and 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')
    )
  );

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'))
        )
      )
    )
  )
)

但在两种情况下,查询都返回与整个文档中的两个条件匹配的文档,而不是在 languages 数组的每个对象中匹配的文档.

but in both cases, the query return documents that match the 2 conditions in the whole document, not in each object of languages array.

我阅读了文档,但没有发现任何东西.您有什么想法可以查询吗?

I read the documentation but I haven't found anything. Do you have any ideas how I can make my query ?

编辑:我尝试了近查询,但是它不起作用.它与好的文档不匹配.

edit: I try a near query but it doesn't work. It doesn't match the good 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 will ask if I can change my object structure.

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

edit2: Finally, I use Xquery request to get the correct result.

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

在我的情况下,我将eq用于 content/translated 条件,因为我的布尔值存储为字符串. !root(.):返回整个对象,不仅返回符合条件 [code eq"ja_JP"和content/translated eq"true"]的语言对象

In my case, I use eq for content/translated condition because my boolean is stored as a string. !root(.): return the whole object, not only the language objects that match the condition [code eq "ja_JP" and content/translated eq "true"]

推荐答案

尝试使用位置限定符之一查询.提供您的语言环境和翻译后的查询,并指定distance: 1ordered: true.请注意,这是否有效取决于您删除的无用属性"的位置.

Try using a near-query, one of the Location Qualifiers available in structured queries. Provide your locale and translated queries, specify a distance: 1 and ordered: true. Note that whether this works will depend on where the "useless attributes" that you removed were.

如果这不起作用,则可能需要在结构中引入另一层.

If that doesn't work, you'll probably need to introduce another layer to your structure.

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

并不是很漂亮,但是它可以让您运行容器-查询.

That's not real pretty, but it would let you run a container-query.

这篇关于Marklogic(Nodejs API)-搜索与对象数组属性中的2个(或更多)条件匹配的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆