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 $timeout; | private $timeout; | ||||
public function __construct($uri) { | public function __construct($uri, $index) { | ||||
$this->uri = $uri; | $this->uri = $uri; | ||||
$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() { | ||||
Show All 29 Lines | foreach ($doc->getRelationshipData() as $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, | ||||
); | ); | ||||
} | } | ||||
$this->executeRequest( | $this->executeRequest( | ||||
"/phabricator/{$type}/{$phid}/", | "/{$type}/{$phid}/", | ||||
$spec, | $spec, | ||||
$is_write = true); | $is_write = true); | ||||
} | } | ||||
public function reconstructDocument($phid) { | public function reconstructDocument($phid) { | ||||
$type = phid_get_type($phid); | $type = phid_get_type($phid); | ||||
$response = $this->executeRequest("/phabricator/{$type}/{$phid}", array()); | $response = $this->executeRequest("/{$type}/{$phid}", array()); | ||||
if (empty($response['exists'])) { | if (empty($response['exists'])) { | ||||
return null; | return null; | ||||
} | } | ||||
$hit = $response['_source']; | $hit = $response['_source']; | ||||
$doc = new PhabricatorSearchAbstractDocument(); | $doc = new PhabricatorSearchAbstractDocument(); | ||||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | final class PhabricatorSearchEngineElastic extends PhabricatorSearchEngine { | ||||
public function executeSearch(PhabricatorSavedQuery $query) { | public function executeSearch(PhabricatorSavedQuery $query) { | ||||
$types = $query->getParameter('types'); | $types = $query->getParameter('types'); | ||||
if (!$types) { | if (!$types) { | ||||
$types = array_keys( | $types = array_keys( | ||||
PhabricatorSearchApplicationSearchEngine::getIndexableDocumentTypes()); | PhabricatorSearchApplicationSearchEngine::getIndexableDocumentTypes()); | ||||
} | } | ||||
// Don't use '/phabricator/_search' for the case that there is something | // Don't use '/_search' for the case that there is something | ||||
// else in the index (for example if 'phabricator' is only an alias to | // else in the index (for example if 'phabricator' is only an alias to | ||||
// some bigger index). | // some bigger index). Use '/$types/_search' instead. | ||||
$uri = '/phabricator/'.implode(',', $types).'/_search'; | $uri = '/'.implode(',', $types).'/_search'; | ||||
try { | try { | ||||
$response = $this->executeRequest($uri, $this->buildSpec($query)); | $response = $this->executeRequest($uri, $this->buildSpec($query)); | ||||
} catch (HTTPFutureResponseStatusHTTP $ex) { | } catch (HTTPFutureResponseStatusHTTP $ex) { | ||||
// elasticsearch probably uses Lucene query syntax: | // elasticsearch probably uses Lucene query syntax: | ||||
// http://lucene.apache.org/core/3_6_1/queryparsersyntax.html | // http://lucene.apache.org/core/3_6_1/queryparsersyntax.html | ||||
// Try literal search if operator search fails. | // Try literal search if operator search fails. | ||||
if (!strlen($query->getParameter('query'))) { | if (!strlen($query->getParameter('query'))) { | ||||
throw $ex; | throw $ex; | ||||
} | } | ||||
$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)); | ||||
} | } | ||||
$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->appendPath($path); | |||||
$data = json_encode($data); | $data = json_encode($data); | ||||
epriestley: This should probably be:
$uri = new PhutilURI($this->uri);
$uri->setPath($this->index)… | |||||
$uri->setPath($path); | |||||
$future = new HTTPSFuture($uri, $data); | $future = new HTTPSFuture($uri, $data); | ||||
if ($is_write) { | if ($is_write) { | ||||
$future->setMethod('PUT'); | $future->setMethod('PUT'); | ||||
} | } | ||||
if ($this->getTimeout()) { | if ($this->getTimeout()) { | ||||
$future->setTimeout($this->getTimeout()); | $future->setTimeout($this->getTimeout()); | ||||
} | } | ||||
list($body) = $future->resolvex(); | list($body) = $future->resolvex(); | ||||
Show All 14 Lines |
This should probably be:
That should get all the various missing-trailing-slash cases correct, I think.