Skip to content

Commit 959bb16

Browse files
author
epriestley
committedFeb 22, 2016
Allow Almanac services to be searched by substring
Summary: Ref T10246. Build an ngram index for Almanac services, and use it to support improved search. Test Plan: {F1121725} Reviewers: chad Reviewed By: chad Maniphest Tasks: T10246 Differential Revision: https://secure.phabricator.com/D15321
1 parent 3192747 commit 959bb16

8 files changed

+86
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE {$NAMESPACE}_almanac.almanac_servicename_ngrams (
2+
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
objectID INT UNSIGNED NOT NULL,
4+
ngram CHAR(3) NOT NULL COLLATE {$COLLATE_TEXT},
5+
KEY `key_object` (objectID),
6+
KEY `key_ngram` (ngram, objectID)
7+
) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$table = new AlmanacService();
4+
5+
foreach (new LiskMigrationIterator($table) as $service) {
6+
PhabricatorSearchWorker::queueDocumentForIndexing(
7+
$service->getPHID(),
8+
array(
9+
'force' => true,
10+
));
11+
}

‎src/__phutil_library_map__.php

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'AlmanacServiceEditController' => 'applications/almanac/controller/AlmanacServiceEditController.php',
9090
'AlmanacServiceEditor' => 'applications/almanac/editor/AlmanacServiceEditor.php',
9191
'AlmanacServiceListController' => 'applications/almanac/controller/AlmanacServiceListController.php',
92+
'AlmanacServiceNameNgrams' => 'applications/almanac/storage/AlmanacServiceNameNgrams.php',
9293
'AlmanacServicePHIDType' => 'applications/almanac/phid/AlmanacServicePHIDType.php',
9394
'AlmanacServiceQuery' => 'applications/almanac/query/AlmanacServiceQuery.php',
9495
'AlmanacServiceSearchEngine' => 'applications/almanac/query/AlmanacServiceSearchEngine.php',
@@ -4082,12 +4083,14 @@
40824083
'PhabricatorProjectInterface',
40834084
'AlmanacPropertyInterface',
40844085
'PhabricatorDestructibleInterface',
4086+
'PhabricatorNgramsInterface',
40854087
),
40864088
'AlmanacServiceController' => 'AlmanacController',
40874089
'AlmanacServiceDatasource' => 'PhabricatorTypeaheadDatasource',
40884090
'AlmanacServiceEditController' => 'AlmanacServiceController',
40894091
'AlmanacServiceEditor' => 'PhabricatorApplicationTransactionEditor',
40904092
'AlmanacServiceListController' => 'AlmanacServiceController',
4093+
'AlmanacServiceNameNgrams' => 'PhabricatorSearchNgrams',
40914094
'AlmanacServicePHIDType' => 'PhabricatorPHIDType',
40924095
'AlmanacServiceQuery' => 'AlmanacQuery',
40934096
'AlmanacServiceSearchEngine' => 'PhabricatorApplicationSearchEngine',

‎src/applications/almanac/editor/AlmanacServiceEditor.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public function getEditorObjectsDescription() {
1111
return pht('Almanac Service');
1212
}
1313

14+
protected function supportsSearch() {
15+
return true;
16+
}
17+
1418
public function getTransactionTypes() {
1519
$types = parent::getTransactionTypes();
1620

‎src/applications/almanac/query/AlmanacServiceQuery.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public function withNameSuffix($suffix) {
5454
return $this;
5555
}
5656

57+
public function withNameNgrams($ngrams) {
58+
return $this->withNgramsConstraint(
59+
new AlmanacServiceNameNgrams(),
60+
$ngrams);
61+
}
62+
5763
public function needBindings($need_bindings) {
5864
$this->needBindings = $need_bindings;
5965
return $this;
@@ -66,7 +72,7 @@ protected function loadPage() {
6672
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
6773
$joins = parent::buildJoinClauseParts($conn);
6874

69-
if ($this->devicePHIDs !== null) {
75+
if ($this->shouldJoinBindingTable()) {
7076
$joins[] = qsprintf(
7177
$conn,
7278
'JOIN %T binding ON service.phid = binding.servicePHID',
@@ -178,6 +184,18 @@ protected function didFilterPage(array $services) {
178184
return parent::didFilterPage($services);
179185
}
180186

187+
private function shouldJoinBindingTable() {
188+
return ($this->devicePHIDs !== null);
189+
}
190+
191+
protected function shouldGroupQueryResultRows() {
192+
if ($this->shouldJoinBindingTable()) {
193+
return true;
194+
}
195+
196+
return parent::shouldGroupQueryResultRows();
197+
}
198+
181199
protected function getPrimaryTableAlias() {
182200
return 'service';
183201
}

‎src/applications/almanac/query/AlmanacServiceSearchEngine.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@ public function newQuery() {
1616
}
1717

1818
public function newResultObject() {
19-
// NOTE: We need to attach a service type in order to generate custom
20-
// field definitions.
21-
return AlmanacService::initializeNewService()
22-
->attachServiceType(new AlmanacCustomServiceType());
19+
return AlmanacService::initializeNewService();
2320
}
2421

2522
protected function buildQueryFromParameters(array $map) {
2623
$query = $this->newQuery();
2724

25+
if ($map['match'] !== null) {
26+
$query->withNameNgrams($map['match']);
27+
}
28+
2829
return $query;
2930
}
3031

3132

3233
protected function buildCustomSearchFields() {
33-
return array();
34+
return array(
35+
id(new PhabricatorSearchTextField())
36+
->setLabel(pht('Name Contains'))
37+
->setKey('match')
38+
->setDescription(pht('Search for services by name substring.')),
39+
);
3440
}
3541

3642
protected function getURI($path) {

‎src/applications/almanac/storage/AlmanacService.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ final class AlmanacService
88
PhabricatorApplicationTransactionInterface,
99
PhabricatorProjectInterface,
1010
AlmanacPropertyInterface,
11-
PhabricatorDestructibleInterface {
11+
PhabricatorDestructibleInterface,
12+
PhabricatorNgramsInterface {
1213

1314
protected $name;
1415
protected $nameIndex;
@@ -231,4 +232,15 @@ public function destroyObjectPermanently(
231232
$this->delete();
232233
}
233234

235+
236+
/* -( PhabricatorNgramInterface )------------------------------------------ */
237+
238+
239+
public function newNgrams() {
240+
return array(
241+
id(new AlmanacServiceNameNgrams())
242+
->setValue($this->getName()),
243+
);
244+
}
245+
234246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
final class AlmanacServiceNameNgrams
4+
extends PhabricatorSearchNgrams {
5+
6+
public function getNgramKey() {
7+
return 'servicename';
8+
}
9+
10+
public function getColumnName() {
11+
return 'name';
12+
}
13+
14+
public function getApplicationName() {
15+
return 'almanac';
16+
}
17+
18+
}

0 commit comments

Comments
 (0)
Failed to load comments.