send() public method

Submits the mapping and sends it to the server.
public send ( array $query = [] ) : Response
$query array Query string parameters to send with mapping
return Elastica\Response Response object
Example #1
0
 public function testGetIdNoSource()
 {
     // Creates a new index 'xodoa' and a type 'user' inside this index
     $indexName = 'xodoa';
     $typeName = 'user';
     $client = $this->_getClient();
     $index = $client->getIndex($indexName);
     $index->create(array(), true);
     $type = $index->getType($typeName);
     $mapping = new Mapping($type);
     $mapping->disableSource();
     $mapping->send();
     // Adds 1 document to the index
     $docId = 3;
     $doc1 = new Document($docId, array('username' => 'hans'));
     $type->addDocument($doc1);
     // Refreshes index
     $index->refresh();
     $resultSet = $type->search('hans');
     $this->assertEquals(1, $resultSet->count());
     $result = $resultSet->current();
     $this->assertEquals(array(), $result->getSource());
     $this->assertInstanceOf('Elastica\\Result', $result);
     $this->assertEquals($indexName, $result->getIndex());
     $this->assertEquals($typeName, $result->getType());
     $this->assertEquals($docId, $result->getId());
     $this->assertGreaterThan(0, $result->getScore());
     $this->assertInternalType('array', $result->getData());
 }
 protected function _getTestIndex()
 {
     $index = $this->_createIndex('has_child_test');
     $parentType = $index->getType('parent');
     $childType = $index->getType('child');
     $childMapping = new Mapping($childType);
     $childMapping->setParent('parent');
     $childMapping->send();
     $altType = $index->getType('alt');
     $altDoc = new Document('alt1', array('name' => 'altname'));
     $altType->addDocument($altDoc);
     $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => '*****@*****.**'));
     $parentType->addDocument($parent1);
     $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => '*****@*****.**'));
     $parentType->addDocument($parent2);
     $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**'));
     $child1->setParent('parent1');
     $childType->addDocument($child1);
     $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => '*****@*****.**'));
     $child2->setParent('parent2');
     $childType->addDocument($child2);
     $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => '*****@*****.**', 'alt' => array(array('name' => 'testname'))));
     $child3->setParent('parent2');
     $childType->addDocument($child3);
     $index->refresh();
     return $index;
 }
 /**
  * @return Status
  */
 public function validate()
 {
     $this->outputIndented("Validating mappings...");
     if ($this->optimizeIndexForExperimentalHighlighter && !in_array('experimental highlighter', $this->availablePlugins)) {
         $this->output("impossible!\n");
         return Status::newFatal(new RawMessage("wgCirrusSearchOptimizeIndexForExperimentalHighlighter is set to true but the " . "'experimental highlighter' plugin is not installed on all hosts."));
     }
     $requiredMappings = $this->mappingConfig;
     if (!$this->checkMapping($requiredMappings)) {
         /** @var Mapping[] $actions */
         $actions = array();
         // TODO Conflict resolution here might leave old portions of mappings
         foreach ($this->types as $typeName => $type) {
             $action = new Mapping($type);
             foreach ($requiredMappings[$typeName] as $key => $value) {
                 $action->setParam($key, $value);
             }
             $actions[] = $action;
         }
         try {
             foreach ($actions as $action) {
                 $action->send();
             }
             $this->output("corrected\n");
         } catch (ExceptionInterface $e) {
             $this->output("failed!\n");
             $message = ElasticsearchIntermediary::extractMessage($e);
             return Status::newFatal(new RawMessage("Couldn't update mappings.  Here is elasticsearch's error message: {$message}\n"));
         }
     }
     return Status::newGood();
 }
Example #4
0
 /**
  * @param \Elastica\Index $index
  * @param string $mappingName
  * @param array $mappingData
  *
  * @return void
  */
 protected function sendMapping(Index $index, $mappingName, array $mappingData)
 {
     $type = $index->getType($mappingName);
     $this->messenger->info(sprintf('Send mapping type "%s" (index: "%s")', $mappingName, $index->getName()));
     $mapping = new Mapping($type);
     foreach ($mappingData as $key => $value) {
         $mapping->setParam($key, $value);
     }
     $mapping->send();
 }
