Source of file FulltextSearchExtension.php
Size: 5,397 Bytes - Last Modified: 2021-12-24T06:47:24+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/FulltextSearchExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | <?php /** * Class FulltextSearchExtension|Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension provide help for * migrating from the old module * * @package Firesphere\Solr\Compatibility * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy */ namespace Firesphere\SolrCompatibility\Extensions; use Firesphere\SolrSearch\Indexes\BaseIndex; use Firesphere\SolrSearch\Queries\BaseQuery; use Firesphere\SolrSearch\Results\SearchResult; use GuzzleHttp\Exception\GuzzleException; use LogicException; use ReflectionException; use SilverStripe\Core\Extension; use SilverStripe\Dev\Debug; use SilverStripe\ORM\ValidationException; use SilverStripe\View\ArrayData; /** * Class \Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension * Backward compatibility stubs for the Full text search module * * @package Firesphere\Solr\Compatibility * @property BaseIndex|FulltextSearchExtension $owner */ class FulltextSearchExtension extends Extension { /** * Generate a yml version of the init method indexes */ public function initToYml(): void { // @codeCoverageIgnoreStart if (function_exists('yaml_emit')) { /** @var BaseIndex $owner */ $owner = $this->owner; $result = [ BaseIndex::class => [ $owner->getIndexName() => [ 'Classes' => $owner->getClasses(), 'FulltextFields' => array_values($owner->getFulltextFields()), 'SortFields' => $owner->getSortFields(), 'FilterFields' => $owner->getFilterFields(), 'BoostedFields' => $owner->getBoostedFields(), 'CopyFields' => $owner->getCopyFields(), 'DefaultField' => $owner->getDefaultField(), 'FacetFields' => $owner->getFacetFields(), 'StoredFields' => $owner->getStoredFields(), ], ], ]; Debug::dump(yaml_emit($result)); return; } // @codeCoverageIgnoreEnd throw new LogicException('yaml-emit PHP module missing'); } /** * Convert the SearchResult class to a Full text search compatible ArrayData * * @param SearchResult|ArrayData $results */ public function updateSearchResults(&$results): void { $data = [ 'Matches' => $results->getPaginatedMatches(), 'Facets' => $results->getFacets(), 'Highlights' => $results->getHighlight(), 'Spellcheck' => $results->getSpellcheck(), 'Suggestion' => $results->getCollatedSpellcheck(), 'SuggestionNice' => $this->getCollatedNice($results->getCollatedSpellcheck()), 'SuggestionQueryString' => $results->getCollatedSpellcheck(), 'Original' => $results ]; // Override the results with an FTS compatible feature list $results = ArrayData::create($data); } /** * Create a spellcheck string that's not the literal collation with Solr query parts * * @param string $spellcheck * @return string mixed */ protected function getCollatedNice($spellcheck): string { return str_replace(' +', ' ', $spellcheck); } /** * Convert the old search method to the new BaseIndex doSearch methods * * @param BaseQuery $query * @param int $start deprecated in favour of $query, exists for backward compatibility with FTS * @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS * @param array $params deprecated in favour of $query, exists for backward compatibility with FTS * @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS * @return SearchResult|ArrayData|mixed * @throws ValidationException * @throws GuzzleException * @throws ReflectionException * @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query */ public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null) { $query->getStart() === $start ?: $query->setStart($start); $query->getRows() === $limit ?: $query->setRows($limit); $query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck); if (isset($params['fq']) && !count($query->getFields())) { $query->addField($params['fq']); } /** @var BaseIndex $owner */ $owner = $this->owner; return $owner->doSearch($query); } /** * Add a Fulltext Field * * @param bool $includeSubclasses Compatibility mode, not actually used * @throws ReflectionException * @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore */ public function addFulltextFields($includeSubclasses = true) { /** @var BaseIndex $owner */ $owner = $this->owner; $owner->addAllFulltextFields(); } } |