Search: countries named in the phrase filter by company country (Q6)
"pigment Italie" only matched "pigment": the country was neither a tag nor in the AND query fields, and the model matched Italian-sounding names. Country names and demonyms (FR/EN, 259 countries, CountryForms generated from Drupal and Wikidata) are detected in the phrase, taken out of the keywords and of the model phrase, and become a condition: manufacturer or distributor country, or tag of the same name. Results: exact matches and neighbours of the country first, then the same search without country. Index: manufacturer_country and distributor_country (reference paths, reindexed when a company changes). A country alone lists its records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,9 @@ final class NeighbourSearch {
|
||||
* Language of the results; the current language by default.
|
||||
* @param int $limit
|
||||
* Number of neighbours (topK).
|
||||
* @param callable|null $alter
|
||||
* Adds conditions to the Search API query, applied by Solr as pre-filters
|
||||
* of the kNN search (countries of materio_sapi, Q6).
|
||||
*
|
||||
* @return array<int, float>
|
||||
* Scores keyed by nid, best first, none when materio_ai is switched off.
|
||||
@@ -63,12 +66,12 @@ final class NeighbourSearch {
|
||||
* @throws \Drupal\search_api\SearchApiException
|
||||
* When Solr fails.
|
||||
*/
|
||||
public function search(string $phrase, ?string $langcode = NULL, int $limit = 100): array {
|
||||
public function search(string $phrase, ?string $langcode = NULL, int $limit = 100, ?callable $alter = NULL): array {
|
||||
// Switched off: no text is sent to the model, keyword results only.
|
||||
if (!$this->embedder->isEnabled()) {
|
||||
return [];
|
||||
}
|
||||
return $this->searchVector($this->embedder->embed($phrase), $langcode, $limit);
|
||||
return $this->searchVector($this->embedder->embed($phrase), $langcode, $limit, $alter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +83,7 @@ final class NeighbourSearch {
|
||||
* @return array<int, float>
|
||||
* Scores keyed by nid, best first.
|
||||
*/
|
||||
public function searchVector(array $vector, ?string $langcode = NULL, int $limit = 100): array {
|
||||
public function searchVector(array $vector, ?string $langcode = NULL, int $limit = 100, ?callable $alter = NULL): array {
|
||||
$dimension = (int) $this->configFactory->get('materio_ai.settings')->get('dimension');
|
||||
if (count($vector) !== $dimension || $limit <= 0) {
|
||||
return [];
|
||||
@@ -94,6 +97,9 @@ final class NeighbourSearch {
|
||||
// Only the item IDs are needed.
|
||||
$query->setOption('search_api_retrieved_field_values', ['search_api_id' => 'search_api_id']);
|
||||
$query->addTag(self::TAG);
|
||||
if ($alter) {
|
||||
$alter($query);
|
||||
}
|
||||
|
||||
$scores = [];
|
||||
foreach ($query->execute() as $item_id => $item) {
|
||||
|
||||
Reference in New Issue
Block a user