/**
  * Set a script for this aggregation.
  *
  * @param string|Script $script
  *
  * @return $this
  */
 public function setScript($script)
 {
     if ($script instanceof Script) {
         $params = array_merge($this->getParams(), $script->toArray());
         return $this->setParams($params);
     }
     return $this->setParam('script', $script);
 }
 /**
  * Set a script for this aggregation
  * @param string|Script $script
  * @return AbstractSimpleAggregation
  */
 public function setScript($script)
 {
     if ($script instanceof Script) {
         $this->setParam('params', $script->getParams());
         $script = $script->getScript();
     }
     return $this->setParam('script', $script);
 }
Ejemplo n.º 3
0
 /**
  * @param  string                              $name   Name of the Script field
  * @param  \Elastica\Script                     $script
  * @throws \Elastica\Exception\InvalidException
  * @return \Elastica\ScriptFields
  */
 public function addScript($name, Script $script)
 {
     if (!is_string($name) || !strlen($name)) {
         throw new InvalidException('The name of a Script is required and must be a string');
     }
     $this->setParam($name, $script->toArray());
     return $this;
 }
Ejemplo n.º 4
0
 public function testSetScript()
 {
     $string = '_score * 2.0';
     $lang = 'mvel';
     $params = array('param1' => 'one', 'param2' => 1);
     $script = new Script($string);
     $script->setLang('mvel');
     $script->setParams($params);
     $customScoreQuery = new CustomScore();
     $customScoreQuery->setScript($script);
     $expected = array('custom_score' => array('query' => array('match_all' => new \stdClass()), 'script' => $string, 'params' => $params, 'lang' => $lang));
     $this->assertEquals($expected, $customScoreQuery->toArray());
 }
Ejemplo n.º 5
0
 /**
  * Set script
  *
  * @param  string|\Elastica\Script          $script
  * @return \Elastica\Query\CustomScore
  */
 public function setScript($script)
 {
     $script = Script::create($script);
     foreach ($script->toArray() as $param => $value) {
         $this->setParam($param, $value);
     }
     return $this;
 }
Ejemplo n.º 6
0
 public function testAddFilterScript()
 {
     $customFiltersScoreQuery = new CustomFiltersScore();
     $rangeFilter = new Range('age', array('from' => 20, 'to' => 30));
     $rangeScript = "doc['num1'].value > 1";
     $customFiltersScoreQuery->addFilterScript($rangeFilter, $rangeScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $rangeScript))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
     $customFiltersScoreQuery = new CustomFiltersScore();
     $script = "doc['num1'].value > 1";
     $rangeScript = new Script($script);
     $rangeScript->setParam('param1', 1);
     $rangeScript->setLang(Script::LANG_GROOVY);
     $customFiltersScoreQuery->addFilterScript($rangeFilter, $rangeScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $script))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
     $termFilter = new Term();
     $termFilter->setTerm('name', 'ruflin');
     $termScript = "doc['num2'].value > 1";
     $customFiltersScoreQuery->addFilterScript($termFilter, $termScript);
     $expected = array('custom_filters_score' => array('query' => array('match_all' => new \stdClass()), 'filters' => array(array('filter' => $rangeFilter->toArray(), 'script' => $script), array('filter' => $termFilter->toArray(), 'script' => $termScript))));
     $this->assertEquals($expected, $customFiltersScoreQuery->toArray());
 }
