Skip to content

Commit

Permalink
Merge pull request #914 from ruflin/nicolassing-master
Browse files Browse the repository at this point in the history
FileScript implementaiton
  • Loading branch information
ruflin committed Aug 11, 2015
2 parents 34ca77d + fb36fb7 commit 74f9137
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes

### Added
- Add Script File feature #902 #914

### Improvements

Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ gource:

## DOCKER IMAGES


# This creates the base image locally for local development. In case no local development is done anymore, make sure to remove this image.
all: nginx-image elasticsearch-image elastica-dev-image elastica-image elastica-data
# elastica image has to be built after elastica-dev image as it depends on it. Otherwise the remote image is fetched.

elastica-image:
docker build -t ruflin/elastica .
Expand Down
8 changes: 8 additions & 0 deletions ansible/roles/elasticsearch/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
- config-1.yml
- logging.yml
notify: restart elasticsearch

- name: Assures scripts dir exists
file: path=/etc/elasticsearch/scripts/ state=directory
- name: create custom config scripts
template: >
dest=/etc/elasticsearch/scripts/
src=calculate-distance.groovy
notify: restart elasticsearch

- name: create elasticsearch service script
template: >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
doc['location'].arcDistanceInKm(lat,lon)
4 changes: 4 additions & 0 deletions ansible/roles/elasticsearch/templates/config-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ bulk.udp.bulk_actions: 5
script.inline: on
script.indexed: on

script.engine.groovy.file: on

# Disable dynamic memory allocation
bootstrap.mlockall: true

Expand All @@ -33,6 +35,8 @@ node.name: Elastica
# Added for snapshot tests
path.repo: ["/tmp/backups"]

script.engine.groovy.file: on

{% endblock %}

{% block config %}
Expand Down
2 changes: 2 additions & 0 deletions env/elasticsearch/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ RUN ${ES_PLUGIN_BIN} -install mobz/elasticsearch-head

# Copy config files
COPY *.yml /usr/share/elasticsearch/config/
COPY scripts/* /usr/share/elasticsearch/config/scripts/


RUN mkdir -p /tmp/backups/backup1
RUN mkdir -p /tmp/backups/backup2
Expand Down
2 changes: 2 additions & 0 deletions env/elasticsearch/elasticsearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ bulk.udp.bulk_actions: 5
script.inline: on
script.indexed: on

script.engine.groovy.file: on

# Disable dynamic memory allocation
bootstrap.mlockall: true

Expand Down
1 change: 1 addition & 0 deletions env/elasticsearch/scripts/calculate-distance.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
doc['location'].arcDistanceInKm(lat,lon)
27 changes: 27 additions & 0 deletions lib/Elastica/AbstractScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Elastica;

/**
* Base class for Script object.
*
* @author Nicolas Assing <[email protected]>
*
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
*/
abstract class AbstractScript extends AbstractUpdateAction
{
/**
* @param array|null $params
* @param string $id
*/
public function __construct(array $params = null, $id = null)
{
if ($params) {
$this->setParams($params);
}

if ($id) {
$this->setId($id);
}
}
}
6 changes: 3 additions & 3 deletions lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ public function setScriptFields($scriptFields)
/**
* Adds a Script to the query.
*
* @param string $name
* @param \Elastica\Script $script Script object
* @param string $name
* @param \Elastica\AbstractScript $script Script object
*
* @return $this
*/
public function addScriptField($name, Script $script)
public function addScriptField($name, AbstractScript $script)
{
$this->_params['script_fields'][$name] = $script->toArray();

Expand Down
12 changes: 3 additions & 9 deletions lib/Elastica/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
*/
class Script extends AbstractUpdateAction
class Script extends AbstractScript
{
const LANG_MVEL = 'mvel';
const LANG_JS = 'js';
Expand All @@ -35,19 +35,13 @@ class Script extends AbstractUpdateAction
*/
public function __construct($script, array $params = null, $lang = null, $id = null)
{
$this->setScript($script);
parent::__construct($params, $id);

if ($params) {
$this->setParams($params);
}
$this->setScript($script);

if ($lang) {
$this->setLang($lang);
}

if ($id) {
$this->setId($id);
}
}

/**
Expand Down
115 changes: 115 additions & 0 deletions lib/Elastica/ScriptFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
namespace Elastica;

use Elastica\Exception\InvalidException;

/**
* Script objects, containing script internals.
*
* @author avasilenko <[email protected]>
* @author Nicolas Assing <[email protected]>
*
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
*/
class ScriptFile extends AbstractScript
{
/**
* @var string
*/
private $_scriptFile;

/**
* @param string $scriptFile
* @param array|null $params
* @param null $id
*/
public function __construct($scriptFile, array $params = null, $id = null)
{
parent::__construct($params, $id);

$this->setScriptFile($scriptFile);
}

/**
* @param string $scriptFile
*
* @return $this
*/
public function setScriptFile($scriptFile)
{
$this->_scriptFile = $scriptFile;

return $this;
}

/**
* @return string
*/
public function getScriptFile()
{
return $this->_scriptFile;
}

/**
* @param string|array|\Elastica\Script $data
*
* @throws \Elastica\Exception\InvalidException
*
* @return self
*/
public static function create($data)
{
if ($data instanceof self) {
$scriptFile = $data;
} elseif (is_array($data)) {
$scriptFile = self::_createFromArray($data);
} elseif (is_string($data)) {
$scriptFile = new self($data);
} else {
throw new InvalidException('Failed to create scriptFile. Invalid data passed.');
}

return $scriptFile;
}

/**
* @param array $data
*
* @throws \Elastica\Exception\InvalidException
*
* @return self
*/
protected static function _createFromArray(array $data)
{
if (!isset($data['script_file'])) {
throw new InvalidException("\$data['script_file'] is required");
}

$scriptFile = new self($data['script_file']);

if (isset($data['params'])) {
if (!is_array($data['params'])) {
throw new InvalidException("\$data['params'] should be array");
}
$scriptFile->setParams($data['params']);
}

return $scriptFile;
}

/**
* @return array
*/
public function toArray()
{
$array = array(
'script_file' => $this->_scriptFile,
);

if (!empty($this->_params)) {
$array['params'] = $this->_params;
}

return $array;
}
}
Loading

0 comments on commit 74f9137

Please sign in to comment.