Example #5
0
 public function postInstall(RecordInterface $record)
 {
     $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port']));
     $index = $client->getIndex('amun');
     $index->create();
     $type = $index->getType('page');
     $mapping = new Mapping();
     $mapping->setType($type);
     $mapping->setProperties(array('id' => array('type' => 'string', 'include_in_all' => false), 'userId' => array('type' => 'integer', 'include_in_all' => false), 'path' => array('type' => 'string', 'include_in_all' => true), 'title' => array('type' => 'string', 'include_in_all' => true), 'content' => array('type' => 'string', 'include_in_all' => true), 'date' => array('type' => 'date', 'include_in_all' => false)));
     $mapping->send();
 }
 /**
  * @return Type
  */
 private function getType()
 {
     if (null === $this->type) {
         $this->type = $this->getIndex()->getType($this->typeName);
         $mapping = new Type\Mapping();
         $mapping->setType($this->type);
         $mapping->setTtl(['enabled' => true]);
         $mapping->setProperties([self::ID_FIELD => ['type' => 'string', 'include_in_all' => true], self::VALUE_FIELD => ['type' => 'string', 'include_in_all' => true]]);
         $mapping->send();
     }
     return $this->type;
 }
 /**
  * Create the mapping for the type.
  *
  * @param \Cake\Datasource\ConnectionInterface $db The Elasticsearch connection
  * @return void
  */
 public function create(ConnectionInterface $db)
 {
     if (empty($this->schema)) {
         return;
     }
     $index = $db->getIndex();
     if (!$index->exists()) {
         $index->create();
     }
     $type = $index->getType($this->table);
     $mapping = new ElasticaMapping();
     $mapping->setType($type);
     $mapping->setProperties($this->schema);
     $mapping->send();
     $this->created[] = $db->configName();
 }
 /**
  * Creates the elastic search mapping for the provided table, or just prints it out
  * to the screen if the `dry-run` option is provided.
  *
  * @param string $table The table name to inspect and create a mapping for
  * @return bool
  */
 public function main($table)
 {
     $table = TableRegistry::get($table);
     $schema = $table->schema();
     $mapping = ['@timestamp' => ['type' => 'date', 'format' => 'basic_t_time_no_millis||dateOptionalTime||basic_date_time||ordinal_date_time_no_millis||yyyy-MM-dd HH:mm:ss'], 'transaction' => ['type' => 'string', 'index' => 'not_analyzed'], 'type' => ['type' => 'string', 'index' => 'not_analyzed'], 'primary_key' => ['type' => 'string', 'index' => 'not_analyzed'], 'source' => ['type' => 'string', 'index' => 'not_analyzed'], 'parent_source' => ['type' => 'string', 'index' => 'not_analyzed'], 'original' => ['properties' => []], 'changed' => ['properties' => []], 'meta' => ['properties' => ['ip' => ['type' => 'string', 'index' => 'not_analyzed'], 'url' => ['type' => 'string', 'index' => 'not_analyzed'], 'user' => ['type' => 'string', 'index' => 'not_analyzed'], 'app_name' => ['type' => 'string', 'index' => 'not_analyzed']]]];
     $properties = [];
     foreach ($schema->columns() as $column) {
         $properties[$column] = $this->mapType($schema, $column);
     }
     if ($table->hasBehavior('AuditLog')) {
         $whitelist = (array) $table->behaviors()->AuditLog->config('whitelist');
         $blacklist = (array) $table->behaviors()->AuditLog->config('blacklist');
         $properties = empty($whitelist) ? $properties : array_intersect_key($properties, array_flip($whitelist));
         $properties = array_diff_key($properties, array_flip($blacklist));
     }
     $mapping['original']['properties'] = $mapping['changed']['properties'] = $properties;
     $client = ConnectionManager::get('auditlog_elastic');
     $index = $client->getIndex(sprintf($client->getConfig('index'), '-' . gmdate('Y.m.d')));
     $type = $index->getType($table->table());
     $elasticMapping = new ElasticaMapping();
     $elasticMapping->setType($type);
     $elasticMapping->setProperties($mapping);
     if ($this->params['dry-run']) {
         $this->out(json_encode($elasticMapping->toArray(), JSON_PRETTY_PRINT));
         return true;
     }
     if ($this->params['use-templates']) {
         $template = ['template' => sprintf($client->getConfig('index'), '*'), 'mappings' => $elasticMapping->toArray()];
         $response = $client->request('_template/template_' . $type->getName(), Request::PUT, $template);
         $this->out('<success>Successfully created the mapping template</success>');
         return $response->isOk();
     }
     if (!$index->exists()) {
         $index->create();
     }
     $elasticMapping->send();
     $this->out('<success>Successfully created the mapping</success>');
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function store(array $data, array $keys, $namespace)
 {
     $type = $this->index->getType($namespace);
     // Build mapping
     $mapping = new Mapping();
     $mapping->setType($type);
     $mapping->setProperties(array('hash' => array('type' => 'string', 'include_in_all' => true), 'keys' => array('type' => 'string', 'include_in_all' => true)));
     $mapping->send();
     // Build document
     $document = new Document('', array_merge($data, array('hash' => $this->getHash($data, $keys), 'keys' => $keys)));
     $type->addDocument($document);
     $this->index->refresh();
 }
Example #10
0
 /**
  * {@inheritDoc}
  */
 public function createType(ClassMetadata $metadata)
 {
     $type = $this->getIndex($metadata->index)->getType($metadata->type);
     $properties = $this->getMapping($metadata->fieldMappings);
     $rootProperties = $this->getRootMapping($metadata->rootMappings);
     $mapping = new Mapping($type, $properties);
     $mapping->disableSource($metadata->source);
     if (isset($metadata->boost)) {
         $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => $metadata->boost));
     }
     if (isset($metadata->parent)) {
         $mapping->setParent($metadata->parent);
     }
     foreach ($rootProperties as $key => $value) {
         $mapping->setParam($key, $value);
     }
     $mapping->send();
     return $type;
 }
 /**
  * @param array $mappingArray
  * @param Elastica\Type $type
  */
 protected function setMapping(array $mappingArray, Elastica\Type $type)
 {
     try {
         $mapping = new Elastica\Type\Mapping();
         $mapping->setType($type);
         $mapping->setProperties($mappingArray);
         $mapping->send();
     } catch (\Exception $e) {
         $this->outputLine('Could not add mapping for type ' . $type->getName() . '. Message was ' . $e->getMessage());
     }
 }
