ensureFulltextIndex() public method

Create fulltext index
public ensureFulltextIndex ( array | string $field = '$**', array $weights = null, $defaultLanguage = Language::ENGLISH, $languageOverride = null ) : Collection
$field array | string definition of fields where full text index ensured. May be string to ensure index on one field, array of fields to create full text index on few fields, and widdcard '$**' to create index on all fields of collection. Default value is '$**'
$weights array For a text index, the weight of an indexed field denotes the significance of the field relative to the other indexed fields in terms of the text search score.
$defaultLanguage The default language associated with the indexed data determines the rules to parse word roots (i.e. stemming) and ignore stop words. The default language for the indexed data is english.
$languageOverride To use a field with a name other than language, include the language_override option when creating the index.
return Collection
Example #1
0
 public function testEnsureFulltextIndex()
 {
     try {
         $this->collection->ensureFulltextIndex(array('fieldname1', 'fieldname2'), array('fieldname1' => 1, 'fieldname2' => 2), 'spanish');
     } catch (\MongoWriteConcernException $e) {
         $this->assertEquals('127.0.0.1:27017: text search not enabled', $e->getMessage());
         return;
     }
     $indexes = $this->collection->getIndexes();
     $index = $indexes[1];
     // 'textIndexVersion' differ in different versions of mongodb
     $this->assertArrayHasKey('textIndexVersion', $index);
     $dbVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($dbVersion, '2.6', '<')) {
         $this->assertEquals(1, $index['textIndexVersion']);
     } else {
         if (version_compare($dbVersion, '3.2', '<')) {
             $this->assertEquals(2, $index['textIndexVersion']);
         } else {
             $this->assertEquals(3, $index['textIndexVersion']);
         }
     }
     unset($index['textIndexVersion']);
     // chech other params
     $this->assertEquals($index, array('v' => 1, 'key' => array('_fts' => 'text', '_ftsx' => 1), 'name' => 'fieldname1_text_fieldname2_text', 'ns' => 'test.phpmongo_test_collection', 'default_language' => 'spanish', 'weights' => array('fieldname1' => 1, 'fieldname2' => 2), 'language_override' => 'language'));
 }
Example #2
0
 public function testEnsureFulltextIndex()
 {
     $this->collection->ensureFulltextIndex(array('fieldname1', 'fieldname2'), array('fieldname1' => 1, 'fieldname2' => 2), 'spanish');
     $indexes = $this->collection->getIndexes();
     $index = $indexes[1];
     // 'textIndexVersion' differ in different versions of mongodb
     $this->assertArrayHasKey('textIndexVersion', $index);
     $dbVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($dbVersion, '2.6', '<')) {
         $this->assertEquals(1, $index['textIndexVersion']);
     } else {
         if (version_compare($dbVersion, '3', '<')) {
             $this->assertEquals(2, $index['textIndexVersion']);
         } else {
             $this->assertEquals(3, $index['textIndexVersion']);
         }
     }
     unset($index['textIndexVersion']);
     // chech other params
     $this->assertEquals($index, array('v' => 1, 'key' => array('_fts' => 'text', '_ftsx' => 1), 'name' => 'fieldname1_text_fieldname2_text', 'ns' => 'test.phpmongo_test_collection', 'default_language' => 'spanish', 'weights' => array('fieldname1' => 1, 'fieldname2' => 2), 'language_override' => 'language'));
 }