IBM Cloud Docs
正在使用IBM Cloudant查询

正在使用IBM Cloudant查询

IBM® Cloudant® for IBM Cloud®Query 是针对IBM Cloudant数据库的声明式 JSON 查询语法。 您可以通过IBM Cloudant 使用 jsontext 类型的索引。

在下列情况下,可以通过以下方式指定创建索引的方式 使其类型为 json

  • 您很清楚自己想要查找哪些数据。
  • 您希望将存储和处理要求降到最低。

But for maximum flexibility when you search for data, you typically create an index of type text. Indexes of type text have a simple mechanism for automatically indexing all the fields in the documents.

While more flexible, text indexes might take longer to create and require more storage resources than json indexes.

创建索引

您可以创建以下类型的索引:

  • "type": "json"
  • "type": "text"

创建 type=json 索引

To create a JSON index in the database $DATABASE, make a POST request to /$DATABASE/_index with a JSON object that describes the index in the request body. JSON 对象的 type 字段必须设置为 json。 JSON 索引可以是分区的,也可以是全局的。 全局;该选项可通过 partitioned 字段设置。

请参阅下面使用 HTTP 请求 JSON 类型索引的示例:

POST /$DATABASE/_index HTTP/1.1
Content-Type: application/json

请参阅下面的示例,该 JSON 对象为名为 foo-partitioned-index 的字段创建了一个名为 foo 的分区索引:

{
    "index": {
        "fields": ["foo"]
    },
    "name" : "foo-partitioned-index",
    "type" : "json",
    "partitioned": true
}

请参阅下面的示例,该 JSON 对象为名为 bar-global-index 的字段创建了一个名为 bar 的全局索引:

{
    "index": {
        "fields": ["bar"]
    },
    "name" : "bar-global-index",
    "type" : "json",
    "partitioned": false
}

请参阅以下返回 JSON 的示例,确认索引已创建:

{
    "result": "created"
}
请求正文格式
字段 描述
index fields - 使用 排序语法 的字段名 JSON 数组。 也允许使用嵌套字段,例如 person.name
ddoc(可选) 创建索引的设计文档名称。 默认情况下,每个索引都在自己的设计文档中创建。 索引可按设计文件分组,以提高效率。 但是,如果对设计文档中的一个索引进行更改,则同一文档中的所有其他索引都会失效。
type(可选) 可以是 jsontext。 默认为 json
name(可选) 索引名称。 如果没有提供名称,系统将自动生成一个名称。
partitioned (optional, boolean) 确定该索引是否分区。 有关详细信息,请参阅 partitioned 字段

partitioned 字段

该字段设置创建的索引是分区索引还是全局索引。

分区字段值
描述 注释
true 创建分区索引。 只能在分区数据库中使用。
false 创建全局索引。 可用于任何数据库。

默认设置遵循数据库的 partitioned 设置:

默认分区值
数据库是否分区? 默认 partitioned 允许的值
true true, false
false false

需要重申的是,默认 partitioned 值为 true 用于在分区数据库中创建的索引。 该默认值意味着索引不能 用于满足全局查询。

返回码
代码 描述
200 索引已成功创建或存在于数据库中。
400 错误请求 - 请求正文不符合指定格式。

创建 type=text 索引

在创建单个文本索引时,使用默认值是个不错的做法,但有些有用的索引属性是可以修改的。

text 索引可以是分区的,也可以是全局的;该选项可通过使用 partitioned 字段来设置。

对于全文索引 (FTI),type 必须设置为 text

nameddoc 属性用于将索引分组到设计文档中。 通过自定义字符串值使用属性来引用索引组。 如果没有为这些字段提供值、 就会自动填充一个哈希值。

If you create multiple text indexes in a database, with the same ddoc value, you need to know at least the ddoc value and the name value. 创建具有相同 ddoc 值的多个索引时,可将它们放入同一个设计文档中。 一般来说 您必须将每个文本索引放入自己的设计文档中。

