diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Percolator.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Percolator.php | 128 |
1 files changed, 95 insertions, 33 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Percolator.php b/vendor/ruflin/elastica/lib/Elastica/Percolator.php index 2ec312b9..98d0490a 100644 --- a/vendor/ruflin/elastica/lib/Elastica/Percolator.php +++ b/vendor/ruflin/elastica/lib/Elastica/Percolator.php @@ -1,26 +1,44 @@ <?php - namespace Elastica; /** - * Percolator class + * Percolator class. * - * @category Xodoa - * @package Elastica * @author Nicolas Ruflin <spam@ruflin.com> - * @link http://www.elasticsearch.org/guide/reference/api/percolate/ + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html */ class Percolator { + const EXTRA_FILTER = 'filter'; + const EXTRA_QUERY = 'query'; + const EXTRA_SIZE = 'size'; + const EXTRA_TRACK_SCORES = 'track_scores'; + const EXTRA_SORT = 'sort'; + const EXTRA_FACETS = 'facets'; + const EXTRA_AGGS = 'aggs'; + const EXTRA_HIGHLIGHT = 'highlight'; + + private $_extraRequestBodyOptions = array( + self::EXTRA_FILTER, + self::EXTRA_QUERY, + self::EXTRA_SIZE, + self::EXTRA_TRACK_SCORES, + self::EXTRA_SORT, + self::EXTRA_FACETS, + self::EXTRA_AGGS, + self::EXTRA_HIGHLIGHT, + ); + /** - * Index object + * Index object. * * @var \Elastica\Index */ protected $_index = null; /** - * Construct new percolator + * Construct new percolator. * * @param \Elastica\Index $index */ @@ -32,15 +50,16 @@ class Percolator /** * Registers a percolator query, with optional extra fields to include in the registered query. * - * @param string $name Query name - * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to add - * @param array $fields Extra fields to include in the registered query - * and can be used to filter executed queries. + * @param string $name Query name + * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to add + * @param array $fields Extra fields to include in the registered query + * and can be used to filter executed queries. + * * @return \Elastica\Response */ public function registerQuery($name, $query, $fields = array()) { - $path = $this->_index->getName() . '/.percolator/' . $name; + $path = $this->_index->getName().'/.percolator/'.$name; $query = Query::create($query); $data = array_merge($query->toArray(), $fields); @@ -49,58 +68,100 @@ class Percolator } /** - * Removes a percolator query - * @param string $name query name + * Removes a percolator query. + * + * @param string $name query name + * * @return \Elastica\Response */ public function unregisterQuery($name) { - $path = $this->_index->getName() . '/.percolator/' . $name; + $path = $this->_index->getName().'/.percolator/'.$name; return $this->_index->getClient()->request($path, Request::DELETE); } /** - * Match a document to percolator queries + * Match a document to percolator queries. * - * @param \Elastica\Document $doc - * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which + * @param \Elastica\Document $doc + * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which * are executed. - * @param string $type - * @param array $params + * @param string $type + * @param array $params Supports setting additional request body options to the percolate request. + * [ Percolator::EXTRA_FILTER, + * Percolator::EXTRA_QUERY, + * Percolator::EXTRA_SIZE, + * Percolator::EXTRA_TRACK_SCORES, + * Percolator::EXTRA_SORT, + * Percolator::EXTRA_FACETS, + * Percolator::EXTRA_AGGS, + * Percolator::EXTRA_HIGHLIGHT ] + * * @return array With matching registered queries. */ public function matchDoc(Document $doc, $query = null, $type = 'type', $params = array()) { - $path = $this->_index->getName() . '/' . $type . '/_percolate'; + $path = $this->_index->getName().'/'.$type.'/_percolate'; $data = array('doc' => $doc->getData()); + $this->_applyAdditionalRequestBodyOptions($params, $data); + return $this->_percolate($path, $query, $data, $params); } /** - * Percolating an existing document + * Percolating an existing document. * - * @param string $id - * @param string $type - * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which + * @param string $id + * @param string $type + * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which * are executed. - * @param array $params + * @param array $params Supports setting additional request body options to the percolate request. + * [ Percolator::EXTRA_FILTER, + * Percolator::EXTRA_QUERY, + * Percolator::EXTRA_SIZE, + * Percolator::EXTRA_TRACK_SCORES, + * Percolator::EXTRA_SORT, + * Percolator::EXTRA_FACETS, + * Percolator::EXTRA_AGGS, + * Percolator::EXTRA_HIGHLIGHT ] + * * @return array With matching registered queries. */ public function matchExistingDoc($id, $type, $query = null, $params = array()) { $id = urlencode($id); - $path = $this->_index->getName() . '/' . $type . '/'. $id . '/_percolate'; + $path = $this->_index->getName().'/'.$type.'/'.$id.'/_percolate'; + + $data = array(); + $this->_applyAdditionalRequestBodyOptions($params, $data); - return $this->_percolate($path, $query, array(), $params); + return $this->_percolate($path, $query, $data, $params); + } + + /** + * Process the provided parameters and apply them to the data array. + * + * @param &$params + * @param &$data + */ + protected function _applyAdditionalRequestBodyOptions(&$params, &$data) + { + foreach ($params as $key => $value) { + if (in_array($key, $this->_extraRequestBodyOptions)) { + $data[$key] = $params[$key]; + unset($params[$key]); + } + } } /** - * @param string $path - * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query] $query [description] - * @param array $data - * @param array $params + * @param string $path + * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query] $query [description] + * @param array $data + * @param array $params + * * @return array */ protected function _percolate($path, $query, $data = array(), $params = array()) @@ -117,11 +178,12 @@ class Percolator if (isset($data['matches'])) { return $data['matches']; } + return array(); } /** - * Return index object + * Return index object. * * @return \Elastica\Index */ |