Beispiel #1
0
 /**
  * @httpMethod GET
  * @path /
  */
 public function doIndex()
 {
     $url = new Url($this->base->getSelf());
     $count = $url->getParam('count') > 0 ? $url->getParam('count') : 8;
     $count = $count > 16 ? 16 : $count;
     $search = $this->get->search('string');
     if (!empty($search)) {
         $search = strlen($search) > 64 ? substr($search, 0, 64) : $search;
         $queryString = new QueryString();
         //$queryString->setDefaultOperator('AND');
         $queryString->setQuery($search);
         $query = new Query();
         $query->setQuery($queryString);
         $query->setFrom($url->getParam('startIndex'));
         $query->setLimit($count);
         $query->setHighlight(array('pre_tags' => array('<mark>'), 'post_tags' => array('</mark>'), 'fields' => array('title' => new \stdClass(), 'content' => new \stdClass())));
         // get elasticsearch client
         $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port']));
         $index = $client->getIndex('amun');
         $searchResult = $index->search($query);
         $result = new ResultSet($searchResult->getTotalHits(), $url->getParam('startIndex'), $count);
         foreach ($searchResult as $row) {
             $data = $row->getData();
             $data['url'] = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . $data['path'];
             $data['date'] = new DateTime('@' . $data['date']);
             // if we have an highlite overwrite the title or content
             $highlights = $row->getHighlights();
             if (isset($highlights['title'])) {
                 $data['title'] = implode(' ... ', $highlights['title']);
             }
             if (isset($highlights['content'])) {
                 $data['content'] = implode(' ... ', $highlights['content']);
             }
             $result->addData($data);
         }
         $this->template->assign('resultSearch', $result);
         $paging = new Paging($url, $result);
         $this->template->assign('pagingSearch', $paging, 0);
         return $result;
     }
 }
Beispiel #2
0
 /**
  * method to search log details
  *
  * @param string $operator    the operator for the query
  * @param string $words       the words
  * @param string $names_types the types to search
  *
  * @return \Elastica\ResultSet
  */
 function searchQueryLogDetails($operator, $words, $names_types = null)
 {
     $words = CmbString::normalizeUtf8(stripcslashes($words));
     // Define a Query. We want a string query.
     $elasticaQueryString = new QueryString();
     //'And' or 'Or' default : 'Or'
     $elasticaQueryString->setDefaultOperator($operator);
     $elasticaQueryString->setQuery($words);
     // Create the actual search object with some data.
     $elasticaQuery = new Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     //Search on the index.
     $index = CAppUI::conf("search index_name") . "_log";
     $this->_index = $this->loadIndex($index);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($this->_index);
     if ($names_types) {
         $search->addTypes($names_types);
     }
     $elasticaQuery->setFrom(0);
     // Where to start
     $elasticaQuery->setLimit(1000);
     return $search->search($elasticaQuery);
 }
Beispiel #3
0
 /**
  * Search contents.
  *
  * @return array $elasticsearches Combine of all results, total and aggregations
  *
  * @since 1.5.0
  */
 public function searchContents()
 {
     //Return array
     $return = array('query' => array('search' => '', 'type' => '', 'paged' => 0, 'perpage' => 0), 'total' => 0, 'types' => array(), 'results' => array());
     //Check page
     if (!is_search()) {
         return $return;
     }
     //Get query vars
     $request = isset($_REQUEST) ? $_REQUEST : array();
     $results = array();
     $types = array();
     //Check request
     if (empty($request)) {
         return $return;
     }
     //Get Elasticsearch datas
     $index = $this->getIndex();
     //Check index
     if (null === $index || empty($index)) {
         return $return;
     }
     //Get search datas
     $search = isset($request['s']) ? str_replace('\\"', '"', $request['s']) : '';
     //Return everything
     if (empty($search)) {
         return $return;
     }
     //Get search datas
     $type = isset($request['type']) ? $request['type'] : '';
     $paged = isset($request['paged']) && !empty($request['paged']) ? $request['paged'] - 1 : 0;
     $perpage = isset($request['perpage']) ? $request['perpage'] : TeaThemeOptions::getOption('posts_per_page', 10);
     //Build query string
     $es_querystring = new QueryString();
     //'And' or 'Or' default: 'Or'
     $es_querystring->setDefaultOperator('OR');
     $es_querystring->setQuery($search);
     //Create the actual search object with some data.
     $es_query = new Query();
     $es_query->setQuery($es_querystring);
     //Define options
     $es_query->setFrom($paged);
     //Start
     $es_query->setLimit($perpage);
     //How many
     //Search!
     $es_resultset = $index->search($es_query);
     //Retrieve data
     $es_results = $es_resultset->getResults();
     //Check results
     if (null == $es_results || empty($es_results)) {
         $return['query']['search'] = str_replace(' ', '+', $search);
         return $return;
     }
     //Iterate to retrieve all IDs
     foreach ($es_results as $res) {
         $typ = $res->getType();
         //Save type
         $types[$typ] = $typ;
         //Save datas
         $results[$typ][] = array('id' => $res->getId(), 'score' => $res->getScore(), 'source' => $res->getSource());
     }
     //Get total
     $total = $es_resultset->getTotalHits();
     //Return everything
     $return = array('query' => array('search' => str_replace(' ', '+', $search), 'type' => $type, 'paged' => $paged, 'perpage' => $perpage), 'total' => $total, 'types' => $types, 'results' => $results);
     return $return;
 }
