Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/engine/PhabricatorSearchEngineElastic.php
<?php | <?php | ||||
final class PhabricatorSearchEngineElastic extends PhabricatorSearchEngine { | final class PhabricatorSearchEngineElastic extends PhabricatorSearchEngine { | ||||
private $uri; | private $uri; | ||||
private $index; | private $index; | ||||
private $timeout; | private $timeout; | ||||
private $lastResponse; | |||||
public function __construct($uri, $index) { | public function __construct($uri, $index) { | ||||
$this->uri = $uri; | $this->uri = $uri; | ||||
$this->index = $index; | $this->index = $index; | ||||
} | } | ||||
public function setTimeout($timeout) { | public function setTimeout($timeout) { | ||||
$this->timeout = $timeout; | $this->timeout = $timeout; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getTimeout() { | public function getTimeout() { | ||||
return $this->timeout; | return $this->timeout; | ||||
} | } | ||||
public function getLastResponse() { | |||||
return $this->lastResponse; | |||||
} | |||||
public function reindexAbstractDocument( | public function reindexAbstractDocument( | ||||
PhabricatorSearchAbstractDocument $doc) { | PhabricatorSearchAbstractDocument $doc) { | ||||
$type = $doc->getDocumentType(); | $type = $doc->getDocumentType(); | ||||
$phid = $doc->getPHID(); | $phid = $doc->getPHID(); | ||||
$handle = id(new PhabricatorHandleQuery()) | $handle = id(new PhabricatorHandleQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
->withPHIDs(array($phid)) | ->withPHIDs(array($phid)) | ||||
->executeOne(); | ->executeOne(); | ||||
// URL is not used internally but it can be useful externally. | // URL is not used internally but it can be useful externally. | ||||
$spec = array( | $spec = array( | ||||
'title' => $doc->getDocumentTitle(), | 'title' => $doc->getDocumentTitle(), | ||||
'url' => PhabricatorEnv::getProductionURI($handle->getURI()), | 'url' => PhabricatorEnv::getProductionURI($handle->getURI()), | ||||
'dateCreated' => $doc->getDocumentCreated(), | 'dateCreated' => $doc->getDocumentCreated(), | ||||
'_timestamp' => $doc->getDocumentModified(), | '_timestamp' => $doc->getDocumentModified(), | ||||
'field' => array(), | 'field' => array(), | ||||
'relationship' => array(), | 'relationship' => array(), | ||||
); | ); | ||||
foreach ($doc->getFieldData() as $field) { | foreach ($doc->getFieldData() as $field) { | ||||
$spec['field'][] = array_combine(array('type', 'corpus', 'aux'), $field); | $spec['field'][] = array_combine(array('type', 'corpus', 'aux'), $field); | ||||
} | } | ||||
foreach ($doc->getRelationshipData() as $relationship) { | foreach ($doc->getRelationshipData() as $relationship) { | ||||
list($rtype, $to_phid, $to_type, $time) = $relationship; | list($rtype, $to_phid, $to_type, $time) = $relationship; | ||||
$spec['relationship'][$rtype][] = array( | $spec['relationship'][$rtype][] = array( | ||||
'phid' => $to_phid, | 'phid' => $to_phid, | ||||
'phidType' => $to_type, | 'phidType' => $to_type, | ||||
'when' => $time, | 'when' => $time, | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public function reconstructDocument($phid) { | ||||
return $doc; | return $doc; | ||||
} | } | ||||
private function buildSpec(PhabricatorSavedQuery $query) { | private function buildSpec(PhabricatorSavedQuery $query) { | ||||
$spec = array(); | $spec = array(); | ||||
$filter = array(); | $filter = array(); | ||||
if (strlen($query->getParameter('query'))) { | if (strlen($query->getParameter('query'))) { | ||||
$query_field = $query->getParameter('query_field', 'field.corpus'); | |||||
$spec[] = array( | $spec[] = array( | ||||
'match' => array( | 'match' => array( | ||||
'field.corpus' => array( | $query_field => array( | ||||
'operator' => 'and', | 'operator' => $query->getParameter('operator', 'and'), | ||||
'query' => $query->getParameter('query'), | 'query' => $query->getParameter('query'), | ||||
), | ), | ||||
), | ), | ||||
); | ); | ||||
} | } | ||||
$exclude = $query->getParameter('exclude'); | $exclude = $query->getParameter('exclude'); | ||||
if ($exclude) { | if ($exclude) { | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | try { | ||||
$query = clone $query; | $query = clone $query; | ||||
$query->setParameter( | $query->setParameter( | ||||
'query', | 'query', | ||||
addcslashes( | addcslashes( | ||||
$query->getParameter('query'), '+-&|!(){}[]^"~*?:\\')); | $query->getParameter('query'), '+-&|!(){}[]^"~*?:\\')); | ||||
$response = $this->executeRequest($uri, $this->buildSpec($query)); | $response = $this->executeRequest($uri, $this->buildSpec($query)); | ||||
} | } | ||||
$this->lastResponse = $response; | |||||
$phids = ipull($response['hits']['hits'], '_id'); | $phids = ipull($response['hits']['hits'], '_id'); | ||||
return $phids; | return $phids; | ||||
} | } | ||||
private function executeRequest($path, array $data, $is_write = false) { | private function executeRequest($path, array $data, $is_write = false) { | ||||
$uri = new PhutilURI($this->uri); | $uri = new PhutilURI($this->uri); | ||||
$uri->setPath($this->index); | $uri->setPath($this->index); | ||||
$uri->appendPath($path); | $uri->appendPath($path); | ||||
Show All 24 Lines |