有关更多信息、 有关 text 索引的更多 信息。

请参阅以下请求创建分区索引的 JSON 文档示例:

{
    "index": {
        "fields": [
            {
                "name": "Movie_name",
                "type": "string"
            }
        ]
    },
    "name": "Movie_name-text",
    "type": "text",
    "partitioned": true
}

请参阅以下请求创建全局索引的 JSON 文档示例:

{
    "index": {
        "fields": [
            {
                "name": "Movie_name",
                "type": "string"
            }
        ]
    },
    "name": "Movie_name-text",
    "type": "text",
    "partitioned": false
}

请参阅以下请求创建更复杂分区索引的 JSON 文档示例:

{
    "type": "text",
    "name": "my-index",
    "ddoc": "my-index-design-doc",
    "index": {
        "default_field": {
            "enabled": true,
            "analyzer": "german"
        },
        "selector": {},
        "fields": [
            {"name": "married", "type": "boolean"},
            {"name": "lastname", "type": "string"},
            {"name": "year-of-birth", "type": "number"}
        ]
    },
    "partitioned": true
}

index 字段

index 字段包含文本索引的特定设置。

要自动索引所有文档中的所有字段、 使用简单的语法:

"index": {}

索引过程会遍历数据库中所有文档的所有字段。

示例电影的演示数据库 中,你可以看到一个包含数据库中所有字段和所有文档的文本索引示例。

在为大型数据集的所有文档中的所有字段编制索引时要小心谨慎,因为这可能是一项耗费资源的工作。

请参阅下面的 JSON 文档示例,该文档请求创建所有文档中所有字段的索引:

{
	"type": "text",
	"index": { }
}

default_field 字段

default_field 值指定 $text 操作符如何与索引一起使用。

default_field 包括两把钥匙:

Default_field 字段键
描述
enabled 启用或禁用 default_field index。 缺省值是 true

default_field 中的 analyzer 关键字指定了索引分析文本的方式。 稍后、 可以使用 $text 操作符查询该索引。 更多信息,请参阅替代分析仪的 搜索文档。 当文件索引使用英语以外的语言时,您可以选择其他分析器、 或对分析器有其他特殊要求时,如匹配电子邮件地址。

If the default_field isn't specified, or is supplied with an empty object, it defaults to true and the standard analyzer is used.

The fields array

The fields array includes a list of fields that must be indexed for each document. 如果您知道某个索引只能对特定字段进行查询、 则可使用此字段限制索引的大小。 每个字段还必须指定要索引的类型。 可接受的类型如下表所示:

  • "boolean"
  • "string"
  • "number"

index_array_lengths 字段

IBM Cloudant查询文本索引有一个名为 index_array_lengths 的属性。 如果没有明确设置该属性、 默认值为 true

如果字段设置为 true、 索引需要额外工作。 这项工作包括扫描每份文档中的任何数组、 并创建一个字段来保存找到的每个数组的长度。

在下列情况下,您可能更愿意将 index_array_lengths 字段设置为 false

  • 您不需要知道数组的长度。
  • 你不能使用 $size 运算符。
  • 数据库中的文件很复杂、 或不完全受你控制。 因此,很难估计确定和存储数组长度所需的额外处理所产生的影响。

$size 操作符要求将 index_array_lengths 字段设置为 true。 否则,操作员就无法工作。

请参阅以下 JSON 文件示例,其中包含优化生产系统性能的建议设置:

{
	"default_field": {
		"enabled": false
	},
	"index_array_lengths": false
}

partitioned 字段

该字段决定创建的索引是分区索引还是全局索引。

分区字段值
描述 注释
true 创建分区索引。 只能在分区数据库中使用。
false 创建全局索引。 可用于任何数据库。

默认设置遵循数据库的 partitioned 设置:

数据库分区设置
数据库是否分区? 默认 partitioned 允许的值
true true, false
false false