Beispiel #4
0
 /**
  * simple search with an operator and words
  *
  * @param string  $operator    'And' or 'Or' default : 'Or'
  * @param string  $words       data
  * @param integer $start       the begining of the paging
  * @param integer $limit       the interval of the paging
  * @param array   $names_types the restrictive type(s) where the search take place.
  * @param bool    $aggregation parameter the search to be aggregated or not.
  *
  * @return \Elastica\ResultSet
  */
 function searchQueryString($operator, $words, $start = 0, $limit = 30, $names_types = null, $aggregation = false)
 {
     $words = CSearch::normalizeEncoding($words);
     // Define a Query. We want a string query.
     $queryString = new Elastica\Query\QueryString($words);
     $queryString->setDefaultOperator("and");
     // Create the actual search object with some data.
     $query = new Elastica\Query($queryString);
     //create aggregation
     if ($aggregation) {
         // on aggrège d'abord par class d'object référents
         // on effectue un sous aggrégation par id des objets référents.
         $agg_by_date = new CSearchAggregation("Terms", "date_log", "date", 10);
         $sub_agg_by_user = new CSearchAggregation("Terms", "user_id", "user_id", 10);
         $sub_agg_by_contexte = new CSearchAggregation("Terms", "contexte", "_type", 10);
         $sub_agg_by_user->_aggregation->addAggregation($sub_agg_by_contexte->_aggregation);
         $agg_by_date->_aggregation->addAggregation($sub_agg_by_user->_aggregation);
         $query->addAggregation($agg_by_date->_aggregation);
     } else {
         //  Pagination
         $query->setFrom($start);
         // Where to start
         $query->setLimit($limit);
     }
     //Highlight
     $query->setHighlight(array("fields" => array("body" => array("pre_tags" => array(" <em> <strong> "), "post_tags" => array(" </strong> </em>"), "fragment_size" => 80, "number_of_fragments" => 10))));
     //Search on the index.
     $index = CAppUI::conf("search index_name") . "_log";
     $index = $this->loadIndex($index);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($index);
     if ($names_types) {
         $search->addTypes($names_types);
     }
     return $search->search($query);
 }
Beispiel #5
0
 public function testDeltaSync()
 {
     // @TODO: ensure records flagged as deleted are removed from the index
     // delete and create index if it already exists
     // make index dirty with prepopulated stuff
     $this->createEsIndex();
     $initial_data = $this->populateEsIndex();
     $data_source = Phake::mock('Renegare\\ES\\Tests\\Mock\\TestModel');
     $index = $this->es_test_index_name;
     $doc_type = $this->es_test_doc_type;
     $last_sync = new \DateTime();
     $updated_data = $this->getFakeDeltaSyncData($initial_data);
     $expected_data = array_merge($initial_data, $updated_data);
     // extract data flagged to be deleted ... if any!
     $deleted_data = array();
     foreach ($expected_data as $id => $data) {
         if (isset($data['__deleted'])) {
             $deleted_data[$id] = $data;
             unset($expected_data[$id]);
         }
     }
     $expected_data_count = count($expected_data);
     Phake::when($data_source)->getData($index, $doc_type, $last_sync)->thenReturn($updated_data);
     Phake::when($data_source)->getLastSync($index, $doc_type)->thenReturn($last_sync);
     $this->manager->setDataSource($data_source);
     $this->manager->setLastSyncHandler($data_source);
     $this->manager->sync(ElasticSearchManager::SYNC_DELTA, 'test', 'test_type');
     // make sure our data source is called correctly
     Phake::verify($data_source, Phake::times(1))->getData($index, $doc_type, $last_sync);
     Phake::verify($data_source, Phake::times(1))->getLastSync($index, $doc_type);
     // needs more thought as the date time does not always match and fails
     Phake::verify($data_source, Phake::times(1))->setLastSync($index, $doc_type);
     // make sure that all and only expected_data is in es
     // we are using Elastica Library here and not the ESManager ... #ethical
     $es_index = $this->es_client->getIndex($index);
     $es_query_string = new QueryString();
     $es_query_string->setQuery('*');
     $es_query = new Query();
     $es_query->setQuery($es_query_string);
     // ensure we get everything back!
     $es_query->setFrom(0);
     $es_query->setLimit($expected_data_count);
     //Search on the index.
     $es_result = $es_index->search($es_query);
     $this->assertEquals($expected_data_count, $es_result->getTotalHits());
     foreach ($es_result->getResults() as $result) {
         $data = $result->getData();
         $id = $result->getId();
         $this->assertArrayHasKey($id, $expected_data);
         $this->assertEquals(serialize($expected_data[$id]), serialize($data));
         // ensure a duplicate record does not exist
         unset($expected_data[$id]);
     }
     $this->assertEquals(0, count($expected_data));
     // delete test index
     $this->deleteEsIndex();
 }
 /**
  * Query to search auto
  *
  * @param CSearchThesaurusEntry $favori The favori
  * @param CSejour               $sejour The sejour
  *
  * @return Query
  */
 function querySearchAuto($favori, $sejour)
 {
     $query_bool = new Elastica\Query\Bool();
     // query des séjours
     $query_sejour = new Elastica\Query\QueryString();
     $query_sejour->setQuery($this->constructWordsWithSejour($sejour->_id));
     $query_sejour->setDefaultOperator("and");
     $query_bool->addMust($query_sejour);
     // query du favoris
     $query_words = new Elastica\Query\QueryString();
     $query_words->setQuery($this->normalizeEncoding($favori->entry));
     $query_words->setFields(array("body", "title"));
     $query_words->setDefaultOperator("and");
     $query_bool->addMust($query_words);
     $query = new Query($query_bool);
     //  Pagination
     $query->setFrom(0);
     // Where to start
     $query->setLimit(30);
     //Highlight
     $query->setHighlight(array("pre_tags" => array(" <em> <strong> "), "post_tags" => array(" </strong> </em>"), "fields" => array("body" => array("fragment_size" => 50, "number_of_fragments" => 3, "highlight_query" => array("bool" => array("must" => array("match" => array("body" => array("query" => $this->normalizeEncoding($favori->entry)))), "minimum_should_match" => 1))))));
     return $query;
 }