Example #12
0
 /**
  * Create Elastica Analysis.
  *
  * @param object $index Elastica Index
  * @param array $posttypes Array containing all post types
  * @param array $terms Array containing all terms
  * @return object $index Elastica Index
  *
  * @since 3.0.0
  */
 protected function analysis($index, $posttypes, $terms)
 {
     //Check integrity
     if (empty($index)) {
         return null;
     }
     //Check integrity
     if (empty($posttypes) && empty($terms)) {
         return null;
     }
     //Define properties
     $props = array('id' => array('type' => 'integer', 'include_in_all' => false), 'tags' => array('type' => 'string', 'index' => 'analyzed'), 'parent' => array('type' => 'integer', 'index' => 'analyzed'), 'title' => array('type' => 'string', 'index' => 'analyzed'), 'content' => array('type' => 'string', 'index' => 'analyzed'), 'excerpt' => array('type' => 'string', 'index' => 'analyzed'), 'author' => array('type' => 'integer', 'index' => 'analyzed'), 'date' => array('type' => 'date', 'format' => 'date_time_no_millis'), 'tags_suggest' => array('type' => 'completion', 'analyzer' => 'simple', 'search_analyzer' => 'simple', 'payloads' => false), '_boost' => array('type' => 'float', 'include_in_all' => false));
     /**
      * Update props analysis.
      *
      * @param array $props
      *
      * @since 3.3.0
      */
     do_action('tto_plugin_search_analysis', $props);
     //Set analysis
     if (isset($posttypes) && !empty($posttypes)) {
         foreach ($posttypes as $k) {
             if (empty($k)) {
                 continue;
             }
             $index->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('indexAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'asciifolding', 'filter_' . $k)), 'searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase', 'asciifolding', 'filter_' . $k))), 'filter' => array('filter_' . $k => array('type' => 'standard', 'language' => TTO_LOCAL, 'ignoreCase' => true)))), true);
             //Define new Type
             $type = $index->getType($k);
             //Define a new Elastica Mapper
             $mapping = new Mapping();
             $mapping->setType($type);
             //$mapping->setParam('analyzer', 'indexAnalyzer');
             //$mapping->setParam('search_analyzer', 'searchAnalyzer');
             //Define boost field
             /*$mapping->setParam('_boost', array(
                   'name' => '_boost',
                   'null_value' => 1.0
               ));*/
             //Set mapping
             $mapping->setProperties($props);
             //Send mapping to type
             $mapping->send();
         }
     }
     //Set analysis
     if (isset($terms) && !empty($terms)) {
         foreach ($terms as $t) {
             if (empty($t)) {
                 continue;
             }
             $index->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('indexAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'asciifolding', 'filter_' . $t)), 'searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase', 'asciifolding', 'filter_' . $t))), 'filter' => array('filter_' . $t => array('type' => 'standard', 'language' => TTO_LOCAL, 'ignoreCase' => true)))), true);
             //Define new Type
             $type = $index->getType($t);
             //Define a new Elastica Mapper
             $mapping = new Mapping();
             $mapping->setType($type);
             $mapping->setParam('index_analyzer', 'indexAnalyzer');
             $mapping->setParam('search_analyzer', 'searchAnalyzer');
             //Define boost field
             $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0));
             //Set mapping
             $mapping->setProperties($props);
             // Send mapping to type
             $mapping->send();
         }
     }
     //Return index
     return $index;
 }
