1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
<?php
/**
* Copyright 2011-2014 Horde LLC (http://www.horde.org/)
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Horde_Content
*/
class Content_Indexer
{
/**
* ElasticSearch client
* @var Horde_ElasticSearch_Client
*/
protected $_es;
/**
* User manager object
* @var Content_Users_Manager
*/
protected $_userManager;
/**
* Type management object
* @var Content_Types_Manager
*/
protected $_typeManager;
/**
* Object manager
* @var Content_Objects_Manager
*/
protected $_objectManager;
/**
* Constructor
*/
public function __construct(Horde_ElasticSearch_Client $es,
Content_Users_Manager $userManager,
Content_Types_Manager $typeManager,
Content_Objects_Manager $objectManager)
{
$this->_es = $es;
$this->_userManager = $userManager;
$this->_typeManager = $typeManager;
$this->_objectManager = $objectManager;
}
public function index($index, $type, $id, $data)
{
try {
$this->_es->add($index, $type, $id, $data);
} catch (Horde_ElasticSearch_Exception $e) {
throw new Content_Exception($e);
}
}
public function search($index, $type, $query)
{
try {
return $this->_es->search($index, $type, $query);
} catch (Horde_ElasticSearch_Exception $e) {
throw new Content_Exception($e);
}
}
/**
* Convenience method - if $object is an array, it is taken as an array of
* 'object' and 'type' to pass to objectManager::ensureObjects() if it's a
* scalar value, it's taken as the object_id and simply returned.
*/
protected function _ensureObject($object)
{
if (is_array($object)) {
$object = current($this->_objectManager->ensureObjects(
$object['object'], (int)current($this->_typeManager->ensureTypes($object['type']))));
}
return (int)$object;
}
}
|