Cloudant Search:使用计数方面的条件是什么?

Cloudant Search: what are the conditions for using the count facet?

我正在尝试使用 Cloudant 设置搜索索引,但我发现 the documentation 非常混乱。它指出:

FACETING

In order to use facets, all the documents in the index must include all the fields that have faceting enabled. If your documents do not include all the fields, you will receive a bad_request error with the following reason, “dim field_name does not exist.”

If each document does not contain all the fields for facets, it is recommended that you create separate indexes for each field. If you do not create separate indexes for each field, you must include only documents that contain all the fields. Verify that the fields exist in each document using a single if statement.

Counts

The count facet syntax takes a list of fields, and returns the number of query results for each unique value of each named field.

The count operation works only if the indexed values are strings. The indexed values cannot be mixed types. For example, if 100 strings are indexed, and one number, then the index cannot be used for count operations. You can check the type using the typeof operator, and convert using parseInt, parseFloat and .toString() functions.

具体来说,"all the documents in the index include all the fields that have faceting enabled".

是什么意思

例如,如果我的数据库包含以下文档:

{
  "_id": "mydoc"
  "subjects": [ "subject A", "subject B" ]
}

然后我这样写了一个搜索索引:

function (doc) {
  for(var i=0; i < doc.subjects.length; i++)
    index("hasSubject", doc.subjects[i], {facet: true});
}

这是否违法,因为 mydoc 没有名为 hasSubject 的字段?当我们重写查询时,它看起来像;

{
  "_id": "mydoc"
  "hasSubject": true,
  "subjects": [ "subject A", "subject B" ]
}

那会突然好起来吗...?

所以新文档位于 https://console.ng.bluemix.net/docs/services/Cloudant/api/search.html#faceting ;但是,分面的条目是相同的。所以没什么大不了的。

为了回答您的问题,我认为文档的意思是您数据库中的所有 JSON 文档都必须包含 subjects 字段,这就是您声明的内容在您的示例中进行分面。

所以我也会考虑像这样定义您的搜索索引:

function (doc) {
  if (doc.subjects) {
    for(var i=0; i < doc.subjects.length; i++) {
      if (typeof doc.subjects[i] == "string") {
        index("hasSubject", doc.subjects[i], {facet: true});
      }
    }
  }
}

如果您的数据库中有这样的文档:

{
  "_id": "mydoc"
  "hasSubject": true,
}

我认为那会突然让你的小平面变得不正常。