Ejemplo n.º 7
0
 /**
  * @param \Elastica\Document|\Elastica\Script $data
  * @param string $opType
  * @return \Elastica\Bulk\Action\AbstractDocument
  */
 public static function create($data, $opType = null)
 {
     //Check type
     if (!$data instanceof Document && !$data instanceof Script) {
         throw new \InvalidArgumentException("The data needs to be a Document or a Script.");
     }
     if (null === $opType && $data->hasOpType()) {
         $opType = $data->getOpType();
     }
     //Check that scripts can only be used for updates
     if ($data instanceof Script && isset($opType) && $opType != self::OP_TYPE_UPDATE) {
         throw new \InvalidArgumentException("When performing an update action, the data needs to be a Document or a Script.");
     }
     switch ($opType) {
         case self::OP_TYPE_DELETE:
             $action = new DeleteDocument($data);
             break;
         case self::OP_TYPE_CREATE:
             $action = new CreateDocument($data);
             break;
         case self::OP_TYPE_UPDATE:
             $action = new UpdateDocument($data);
             break;
         case self::OP_TYPE_INDEX:
         default:
             $action = new IndexDocument($data);
             break;
     }
     return $action;
 }
Ejemplo n.º 8
0
 /**
  * Adds a Script to the query
  *
  * @param  string          $name
  * @param  \Elastica\Script $script Script object
  * @return \Elastica\Query  Query object
  */
 public function addScriptField($name, Script $script)
 {
     $this->_params['script_fields'][$name] = $script->toArray();
     return $this;
 }
Ejemplo n.º 9
0
 /**
  * Sets the script for the term.
  *
  * @param string $script The script for the term.
  *
  * @return $this
  */
 public function setScript($script)
 {
     $this->setParam('script', Script::create($script));
     return $this;
 }
Ejemplo n.º 10
0
 /**
  * Sets script object
  *
  * @param  \Elastica\Script|string|array $script
  * @return \Elastica\Filter\Script
  */
 public function setScript($script)
 {
     $script = Elastica\Script::create($script);
     return $this->setParams($script->toArray());
 }
Ejemplo n.º 11
0
 /**
  * Update document, using update script. Requires elasticsearch >= 0.19.0
  *
  * @param  int                                       $id      document id
  * @param  array|\Elastica\Script|\Elastica\Document $data    raw data for request body
  * @param  string                                    $index   index to update
  * @param  string                                    $type    type of index to update
  * @param  array                                     $options array of query params to use for query. For possible options check es api
  * @return \Elastica\Response
  * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
  */
 public function updateDocument($id, $data, $index, $type, array $options = array())
 {
     $path = $index . '/' . $type . '/' . $id . '/_update';
     if ($data instanceof Script) {
         $requestData = $data->toArray();
     } elseif ($data instanceof Document) {
         $requestData = array('doc' => $data->getData());
         if ($data->getDocAsUpsert()) {
             $requestData['doc_as_upsert'] = true;
         }
         $docOptions = $data->getOptions(array('version', 'version_type', 'routing', 'percolate', 'parent', 'fields', 'retry_on_conflict', 'consistency', 'replication', 'refresh', 'timeout'));
         $options += $docOptions;
         // set fields param to source only if options was not set before
         if ($data instanceof Document && ($data->isAutoPopulate() || $this->getConfigValue(array('document', 'autoPopulate'), false)) && !isset($options['fields'])) {
             $options['fields'] = '_source';
         }
     } else {
         $requestData = $data;
     }
     //If an upsert document exists
     if ($data instanceof Script || $data instanceof Document) {
         if ($data->hasUpsert()) {
             $requestData['upsert'] = $data->getUpsert()->getData();
         }
     }
     if (!isset($options['retry_on_conflict'])) {
         $retryOnConflict = $this->getConfig("retryOnConflict");
         $options['retry_on_conflict'] = $retryOnConflict;
     }
     $response = $this->request($path, Request::POST, $requestData, $options);
     if ($response->isOk() && $data instanceof Document && ($data->isAutoPopulate() || $this->getConfigValue(array('document', 'autoPopulate'), false))) {
         $responseData = $response->getData();
         if (isset($responseData['_version'])) {
             $data->setVersion($responseData['_version']);
         }
         if (isset($options['fields'])) {
             $this->_populateDocumentFieldsFromResponse($response, $data, $options['fields']);
         }
     }
     return $response;
 }
