/** * Merge in other spellcheck information. * * @param Spellcheck $spellcheck Other spellcheck information * * @return void */ public function mergeWith(Spellcheck $spellcheck) { // Merge primary suggestions: $this->terms->uksort([$this, 'compareTermLength']); foreach ($spellcheck as $term => $info) { if (!$this->contains($term)) { $this->terms->offsetSet($term, $info); } } // Store secondary suggestions in case merge yielded non-useful // result set: if (!$this->secondary) { $this->secondary = $spellcheck; } else { $this->secondary->mergeWith($spellcheck); } }
protected function aggregateSpellcheck(Spellcheck $spellcheck, $query) { /* while (next($this->dictionaries) !== false) { $params = new ParamBag(); $params->set('spellcheck', 'true'); $params->set('spellcheck.dictionary', current($this->dictionaries)); $queryObj = new Query($query, 'AllFields'); $collection = $this->backend->search($queryObj, 0, 0, $params); $spellcheck->mergeWith($collection->getSpellcheck()); } */ foreach ($this->dictionaries as $dictionary) { $params = new ParamBag(); $params->set('spellcheck', 'true'); $params->set('spellcheck.dictionary', $dictionary); $queryObj = new Query($query, 'AllFields'); $collection = $this->backend->search($queryObj, 0, 0, $params); $spellcheck->mergeWith($collection->getSpellcheck()); } }
/** * Test getQuery() * * @return void */ public function testGetQuery() { $s = new Spellcheck([], 'test'); $this->assertEquals('test', $s->getQuery()); }
/** * Submit requests for more spelling suggestions. * * @param Spellcheck $spellcheck Aggregating spellcheck object * @param string $query Spellcheck query * * @return void */ protected function aggregateSpellcheck(Spellcheck $spellcheck, $query) { while (next($this->dictionaries) !== false) { $params = new ParamBag(); $params->set('spellcheck', 'true'); $params->set('spellcheck.dictionary', current($this->dictionaries)); $queryObj = new Query($query, 'AllFields'); try { $collection = $this->backend->search($queryObj, 0, 0, $params); $spellcheck->mergeWith($collection->getSpellcheck()); } catch (\Exception $ex) { if ($this->logger) { $this->logger->err("Exception thrown when aggregating spellcheck, ignoring.", array('exception' => $ex)); } } } }
/** * Get raw spelling suggestions for a query. * * @param Spellcheck $spellcheck Complete spellcheck information * @param AbstractQuery $query Query for which info should be retrieved * * @return array * @throws \Exception */ public function getSuggestions(Spellcheck $spellcheck, AbstractQuery $query) { $allSuggestions = []; foreach ($spellcheck as $term => $info) { if (!$this->shouldSkipTerm($query, $term, false) && ($suggestions = $this->formatAndFilterSuggestions($query, $info))) { $allSuggestions[$term] = ['freq' => $info['origFreq'], 'suggestions' => $suggestions]; } } // Fail over to secondary suggestions if primary failed: if (empty($allSuggestions) && ($secondary = $spellcheck->getSecondary())) { return $this->getSuggestions($secondary, $query); } return $allSuggestions; }