Example #1
0
	/**
	 * @param string $name Name of the Script field
	 * @param Elastica_Script $script
	 * @return Elastica_ScriptFields
	 */
	public function addScript($name, Elastica_Script $script) {
		if (!is_string($name) || !strlen($name)) {
			throw new Elastica_Exception_Invalid('The name of a Script is required and must be a string');
		}
		$this->setParam($name, $script->toArray());

		return $this;
	}
Example #2
0
	/**
	 * Update document, using update script. Requires elasticsearch >= 0.19.0
	 *
	 * @param int $id document id
	 * @param Elastica_Script $script script to use for update
	 * @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.elasticsearch.org/guide/reference/api/update.html
	 */
	public function updateDocument($id, Elastica_Script $script, $index, $type, array $options = array()) {
		$path =  $index . '/' . $type . '/' . $id . '/_update';
		if (!isset($options['retry_on_conflict'])) {
			$retryOnConflict = $this->getConfig("retryOnConflict");
			$options['retry_on_conflict'] = $retryOnConflict;
		}

		$data = array(
			'script' => $script->getScript(),
		);
		if ($script->getLang() != null) {
			$data['lang'] = $script->getLang();
		}
		if ($script->getParams() != null) {
			$data['params'] = $script->getParams();
		}

		return $this->request($path, Elastica_Request::POST, $data, $options);
	}
Example #3
0
	/**
	 * Add a filter with a script to calculate the score
	 *
	 * @param Elastica_Filter_Abstract $filter Filter object
	 * @param Elastica_Script $script Script for calculating the score
	 * @return Elastica_Query_CustomFiltersScore Current object
	 */
	public function addFilterScript(Elastica_Filter_Abstract $filter, Elastica_Script $script) {
		$filterParam = array('filter' => $filter->toArray(), 'script' => $script->getScript());
		$this->addParam('filters', $filterParam);
		return $this;
	}
Example #4
0
 /**
  * Adds a Script to the query
  *
  * @param  Elastica_Script $script Script object
  * @return Elastica_Query  Query object
  */
 public function addScriptField($name, Elastica_Script $script)
 {
     $this->_params['script_fields'][$name] = $script->toArray();
     return $this;
 }
Example #5
0
 private static function _build_query($option)
 {
     $bool = null;
     if ($option['must']) {
         foreach ($option['must'] as $field => $values) {
             if ($values = !is_array($values) ? array($values) : $values) {
                 foreach ($values as $value) {
                     $bool = !$bool ? new Elastica_Query_Bool() : $bool;
                     $text = new Elastica_Query_Match();
                     $text->setMatch($field, $value);
                     $bool->addMust($text);
                 }
             }
         }
     }
     if ($option['must_not']) {
         foreach ($option['must_not'] as $field => $values) {
             if ($values = !is_array($values) ? array($values) : $values) {
                 foreach ($values as $value) {
                     $bool = !$bool ? new Elastica_Query_Bool() : $bool;
                     $text = new Elastica_Query_Match();
                     $text->setMatch($field, $value);
                     $bool->addMustNot($text);
                 }
             }
         }
     }
     if ($option['should']) {
         foreach ($option['should'] as $field => $values) {
             if ($values = !is_array($values) ? array($values) : $values) {
                 foreach ($values as $value) {
                     $bool = !$bool ? new Elastica_Query_Bool() : $bool;
                     $text = new Elastica_Query_Match();
                     $text->setMatch($field, $value);
                     $bool->addShould($text);
                 }
             }
         }
     }
     if ($option['range']) {
         foreach ($option['range'] as $field => $values) {
             $bool = !$bool ? new Elastica_Query_Bool() : $bool;
             $text = new Elastica_Query_Range();
             $text->addField($field, $values);
             $bool->addMust($text);
         }
     }
     $query = $bool ? new Elastica_Query($bool) : new Elastica_Query();
     if ($option['sort']) {
         $query->setSort($option['sort']);
     }
     if ($option['offset']) {
         $query->setFrom($option['offset']);
     }
     if ($option['select']) {
         $query->setFields($option['select']);
     }
     if ($option['limit'] > 0) {
         $query->setSize($option['limit']);
     }
     if ($option['script_fields']) {
         foreach ($option['script_fields'] as $name => $script_field) {
             if (!(isset($script_field['script']) && $script_field['script'])) {
                 continue;
             }
             $script = new Elastica_Script($script_field['script']);
             if (isset($script_field['params']) && $script_field['params']) {
                 $script->setParams($script_field['params']);
             }
             $query->addScriptField($name, $script);
         }
     }
     return $query;
 }