Ejemplo n.º 12
0
 /**
  * Add a script_score function to the query.
  *
  * @param Script         $script a Script object
  * @param AbstractFilter $filter an optional filter to apply to the function
  * @param float          $weight the weight of the function
  *
  * @return $this
  */
 public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null, $weight = null)
 {
     return $this->addFunction('script_score', $script->toArray(), $filter, $weight);
 }
Ejemplo n.º 13
0
 /**
  * Sets script object.
  *
  * @param \Elastica\Script|string|array $script
  *
  * @return $this
  */
 public function setScript($script)
 {
     return $this->setParam('script', Elastica\Script::create($script));
 }
Ejemplo n.º 14
0
 /**
  * @group functional
  */
 public function testUpdateDocumentWithoutSource()
 {
     $index = $this->_createIndex();
     $type = $index->getType('elastica_type');
     $mapping = new Mapping();
     $mapping->setProperties(array('name' => array('type' => 'string', 'store' => 'yes'), 'counter' => array('type' => 'integer', 'store' => 'no')));
     $mapping->disableSource();
     $type->setMapping($mapping);
     $newDocument = new Document();
     $newDocument->setAutoPopulate();
     $newDocument->set('name', 'Batman');
     $newDocument->set('counter', 1);
     $type->addDocument($newDocument);
     $script = new Script('ctx._source.counter += count; ctx._source.name = name');
     $script->setId($newDocument->getId());
     $script->setParam('count', 2);
     $script->setParam('name', 'robin');
     $script->setUpsert($newDocument);
     try {
         $type->updateDocument($script);
         $this->fail('Update request should fail because source is disabled. Fields param is not set');
     } catch (ResponseException $e) {
         $error = $e->getResponse()->getError();
         $this->assertContains('document_source_missing_exception', $error['type']);
     }
     $newDocument->setFieldsSource();
     try {
         $type->updateDocument($newDocument);
         $this->fail('Update request should fail because source is disabled. Fields param is set to _source');
     } catch (ResponseException $e) {
         $error = $e->getResponse()->getError();
         $this->assertContains('document_source_missing_exception', $error['type']);
     }
 }
 /**
  * Add a filter with a script to calculate the score
  * Only script part of script object is used
  *
  * @param  \Elastica\Filter\AbstractFilter         $filter Filter object
  * @param  \Elastica\Script|string|array           $script Script for calculating the score
  * @return \Elastica\Query\CustomFiltersScore Current object
  */
 public function addFilterScript(AbstractFilter $filter, $script)
 {
     $script = Script::create($script);
     $filterParam = array('filter' => $filter->toArray(), 'script' => $script->getScript());
     return $this->addParam('filters', $filterParam);
 }
Ejemplo n.º 16
0
 /**
  * Update document, using update script. Requires elasticsearch >= 0.19.0
  *
  * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
  * @throws \Elastica\Exception\InvalidException
  *
  * @param  \Elastica\Document|\Elastica\Script $data    Document with update data
  * @param  array                               $options array of query params to use for query. For possible options check es api
  * @return \Elastica\Response
  */
 public function updateDocument($data, array $options = array())
 {
     if (!$data instanceof Document && !$data instanceof Script) {
         throw new \InvalidArgumentException("Data should be a Document or Script");
     }
     if (!$data->hasId()) {
         throw new InvalidException('Document or Script id is not set');
     }
     $id = urlencode($data->getId());
     return $this->getIndex()->getClient()->updateDocument($id, $data, $this->getIndex()->getName(), $this->getName(), $options);
 }
