Esempio n. 1
0
 /**
  * @param string $search
  * @return Suggest
  */
 private function getSuggests($search)
 {
     $suggest = new Suggest();
     $suggest->setGlobalText($search);
     $termSuggest = new Term('termSuggest', 'text');
     $suggest->addSuggestion($termSuggest);
     return $suggest;
 }
 public function testSuggestResults()
 {
     $suggest = new Suggest();
     $suggest1 = new Term('suggest1', '_all');
     $suggest->addSuggestion($suggest1->setText('Foor seach'));
     $suggest2 = new Term('suggest2', '_all');
     $suggest->addSuggestion($suggest2->setText('Girhub'));
     $result = $this->_index->search($suggest);
     $this->assertEquals(2, $result->countSuggests());
     $suggests = $result->getSuggests();
     // Ensure that two suggestion results are returned for suggest1
     $this->assertEquals(2, sizeof($suggests['suggest1']));
     $this->assertEquals('github', $suggests['suggest2'][0]['options'][0]['text']);
     $this->assertEquals('food', $suggests['suggest1'][0]['options'][0]['text']);
 }
 public function testPhraseSuggest()
 {
     $suggest = new Suggest();
     $phraseSuggest = new Phrase('suggest1', 'text');
     $phraseSuggest->setText("elasticsearch is bansai coor");
     $phraseSuggest->setAnalyzer("simple")->setHighlight("<suggest>", "</suggest>")->setStupidBackoffSmoothing(0.4);
     $phraseSuggest->addCandidateGenerator(new DirectGenerator("text"));
     $suggest->addSuggestion($phraseSuggest);
     $result = $this->_index->search($suggest);
     $suggests = $result->getSuggests();
     // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
     $this->assertEquals(3, sizeof($suggests['suggest1'][0]['options']));
     $this->assertEquals("elasticsearch is <suggest>bonsai cool</suggest>", $suggests['suggest1'][0]['options'][0]['highlighted']);
     $this->assertEquals("elasticsearch is bonsai cool", $suggests['suggest1'][0]['options'][0]['text']);
 }
Esempio n. 4
0
 /**
  * Create with non suggest test.
  *
  * @group functional
  */
 public function testCreateWithNonSuggest()
 {
     try {
         Suggest::create(new BoolFilter());
         $this->fail();
     } catch (NotImplementedException $e) {
     }
 }
Esempio n. 5
0
 /**
  * @group unit
  */
 public function testSetSuggestToArrayCast()
 {
     $query = new Query();
     $suggest = new Suggest();
     $suggest->setGlobalText('text');
     $query->setSuggest($suggest);
     $suggest->setGlobalText('another text');
     $anotherQuery = new Query();
     $anotherQuery->setSuggest($suggest);
     $this->assertEquals($query->toArray(), $anotherQuery->toArray());
 }
Esempio n. 6
0
 /**
  * Add a suggest term
  *
  * @param  \Elastica\Suggest $suggest suggestion object
  */
 public function setSuggest(Suggest $suggest)
 {
     $this->addParam(NULL, $suggest->toArray());
     $this->_suggest = 1;
 }
Esempio n. 7
0
 /**
  * Search suggest.
  *
  * @param string $type Post type
  * @param int $post Post ID to get all suggestions
  * @param array $tags Array contains all post tags
  * @return array $search Combine of all results, total and aggregations
  *
  * @since 3.0.0
  */
 public function searchSuggest($type, $post, $tags)
 {
     //Check page
     if (!is_search()) {
         return;
     }
     //Return array
     $return = array('post' => $post, 'tags' => $tags, 'total' => 0, 'results' => array());
     //Check request
     if (empty($post)) {
         return $return;
     }
     //Get query vars
     $results = array();
     //Get Elasticsearch datas
     $index = $this->getIndex();
     //Check index
     if (null === $index || empty($index)) {
         return $return;
     }
     //Create suggestion
     $es_suggest = new Suggest();
     //Iterate on all tags
     foreach ($tags as $k => $tag) {
         //CReate Term with options
         $es_term = new SuggestTerm('tags_suggest_' . $k, '_all');
         $es_term->setText($tag);
         $es_term->setSize(5);
         $es_term->setAnalyzer('simple');
         //Add Term to current suggestion
         $es_suggest->addSuggestion($es_term);
     }
     //Search!
     $es_resultset = $index->search($es_suggest);
     //Retrieve data
     $es_results = $es_resultset->getSuggests();
     //Check results
     if (null == $es_results || empty($es_results)) {
         return $return;
     }
     //Iterate to retrieve all IDs
     foreach ($es_results as $res) {
         //Check suggestions
         if (empty($res[0]['options'])) {
             continue;
         }
         //Iterate on all options
         foreach ($res[0]['options'] as $opt) {
             //Save datas
             $results[$opt['text']] = array('score' => $opt['score'], 'freq' => $opt['freq']);
         }
     }
     //Get total
     $total = $es_resultset->getTotalHits();
     //Return everything
     $return = array('post' => $post, 'tags' => $tags, 'total' => $total, 'results' => $results);
     return $return;
 }
Esempio n. 8
0
 /**
  * Add a suggest term
  *
  * @param  \Elastica\Suggest $suggest suggestion object
  * @return $this
  */
 public function setSuggest(Suggest $suggest)
 {
     $this->setParams(array_merge($this->getParams(), $suggest->toArray()));
     $this->_suggest = 1;
     return $this;
 }