Skip to content

Commit e14a5f0

Browse files
authored
Add fur (#62)
* First step into fuzzy search * Add some fuzzy support * Fix test on the query side as well * Make the code a tad smaller in config.
1 parent f7db801 commit e14a5f0

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed

src/Queries/ElasticQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function addTerm(string $term, array $fields = [], int $boost = 1, $fuzzy
3131
$this->terms[] = [
3232
'text' => $term,
3333
'fields' => $fields,
34-
'boost' => $boost
34+
'boost' => $boost,
35+
'fuzzy' => $fuzzy
3536
];
3637

3738
return $this;

src/Queries/QueryBuilder.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,22 @@ private function getUserQuery(ElasticQuery|BaseQuery $query): array
178178
{
179179
$q = [];
180180
$terms = $query->getTerms();
181-
// Until wildcards work, just set it to match
181+
// Until wildcard||fuzziness works, just set it to match
182182
$type = 'match';
183-
if (!count($terms)) {
184-
$terms = ['text' => '*'];
185-
}
186183
foreach ($terms as $term) {
184+
if ($term['fuzzy'] !== null) {
185+
$type = 'fuzzy';
186+
}
187187
$q['must'][] = [
188188
$type => [
189189
'_text' => $term['text']
190190
]
191191
];
192-
$q = $this->getFieldBoosting($term, $type, $q);
192+
if ($type === 'match') {
193+
$q = $this->getFieldBoosting($term, $type, $q);
194+
}
195+
// reset to default of "must match"
196+
$type = 'match';
193197
}
194198

195199
return $q;

src/Tasks/ElasticConfigureTask.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class ElasticConfigureTask extends BuildTask
3636
{
3737
use LoggerTrait;
3838

39+
/**
40+
* @var bool[]
41+
*/
42+
public $result;
3943
/**
4044
* @var string URLSegment
4145
*/
@@ -76,7 +80,7 @@ public function __construct()
7680
* Run the config
7781
*
7882
* @param HTTPRequest $request
79-
* @return void|array
83+
* @return void
8084
* @throws NotFoundExceptionInterface
8185
*/
8286
public function run($request)
@@ -114,7 +118,7 @@ public function run($request)
114118
$this->extend('onAfterElasticConfigureTask');
115119

116120
if ($request->getVar('istest')) {
117-
return $result;
121+
$this->result = $result;
118122
}
119123
}
120124

@@ -135,25 +139,22 @@ protected function configureIndex($instance): Elasticsearch
135139

136140
$mappings = $this->convertForJSON($instanceConfig);
137141

138-
$body['index'] = $indexName;
142+
$body = ['index' => $indexName];
139143
$client = $this->service->getClient();
140144

141145
$method = $this->getMethod($instance);
142146
$msg = "%s index %s";
143-
if ($method === 'update') {
144-
$body['body'] = $mappings;
145-
$msg = sprintf($msg, 'Updating', $indexName);
146-
DB::alteration_message($msg);
147-
$this->getLogger()->info($msg);
148-
149-
return $client->indices()->putMapping($body);
147+
$msgType = 'Updating';
148+
if ($method === 'create') {
149+
$mappings = ['mappings' => $mappings];
150+
$msgType = 'Creating';
150151
}
151-
$body['body']['mappings'] = $mappings;
152-
$msg = sprintf($msg, 'Creating', $indexName);
152+
$body['body'] = $mappings;
153+
$msg = sprintf($msg, $msgType, $indexName);
153154
DB::alteration_message($msg);
154155
$this->getLogger()->info($msg);
155156

156-
return $client->indices()->create($body);
157+
return $client->indices()->$method($body);
157158
}
158159

159160
/**
@@ -223,7 +224,7 @@ protected function getMethod(ElasticIndex $index): string
223224
$check = $index->indexExists();
224225

225226
if ($check) {
226-
return 'update';
227+
return 'putMapping';
227228
}
228229

229230
return 'create';

tests/unit/Queries/ElasticQueryTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public function testTerms()
1616
[
1717
'text' => 'Testing',
1818
'fields' => [],
19-
'boost' => 1
19+
'boost' => 1,
20+
'fuzzy' => null
2021
]
2122
], $query->getTerms());
2223

@@ -26,12 +27,38 @@ public function testTerms()
2627
[
2728
'text' => 'Testing',
2829
'fields' => [],
29-
'boost' => 1
30+
'boost' => 1,
31+
'fuzzy' => null
3032
],
3133
[
3234
'text' => 'Test 2',
3335
'fields' => ['SiteTree.Title'],
34-
'boost' => 2
36+
'boost' => 2,
37+
'fuzzy' => null
38+
]
39+
], $query->getTerms());
40+
41+
$query->addTerm('Fuzzy test', [], 1, true);
42+
43+
44+
$this->assertEquals([
45+
[
46+
'text' => 'Testing',
47+
'fields' => [],
48+
'boost' => 1,
49+
'fuzzy' => null
50+
],
51+
[
52+
'text' => 'Test 2',
53+
'fields' => ['SiteTree.Title'],
54+
'boost' => 2,
55+
'fuzzy' => null
56+
],
57+
[
58+
'text' => 'Fuzzy test',
59+
'fields' => [],
60+
'boost' => 1,
61+
'fuzzy' => true
3562
]
3663
], $query->getTerms());
3764
}

tests/unit/Queries/QueryBuilderTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testBuildQuery()
8686

8787
$this->assertEquals('Home', $query->getFilters()['SiteTree.Title']);
8888
$this->assertEquals('Away', $query->getOrFilters()['SiteTree.Title']);
89-
$this->assertEquals([['text' => 'TestSearch', 'fields' => [], 'boost' => 1]], $query->getTerms());
89+
$this->assertEquals([['text' => 'TestSearch', 'fields' => [], 'boost' => 1, 'fuzzy' => null]], $query->getTerms());
9090

9191
$resultQuery = QueryBuilder::buildQuery($query, $idx);
9292

@@ -129,5 +129,11 @@ public function testBuildQuery()
129129
$resultQuery = QueryBuilder::buildQuery($query, $idx);
130130

131131
$this->assertEquals(['Title' => 'asc'], $resultQuery['body']['sort']);
132+
133+
$query->addTerm('Filt', [], 1, 1);
134+
135+
$resultQuery = QueryBuilder::buildQuery($query, $idx);
136+
137+
$this->assertArrayHasKey('fuzzy', $resultQuery['body']['query']['bool']['must'][2]);
132138
}
133139
}

tests/unit/Tasks/ElasticConfigureTaskTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function testRun()
1717

1818
$this->assertInstanceOf(ElasticCoreService::class, $task->getService());
1919

20-
$run = $task->run(new HTTPRequest('GET', 'dev/tasks/ElasticConfigureTask', ['istest' => self::$is_running_test]));
20+
$task->run(new HTTPRequest('GET', 'dev/tasks/ElasticConfigureTask', ['istest' => self::$is_running_test]));
2121

22-
$this->assertNotContains(false, $run);
22+
$this->assertNotContains(false, $task->result);
2323

2424
// Same, but with clearing
25-
$run = $task->run(new HTTPRequest('GET', 'dev/tasks/ElasticConfigureTask', ['istest' => self::$is_running_test, 'clear' => true]));
25+
$task->run(new HTTPRequest('GET', 'dev/tasks/ElasticConfigureTask', ['istest' => self::$is_running_test, 'clear' => true]));
2626

27-
$this->assertNotContains(false, $run);
27+
$this->assertNotContains(false, $task->result);
2828

2929
}
3030
}

0 commit comments

Comments
 (0)