Example #13
0
 /**
  * Creates the index and sets the mapping for this type.
  *
  * @param bool $recreate OPTIONAL Recreates the index if true (default = false)
  */
 public function create($recreate = false)
 {
     $this->getIndex()->create($this->_indexParams, $recreate);
     $mapping = new Mapping($this->getType());
     $mapping->setProperties($this->_mapping);
     $mapping->setSource(array('enabled' => $this->_source));
     $mapping->send();
 }
 /**
  * Define all known mappings
  */
 protected function createMappings(\Elastica\Index $index)
 {
     foreach ($this->getIndexedClasses() as $class) {
         /** @var $sng Searchable */
         $sng = singleton($class);
         $type = $sng->getElasticaType();
         if (isset($this->mappings[$type])) {
             // captured later
             continue;
         }
         $mapping = $sng->getElasticaMapping();
         $mapping->setType($index->getType($type));
         $mapping->send();
     }
     if ($this->mappings) {
         foreach ($this->mappings as $type => $fields) {
             $mapping = new Mapping();
             $mapping->setProperties($fields);
             $mapping->setParam('date_detection', false);
             $mapping->setType($index->getType($type));
             $mapping->send();
         }
     }
 }
Example #15
0
/**
 * Created by PhpStorm.
 * User: Jiang Yu
 * Date: 2016/04/22
 * Time: 16:11.
 */
