public function testSuggestNoResults()
 {
     $termSuggest = new Term('suggest1', '_all');
     $termSuggest->setText('Foobar')->setSize(4);
     $result = $this->_index->search($termSuggest);
     $this->assertEquals(1, $result->countSuggests());
     // Assert that no suggestions were returned
     $suggests = $result->getSuggests();
     $this->assertEquals(0, sizeof($suggests['suggest1'][0]['options']));
 }
Ejemplo n.º 2
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;
 }