Collection::createIndex
(No version information available, might only be in Git)
Collection::createIndex — Create collection index
Description
$index_name
, string $index_desc_json
) : voidCreates 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.
Parameters
-
index_name
-
The name of the index that to create. This name must be a valid index name as accepted by the CREATE INDEX SQL 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 for GEOJSON where it defaults toTRUE
. -
options
: integer, (optional) special option flags for use when decoding GEOJSON data. -
srid
: integer, (optional) srid value for use when decoding GEOJSON data.
It is an error to include other fields not described above in IndexDefinition or IndexField documents.
-
Examples
Example #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
}'
);
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mysql-xdevapi-collection.createindex.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.