2

I have several JSON like that :

[{
    "type": "car",
    "field1": "test"
}, {
    "type": "bike",
    "field1": "test"
}]

I stored them in MarkLogic 8.4 and I want to perform some search query on them to retrieve documents according to the type (eg : Find all documents with the type "car").

I have two potential solutions :

  • Set Marklogic collections to each document. Example : put "car" and "bike" collection on the example document. In my search query I can add a collection restriction.
  • Put an index on "type" field of each JSON

Is one method is better than another one in term of performances and/or best practices ?

Thanks, Romain.

4

1 回答 1

8

试试 cts.jsonPropertyValueQuery:

cts.search(cts.jsonPropertyValueQuery("type", "car"))

通用索引应该有你需要的信息。

编辑以扩展我的答案:您提到的两种解决方案都需要存储其他信息。在所描述的情况下,通用索引已经拥有您需要的信息,使其成为首选解决方案。如果 jsonPropertyValueQuery 变得模棱两可,这种方法将不再是我的首选;type也就是说,如果每份文件有多个财产。在这种情况下,查询将匹配任何type属性。

如果是这种情况,在属性上放置 JSON 属性范围索引type将无济于事,因为范围索引仍将包含该type属性的所有实例。

要处理文档中的多种类型,您将有两种选择:

  1. 使用集合
  2. 使用路径范围索引

这两个,我喜欢第一个。它很灵活——即使您的数据库中有不同结构的文档,您也可以使用它。这样,它可能会“证明”您的项目。权衡是您的代码在执行插入时需要管理文档的集合。不过,这很简单。

在性能方面,这两种方法中的任何一种都可以很好地处理查询,但选项二在索引期间需要做更多的工作。MarkLogic 将需要检查配置的路径是否存在于文档中,如果存在,则相应地更新索引。这是一个微小的差异,但有可能加起来。

于 2016-02-29T17:45:17.230 回答