Skip to content

Commit 2bf4e2e

Browse files
authored
Fix fulltext keys search (#4689)
1 parent 31912bc commit 2bf4e2e

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

modules/dkan_metastore/modules/dkan_metastore_search/src/QueryBuilderTrait.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,12 @@ private function getQuery(array $params, IndexInterface $index, QueryHelperInter
5555
* active as the second. Example: [$query, TRUE].
5656
*/
5757
private function setFullText(QueryInterface $query, array $params, IndexInterface $index): array {
58-
if (!isset($params['fulltext']) || empty($params['fulltext'])) {
58+
if (!isset($params['fulltext']) || empty($params['fulltext']) || empty($index->getFulltextFields())) {
5959
return [$query, FALSE];
6060
}
6161

62-
$fulltextFields = $index->getFulltextFields();
63-
64-
if (empty($fulltextFields)) {
65-
return [$query, FALSE];
66-
}
67-
68-
$conditions = [];
69-
foreach ($fulltextFields as $field) {
70-
$conditions[$field][] = $params['fulltext'];
71-
}
72-
73-
$query = $this->createConditionGroup($query, $conditions, 'OR');
62+
// Use Search API fulltext parsing.
63+
$query->keys($params['fulltext']);
7464

7565
return [$query, TRUE];
7666
}

modules/dkan_metastore/modules/dkan_metastore_search/tests/src/Unit/SearchTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
use Drupal\dkan_metastore\MetastoreService;
1111
use Drupal\search_api\IndexInterface;
1212
use Drupal\search_api\Item\Item;
13+
use Drupal\search_api\ParseMode\ParseModePluginManager;
14+
use Drupal\search_api\Plugin\search_api\parse_mode\Terms;
1315
use Drupal\search_api\Query\ConditionGroup;
16+
use Drupal\search_api\Query\Query;
1417
use Drupal\search_api\Query\QueryInterface;
1518
use Drupal\search_api\Query\ResultSet;
1619
use Drupal\search_api\Utility\QueryHelperInterface;
@@ -70,7 +73,19 @@ public function testSearch() {
7073
]));
7174
}
7275

73-
public function testSearchParameterWithComma() {
76+
/**
77+
* Test two things.
78+
*
79+
* 1. That commas in non-fulltext parameters get properly handled and don't
80+
* mess up parsing.
81+
* 2. That fulltext parameters get sent to keys()
82+
*
83+
* Because none of the functions we can test here return the query object, we
84+
* can't test that the keys() are being parsed as expected, but we should be
85+
* able to trust that if the input is sent to keys(), it will be handled
86+
* as expected by Search API.
87+
*/
88+
public function testSearchParameterWithCommaAndFulltext() {
7489
$options = (new Options())
7590
->add('dkan.metastore.service', MetastoreService::class)
7691
->add('entity_type.manager', EntityTypeManager::class)
@@ -88,6 +103,7 @@ public function testSearchParameterWithComma() {
88103
->add(QueryHelperInterface::class, 'createQuery', QueryInterface::class)
89104
->add(QueryInterface::class, 'execute', ResultSet::class)
90105
->add(QueryInterface::class, 'createConditionGroup', ConditionGroup::class)
106+
->add(QueryInterface::class, 'keys', null, 'keys')
91107
->add(ConditionGroup::class, 'addCondition', null, 'condition_group');
92108

93109
\Drupal::setContainer($container->getMock());
@@ -102,6 +118,15 @@ public function testSearchParameterWithComma() {
102118
'Steve, and someone else',
103119
$container->getStoredInput('condition_group')[1]
104120
);
121+
122+
// Now assert fulltext ends up getting sent to keys()
123+
$service->search([
124+
'fulltext' => 'Fulltext Param, "Steve, and someone else"',
125+
]);
126+
$this->assertEquals(
127+
'Fulltext Param, "Steve, and someone else"',
128+
$container->getStoredInput('keys')[0]
129+
);
105130
}
106131

107132
/**

0 commit comments

Comments
 (0)