Beispiel #7
0
 public function testGlobalSearchTypeSearch()
 {
     $type = $this->_createType();
     $index = $type->getIndex();
     $client = $index->getClient();
     $multiSearch = new MultiSearch($client);
     $search1 = new Search($client);
     $search1->addIndex($index)->addType($type);
     $query1 = new Query();
     $termQuery1 = new Term();
     $termQuery1->setTerm('username', 'farrelley');
     $query1->setQuery($termQuery1);
     $query1->setLimit(2);
     $search1->setQuery($query1);
     $multiSearch->addSearch($search1);
     $this->assertCount(1, $multiSearch->getSearches());
     $search2 = new Search($client);
     $search2->addIndex($index)->addType($type);
     $query2 = new Query();
     $termQuery2 = new Term();
     $termQuery2->setTerm('username', 'bunny');
     $query2->setQuery($termQuery2);
     $query2->setLimit(3);
     $search2->setQuery($query2);
     $multiSearch->addSearch($search2);
     $multiSearch->setSearchType(Search::OPTION_SEARCH_TYPE_COUNT);
     $multiResultSet = $multiSearch->search();
     $this->assertInstanceOf('Elastica\\Multi\\ResultSet', $multiResultSet);
     $this->assertCount(2, $multiResultSet);
     $this->assertInstanceOf('Elastica\\Response', $multiResultSet->getResponse());
     $resultSets = $multiResultSet->getResultSets();
     $this->assertInternalType('array', $resultSets);
     $this->assertArrayHasKey(0, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[0]);
     $this->assertCount(0, $resultSets[0]);
     $this->assertSame($query1, $resultSets[0]->getQuery());
     $this->assertEquals(3, $resultSets[0]->getTotalHits());
     $this->assertArrayHasKey(1, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[1]);
     $this->assertCount(0, $resultSets[1]);
     $this->assertSame($query2, $resultSets[1]->getQuery());
     $this->assertEquals(6, $resultSets[1]->getTotalHits());
     $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_QUERY_AND_FETCH);
     $multiResultSet = $multiSearch->search();
     $this->assertInstanceOf('Elastica\\Multi\\ResultSet', $multiResultSet);
     $this->assertCount(2, $multiResultSet);
     $this->assertInstanceOf('Elastica\\Response', $multiResultSet->getResponse());
     $resultSets = $multiResultSet->getResultSets();
     $this->assertInternalType('array', $resultSets);
     $this->assertArrayHasKey(0, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[0]);
     $this->assertCount(2, $resultSets[0]);
     $this->assertSame($query1, $resultSets[0]->getQuery());
     $this->assertEquals(3, $resultSets[0]->getTotalHits());
     $this->assertArrayHasKey(1, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[1]);
     $this->assertCount(0, $resultSets[1]);
     $this->assertSame($query2, $resultSets[1]->getQuery());
     $this->assertEquals(6, $resultSets[1]->getTotalHits());
 }
 /**
  * Launch the search
  *
  */
 private function search()
 {
     $this->elasticaQuery->setLimit($this->limitPerPage);
     $this->elasticaQuery->setFrom($this->getOffset());
     $this->resultSet = $this->elastica->search($this->elasticaQuery, $this->index);
 }