Compare commits
1
Commits
58ce1b0513
...
8229a80c57
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8229a80c57 |
@@ -76,6 +76,25 @@ class Base extends ControllerBase {
|
|||||||
*/
|
*/
|
||||||
protected $phrase = '';
|
protected $phrase = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Material references found in the phrase (W0117…), uppercase (H4).
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $references = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pattern of a material reference: family letter and four digits.
|
||||||
|
*/
|
||||||
|
public const REFERENCE_PATTERN = '/\\b[WTRPCMFGSO]\\d{4}\\b/iu';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The reference query, run before the others.
|
||||||
|
*
|
||||||
|
* @var \Drupal\search_api\Query\QueryInterface|null
|
||||||
|
*/
|
||||||
|
protected $reference_query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Most meaningful words for which the phrase and AND queries run (H2, a).
|
* Most meaningful words for which the phrase and AND queries run (H2, a).
|
||||||
*
|
*
|
||||||
@@ -94,14 +113,24 @@ class Base extends ControllerBase {
|
|||||||
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||||
$this->index = Index::load('database');
|
$this->index = Index::load('database');
|
||||||
|
|
||||||
$words = KeywordText::words($this->phrase, $lang);
|
// References first, as exact matches, whatever their case (H4, B2).
|
||||||
|
preg_match_all(self::REFERENCE_PATTERN, $this->phrase, $matches);
|
||||||
|
$this->references = array_values(array_unique(array_map('mb_strtoupper', $matches[0])));
|
||||||
|
$exact = $this->references ? $this->referenceQuery($this->references, $lang) : [];
|
||||||
|
if ($this->isReferenceSearch()) {
|
||||||
|
$this->exactematch_count = count($exact);
|
||||||
|
$this->results = ['uuids' => array_column($exact, 1), 'nids' => array_column($exact, 0)];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keys without the references (« bois R0208 » searches « bois »).
|
||||||
|
$words = KeywordText::words(preg_replace(self::REFERENCE_PATTERN, ' ', $this->phrase) ?? $this->phrase, $lang);
|
||||||
// Only stop words: search the phrase as typed, as before.
|
// Only stop words: search the phrase as typed, as before.
|
||||||
$keys = $words ? implode(' ', $words) : implode(' ', $this->keys);
|
$keys = $words ? implode(' ', $words) : implode(' ', $this->keys);
|
||||||
|
|
||||||
$exact = [];
|
|
||||||
if (count($words) <= self::SHORT_QUERY_WORDS) {
|
if (count($words) <= self::SHORT_QUERY_WORDS) {
|
||||||
// Exact material names (like "wood-skin"), then all words.
|
// Exact material names (like "wood-skin"), then all words.
|
||||||
$exact = $this->phraseQuery($keys, $lang);
|
$exact += $this->phraseQuery($keys, $lang);
|
||||||
$exact += $this->andQuery($keys, $lang);
|
$exact += $this->andQuery($keys, $lang);
|
||||||
}
|
}
|
||||||
$this->exactematch_count = count($exact);
|
$this->exactematch_count = count($exact);
|
||||||
@@ -113,6 +142,34 @@ class Base extends ControllerBase {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nodes whose reference is one of the given ones.
|
||||||
|
*
|
||||||
|
* field_reference is case-sensitive: references are uppercased (B2).
|
||||||
|
*
|
||||||
|
* @param string[] $references
|
||||||
|
* Uppercase references.
|
||||||
|
*
|
||||||
|
* @return array<int, array{0: mixed, 1: string}>
|
||||||
|
* [nid, uuid] keyed by nid, in result order.
|
||||||
|
*/
|
||||||
|
private function referenceQuery(array $references, string $lang): array {
|
||||||
|
$this->reference_query = $this->keywordQuery('terms', 'OR', '', $lang, 'materio_sapi_search_reference_query');
|
||||||
|
$conditions = $this->reference_query->createConditionGroup('OR');
|
||||||
|
foreach ($references as $reference) {
|
||||||
|
$conditions->addCondition('field_reference', $reference);
|
||||||
|
}
|
||||||
|
$this->reference_query->addConditionGroup($conditions);
|
||||||
|
return $this->collect($this->reference_query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the phrase is only references: no need for the model (H4, H5).
|
||||||
|
*/
|
||||||
|
public function isReferenceSearch(): bool {
|
||||||
|
return $this->references && trim(preg_replace(self::REFERENCE_PATTERN, '', $this->phrase) ?? '', " \t,;") === '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The whole keys in the title.
|
* The whole keys in the title.
|
||||||
*
|
*
|
||||||
@@ -166,7 +223,10 @@ class Base extends ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Any word, in every fulltext field; references like W0117 matched exactly.
|
* Any word, in every fulltext field.
|
||||||
|
*
|
||||||
|
* References are no longer a condition here (H4): it restricted the OR
|
||||||
|
* results to the referenced nodes (« bois R0208 » found nothing).
|
||||||
*
|
*
|
||||||
* @return array<int, array{0: mixed, 1: string}>
|
* @return array<int, array{0: mixed, 1: string}>
|
||||||
* [nid, uuid] keyed by nid, in result order.
|
* [nid, uuid] keyed by nid, in result order.
|
||||||
@@ -177,14 +237,6 @@ class Base extends ControllerBase {
|
|||||||
// Hyphenated words as separate words, as the direct mode did: in terms mode
|
// Hyphenated words as separate words, as the direct mode did: in terms mode
|
||||||
// « bio-based » would be a quoted phrase (the phrase query keeps it whole).
|
// « bio-based » would be a quoted phrase (the phrase query keeps it whole).
|
||||||
$this->or_query = $this->keywordQuery('terms', 'OR', preg_replace('/(?<=\p{L})-(?=\p{L})/u', ' ', $keys), $lang, 'materio_sapi_search_or_query');
|
$this->or_query = $this->keywordQuery('terms', 'OR', preg_replace('/(?<=\p{L})-(?=\p{L})/u', ' ', $keys), $lang, 'materio_sapi_search_or_query');
|
||||||
|
|
||||||
if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $keys, $matches)) {
|
|
||||||
$ref_conditions = $this->or_query->createConditionGroup('OR');
|
|
||||||
foreach ($matches[0] as $value) {
|
|
||||||
$ref_conditions->addCondition('field_reference', $value);
|
|
||||||
}
|
|
||||||
$this->or_query->addConditionGroup($ref_conditions);
|
|
||||||
}
|
|
||||||
return $this->collect($this->or_query);
|
return $this->collect($this->or_query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user