Collection::createIndex
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Collection::createIndex
Референца за `mysql-xdevapi-collection.createindex.php` со подобрена типографија и навигација.
Collection::createIndex
(Нема достапни информации за верзијата, можеби е само во Git)
Collection::createIndex — Create collection index
= NULL
Creates an index on the collection.
An exception is thrown if an index with the same name already exists, or if index definition is not correctly formed.
Параметри
index_name-
The name of the index that to create. This name must be a valid index name as accepted by the
CREATE INDEXSQL query. index_desc_json-
Definition of the index to create. It contains an array of IndexField objects, and each object describes a single document member to include in the index, and an optional string for the type of index that might be INDEX (default) or SPATIAL.
A single IndexField description consists of the following fields:
-
field: string, the full document path to the document member or field to be indexed. -
type: string, one of the supported SQL column types to map the field into. For numeric types, the optional UNSIGNED keyword may follow. For the TEXT type, the length to consider for indexing may be added. -
required: bool, (optional) true if the field is required to exist in the document. Defaults tofalse, except forGEOJSONwhere it defaults totrue. -
options: integer, (optional) special option flags for use when decodingGEOJSONdata. -
srid: integer, (optional) srid value for use when decodingGEOJSONdata.
It is an error to include other fields not described above in IndexDefinition or IndexField documents.
-
Вратени вредности
Примери
Пример #1 mysql_xdevapi\Collection::createIndex() example
<?php
$session = mysql_xdevapi\getSession("mysqlx://user:password@localhost");
$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();
$schema = $session->getSchema("addressbook");
$collection = $schema->createCollection("people");
// Creating a text index
$collection->createIndex(
'myindex1',
'{"fields": [{
"field": "$.name",
"type": "TEXT(25)",
"required": true}],
"unique": false}'
);
// A spatial index
$collection->createIndex(
'myindex2',
'{"fields": [{
"field": "$.home",
"type": "GEOJSON",
"required": true}],
"type": "SPATIAL"}'
);
// Index with multiple fields
$collection->createIndex(
'myindex3',
'{"fields": [
{
"field": "$.name",
"type": "TEXT(20)",
"required": true
},
{
"field": "$.age",
"type": "INTEGER"
},
{
"field": "$.job",
"type": "TEXT(30)",
"required": false
}
],
"unique": true
}'
);