use Elastica\Type\Mapping;
use ESGateway\DataModel\FieldMapping\MappingFactory;
require __DIR__ . '/../../bootstrap.php';
$createMappingHandler = function (\Elastica\Type $type) {
    $mapping = new Mapping();
    $mapping->setType($type);
    $mappingProperties = (new MappingFactory())->make();
    $mapping->setProperties($mappingProperties);
    $response = $mapping->send();
    dump('create default mapping');
    dump($response);
};
$options = getopt('', ['gv:', 'index:', 'createIndex:']);
$client = new \Elastica\Client(['host' => ELASTIC_SEARCH_HOST, 'port' => ELASTIC_SEARCH_PORT]);
if (isset($options['createIndex'])) {
    $indexName = trim($options['createIndex']);
    assert(is_string($indexName) && strlen($indexName) > 0 && strpos($indexName, 'farm_v') === 0, 'bad index name');
    $index = $client->getIndex($indexName);
    $exists = $index->exists();
    if (!$exists) {
        $response = $index->create();
        dump('create index');
        dump($response);
    }
Example #16
0
 protected function createMapping()
 {
     dump(__METHOD__);
     $type = $this->getClient()->getIndex($this->getIndexName())->getType($this->getTypeName());
     $mapping = new Mapping();
     $mapping->setType($type);
     $mappingProperties = (new MappingFactory())->make();
     $mapping->setProperties($mappingProperties);
     $response = $mapping->send();
     //        dump($response);
     $this->assertNotTrue($response->hasError());
     return $mappingProperties;
 }
 /**
  * @param Index $elasticaIndex
  */
 protected function setMapping(Index $elasticaIndex)
 {
     foreach ($this->configuration->getTypes() as $typeName) {
         $mapping = new Mapping();
         $type = $elasticaIndex->getType($typeName);
         $mapping->setType($type);
         // Set properties
         if ($properties = $this->configuration->getMappingProperties($type)) {
             $mapping->setProperties($properties);
         }
         // Set params if any
         if ($mappingParams = $this->configuration->getMappingParams($type)) {
             foreach ($mappingParams as $mappingParam => $mappingParamValue) {
                 $mapping->setParam($mappingParam, $mappingParamValue);
             }
         }
         if ($mappingParams || $properties) {
             $mapping->send();
         }
     }
 }
Example #18
0
 /**
  * @group functional
  *
  * Test setting a dynamic template and validate whether the right mapping is applied after adding a document which
  * should match the dynamic template. The example is the template_1 from the Elasticsearch documentation.
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-root-object-type.html
  */
 public function testDynamicTemplate()
 {
     $index = $this->_createIndex();
     $type = $index->getType('person');
     // set a dynamic template "template_1" which creates a multi field for multi* matches.
     $mapping = new Mapping($type);
     $mapping->setParam('dynamic_templates', array(array('template_1' => array('match' => 'multi*', 'mapping' => array('type' => 'multi_field', 'fields' => array('{name}' => array('type' => '{dynamic_type}', 'index' => 'analyzed'), 'org' => array('type' => '{dynamic_type}', 'index' => 'not_analyzed')))))));
     $mapping->send();
     // when running the tests, the mapping sometimes isn't available yet. Optimize index to enforce reload mapping.
     $index->optimize();
     // create a document which should create a mapping for the field: multiname.
     $testDoc = new Document('person1', array('multiname' => 'Jasper van Wanrooy'), $type);
     $index->addDocuments(array($testDoc));
     sleep(1);
     //sleep 1 to ensure that the test passes every time
     // read the mapping from Elasticsearch and assert that the multiname.org field is "not_analyzed"
     $newMapping = $type->getMapping();
     $this->assertArrayHasKey('person', $newMapping, 'Person type not available in mapping from ES. Mapping set at all?');
     $this->assertArrayHasKey('properties', $newMapping['person'], 'Person type doesnt have any properties. Document properly added?');
     $this->assertArrayHasKey('multiname', $newMapping['person']['properties'], 'The multiname property is not added to the mapping. Document properly added?');
     $this->assertArrayHasKey('fields', $newMapping['person']['properties']['multiname'], 'The multiname field of the Person type is presumably not a multi_field type. Dynamic mapping not applied?');
     $this->assertArrayHasKey('org', $newMapping['person']['properties']['multiname']['fields'], 'The multi* matcher did not create a mapping for the multiname.org property when indexing the document.');
     $this->assertArrayHasKey('index', $newMapping['person']['properties']['multiname']['fields']['org'], 'Indexing status of the multiname.org not available. Dynamic mapping not fully applied!');
     $this->assertEquals('not_analyzed', $newMapping['person']['properties']['multiname']['fields']['org']['index']);
     $index->delete();
 }
Example #19
0
 /**
  * Creates a new mapping object
  *
  * @param Elastica\Type $type  the type where you want to create your mapping
  * @param Array         $array the mapping which you want to create
  *
  * @return void
  */
 function createMapping($type, $array)
 {
     // Define mapping
     $mapping = new Mapping();
     $mapping->setType($type);
     // Set mapping
     $mapping->setProperties($array);
     // Send mapping to type
     $mapping->send();
 }