Ejemplo n.º 17
0
 /**
  * @group functional
  */
 public function testUpdateDocumentPopulateFields()
 {
     $this->_checkScriptInlineSetting();
     $index = $this->_createIndex();
     $type = $index->getType('test');
     $client = $index->getClient();
     $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4'));
     $newDocument->setAutoPopulate();
     $type->addDocument($newDocument);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"');
     $script->setParam('count', 5);
     $script->setUpsert($newDocument);
     $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => '_source'));
     $data = $type->getDocument(1)->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('value1', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(15, $data['field2']);
     $this->assertArrayHasKey('field4', $data);
     $this->assertEquals('changed', $data['field4']);
     $this->assertArrayNotHasKey('field3', $data);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;');
     $script->setParam('count', 5);
     $script->setParam('field1', 'updated');
     $script->setUpsert($newDocument);
     $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => 'field2,field4'));
     $document = $type->getDocument(1);
     $data = $document->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('updated', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(20, $data['field2']);
     $this->assertArrayNotHasKey('field3', $data);
     $this->assertArrayNotHasKey('field4', $data);
 }
Ejemplo n.º 18
0
 /**
  * @param \Elastica\Script|array|string $data
  * @return \Elastica\Document
  */
 public function setScript($data)
 {
     $script = Script::create($data);
     $this->_script = $script;
     return $this;
 }
Ejemplo n.º 19
0
 public function testSetScript()
 {
     $document = new Document();
     $script = new Script('ctx._source.counter += count');
     $script->setParam('count', 1);
     $this->assertFalse($document->hasScript());
     $document->setScript($script);
     $this->assertTrue($document->hasScript());
     $this->assertSame($script, $document->getScript());
 }
 /**
  * @dataProvider dataProviderCreateInvalid
  * @expectedException \Elastica\Exception\InvalidException
  */
 public function testCreateInvalid($data)
 {
     Script::create($data);
 }
 /**
  * Add a script_score function to the query
  * @param Script $script a Script object
  * @param AbstractFilter $filter an optional filter to apply to the function
  * @return \Elastica\Query\FunctionScore
  */
 public function addScriptScoreFunction(Script $script, AbstractFilter $filter = NULL)
 {
     return $this->addFunction('script_score', $script->toArray(), $filter);
 }
Ejemplo n.º 22
0
 /**
  * @group unit
  */
 public function testSetScript()
 {
     $script = new Script('foo');
     $this->assertEquals('foo', $script->getScript());
     $script->setScript('bar');
     $this->assertEquals('bar', $script->getScript());
     $this->assertInstanceOf('Elastica\\Script', $script->setScript('foo'));
 }
Ejemplo n.º 23
0
 /**
  * @param \Elastica\Script $script
  * @return array
  */
 protected function _getMetadataByScript(Script $script)
 {
     $params = array('index', 'type', 'id', 'version', 'version_type', 'routing', 'percolate', 'parent', 'ttl', 'timestamp');
     $metadata = $script->getOptions($params, true);
     return $metadata;
 }
Ejemplo n.º 24
0
 public function testUpdateDocumentPopulateFields()
 {
     $index = $this->_createIndex();
     $type = $index->getType('test');
     $client = $index->getClient();
     $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4'));
     $newDocument->setAutoPopulate();
     $type->addDocument($newDocument);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"');
     $script->setParam('count', 5);
     $newDocument->setScript($script);
     $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source'));
     $data = $newDocument->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('value1', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(15, $data['field2']);
     $this->assertArrayHasKey('field4', $data);
     $this->assertEquals('changed', $data['field4']);
     $this->assertArrayNotHasKey('field3', $data);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;');
     $script->setParam('count', 5);
     $script->setParam('field1', 'updated');
     $newDocument->setScript($script);
     $client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => 'field2,field4'));
     $data = $newDocument->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('value1', $data['field1'], 'Field1 should not be updated, because it is not in fields list');
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(20, $data['field2'], 'Field2 should be 20 after incrementing by 5');
     $this->assertArrayNotHasKey('field3', $data, 'Field3 should be removed already');
     $this->assertArrayNotHasKey('field4', $data, 'Field3 should be removed');
     $document = $type->getDocument(1);
     $data = $document->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('updated', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(20, $data['field2']);
     $this->assertArrayNotHasKey('field3', $data);
     $this->assertArrayNotHasKey('field4', $data);
 }