Пример #1
1
 public function action_index()
 {
     $this->view = new View_Search();
     if ($this->request->query('query') !== NULL) {
         $query = $this->request->query('query');
         $queryString = new \Elastica\Query\QueryString();
         $queryString->setDefaultOperator('AND');
         $queryString->setQuery($query);
         $elasticaQuery = new \Elastica\Query();
         $elasticaQuery->setQuery($queryString);
         $resultSet = ElasticSearch::instance()->search($elasticaQuery);
         $results = $resultSet->getResults();
         /** @var Kostache $renderer */
         $renderer = Kostache::factory();
         $out = array();
         foreach ($results as $result) {
             // Attempt to create the avatar instance.
             try {
                 $refl = new ReflectionClass('View_Search_' . ucfirst($result->getType()));
                 $view = $refl->newInstance();
                 $view->data = $result->getData();
                 $out[] = $renderer->render($view);
             } catch (ReflectionException $ex) {
                 Kohana::$log->add(LOG::ERROR, 'No search view class found for search type ":type".', array(':type' => $result->getType()));
             }
         }
         $this->view->query = $query;
         $this->view->results = $out;
     }
 }
Пример #2
1
 /**
  * {@inheritdoc}
  */
 public function findUser($token)
 {
     $query = new \Elastica\Query\Fuzzy();
     $query->setField('username', $token);
     $finalQuery = new \Elastica\Query($query);
     $finalQuery->setFields(['username', 'profilePicture']);
     $finalQuery->setHighlight(['pre_tags' => ['<em style="color: #FF66FF;">'], 'post_tags' => ['</em>'], 'fields' => ['username' => ['fragment_size' => 200, 'number_of_fragments' => 1]]]);
     $users = $this->finder->search($finalQuery);
     $results = [];
     foreach ($users as $user) {
         $results[] = ['username' => $user->getData()['username'][0], 'profilePicture' => $user->getData()['profilePicture'][0], 'highlights' => $user->getHighlights()['username'][0]];
     }
     return $results;
 }
Пример #3
1
 private function makeElasticQuery()
 {
     $exceptedQuery = new \Elastica\Query();
     $aggregationFilters = new \Elastica\Aggregation\Filters("emotion");
     $negative = new \Elastica\Filter\Range('valeur', array('lt' => 0));
     $aggregationFilters->addFilter($negative, 'negative');
     $neutral = new \Elastica\Filter\Range('valeur', array('lt' => 1, 'gt' => -1));
     $aggregationFilters->addFilter($neutral, 'neutral');
     $positive = new \Elastica\Filter\Range('valeur', array('gt' => 0));
     $aggregationFilters->addFilter($positive, 'positive');
     $dateHisto = new \Elastica\Aggregation\DateHistogram('nb', 'date', 'month');
     $aggregationFilters->addAggregation($dateHisto);
     $exceptedQuery->addAggregation($aggregationFilters);
     return $exceptedQuery;
 }
<?php

ini_set("display_errors", true);
require_once __DIR__ . '/../vendor/autoload.php';
$client = new \Elastica\Client();
$search = new Elastica\Search($client);
$search->addIndex('comicbook')->addType('superhero');
$query = new Elastica\Query();
$query->setSize(10)->setSort(['name' => 'asc']);
$searchText = "Kent";
$stringQuery = new Elastica\Query\QueryString($searchText);
$query->setQuery($stringQuery);
$query->setHighlight(array('pre_tags' => array('<strong>'), 'post_tags' => array('</strong>'), 'fields' => array('name' => array('force_source' => true), 'summary' => array('force_source' => true))));
$search->setQuery($query);
$resultSet = $search->search();
echo 'Search "' . $searchText . '"<br />' . PHP_EOL;
echo 'Founded ' . $resultSet->getTotalHits() . ' records in ' . $resultSet->getTotalTime() . 'ms<br /><br />' . PHP_EOL;
foreach ($resultSet as $result) {
    $data = $result->getData();
    foreach ($result->getHighlights() as $key => $value) {
        $data[$key] = join(PHP_EOL, $value);
    }
    echo $data['name'] . ' - ' . $data['summary'] . "<br /><br />" . PHP_EOL;
}
 public function testQuery()
 {
     $client = $this->_getClient();
     $nodes = $client->getCluster()->getNodes();
     if (!$nodes[0]->getInfo()->hasPlugin('geocluster-facet')) {
         $this->markTestSkipped('geocluster-facet plugin not installed');
     }
     $index = $this->_createIndex('geocluster_test');
     $type = $index->getType('testQuery');
     $geoField = 'location';
     $type->setMapping(new \Elastica\Type\Mapping($type, array($geoField => array('type' => 'geo_point', 'lat_lon' => true))));
     $doc = new \Elastica\Document(1, array('name' => 'item1', 'location' => array(20, 20)));
     $type->addDocument($doc);
     $doc = new \Elastica\Document(2, array('name' => 'item2', 'location' => array(20, 20)));
     $type->addDocument($doc);
     $doc = new \Elastica\Document(3, array('name' => 'item3', 'location' => array(20, 20)));
     $type->addDocument($doc);
     $index->refresh();
     $facet = new \Elastica\Facet\GeoCluster('clusters');
     $facet->setField($geoField)->setFactor(1)->setShowIds(false);
     $query = new \Elastica\Query();
     $query->setFacets(array($facet));
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(1, count($facets['clusters']['clusters']));
     $index->delete();
 }
 public function getQueryForSearch(ArticleSearch $articleSearch)
 {
     // we create a query to return all the articles
     // but if the criteria title is specified, we use it
     if ($articleSearch->getTitle() !== null && $articleSearch != '') {
         $query = new \Elastica\Query\Match();
         $query->setFieldQuery('article.title', $articleSearch->getTitle());
         $query->setFieldFuzziness('article.title', 0.7);
         $query->setFieldMinimumShouldMatch('article.title', '80%');
     } else {
         $query = new \Elastica\Query\MatchAll();
     }
     // then we create filters depending on the chosen criterias
     $boolQuery = new \Elastica\Query\Bool();
     $boolQuery->addMust($query);
     /*
         Dates filter
         We add this filter only the ispublished filter is not at "false"
     */
     if ("false" != $articleSearch->isPublished() && null !== $articleSearch->getDateFrom() && null !== $articleSearch->getDateTo()) {
         $boolQuery->addMust(new \Elastica\Query\Range('publishedAt', array('gte' => \Elastica\Util::convertDate($articleSearch->getDateFrom()->getTimestamp()), 'lte' => \Elastica\Util::convertDate($articleSearch->getDateTo()->getTimestamp()))));
     }
     // Published or not filter
     if ($articleSearch->isPublished() !== null) {
         $boolQuery->addMust(new \Elastica\Query\Terms('published', array($articleSearch->isPublished())));
     }
     $query = new \Elastica\Query($boolQuery);
     $query->setSort(array($articleSearch->getSort() => array('order' => $articleSearch->getDirection())));
     return $query;
 }
Пример #7
0
 public function articlelistAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $searchTerm = $request->query->get('q');
     $page = 0;
     $pageOffset = 0;
     if ($request->query->get('p')) {
         $page = $request->query->get('p');
         $pageOffset = ($page - 1) * 12;
     }
     $finder = $this->container->get('fos_elastica.finder.search.article');
     $boolQuery = new \Elastica\Query\BoolQuery();
     $articles = "";
     $totalpages = 0;
     if ($request->query->get('q') != NULL) {
         $searchTerm = $request->query->get('q');
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('allField', $searchTerm);
         $fieldQuery->setFieldOperator('allField', 'AND');
         $fieldQuery->setFieldMinimumShouldMatch('allField', '80%');
         $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer');
         $boolQuery->addMust($fieldQuery);
         $query = new \Elastica\Query();
         $query->setQuery($boolQuery);
         $query->setSize(10000);
         $articleCatgeories = $finder->find($query);
         $totalpages = ceil(count($finder->find($query)) / 12);
         $query->setSize(12);
         $query->setFrom($pageOffset);
         $articles = $finder->find($query);
     }
     return $this->render('OrthAdminBundle:Articles:articlelist.html.twig', array('totalpages' => $totalpages, 'articles' => $articles));
 }
 /**
  * @internal
  **/
 public static function _query($args, $pageIndex, $size, $sortByDate = false)
 {
     $query = new \Elastica\Query($args);
     $query->setFrom($pageIndex * $size);
     $query->setSize($size);
     $query->setFields(array('id'));
     Config::apply_filters('searcher_query', $query);
     try {
         $index = Indexer::_index(false);
         $search = new \Elastica\Search($index->getClient());
         $search->addIndex($index);
         if (!$query->hasParam('sort')) {
             if ($sortByDate) {
                 $query->addSort(array('post_date' => 'desc'));
             } else {
                 $query->addSort('_score');
             }
         }
         Config::apply_filters('searcher_search', $search, $query);
         $results = $search->search($query);
         return self::_parseResults($results);
     } catch (\Exception $ex) {
         error_log($ex);
         return null;
     }
 }
 public function searchAction(Request $request, $page)
 {
     $term = $request->get('q');
     if (strlen($term) === 0) {
         return $this->redirectToRoute('blog_index');
     }
     $finder = $this->container->get('fos_elastica.finder.app.post');
     $queryString = new \Elastica\Query\QueryString();
     $queryString->setDefaultField('_all');
     $queryString->setQuery($term);
     $query = new \Elastica\Query($queryString);
     $query->setSize(50);
     $query->setHighlight(array('fields' => array('*' => new \stdClass())));
     $elasticaSearchResults = $finder->findHybrid($query);
     $searchResults = array();
     $serializer = $this->get('serializer');
     foreach ($elasticaSearchResults as $elasticaSearchResult) {
         $resultJson = $serializer->serialize($elasticaSearchResult->getTransformed(), 'json');
         $resultObj = json_decode($resultJson, true);
         foreach ($elasticaSearchResult->getResult()->getHit()['highlight'] as $key => $value) {
             if ($key !== 'slug') {
                 $resultObj[$key] = current($value);
             }
         }
         array_push($searchResults, $resultObj);
     }
     return $this->render('ElasticSearchBundle::elastica_search_results.html.twig', array('term' => $term, 'results' => $searchResults));
 }
 public function testFuzzyWithFacets()
 {
     $index = $this->_createIndex();
     $type = $index->getType('test');
     $doc = new Document(1, array('name' => 'Basel-Stadt'));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'New York'));
     $type->addDocument($doc);
     $doc = new Document(3, array('name' => 'Baden'));
     $type->addDocument($doc);
     $doc = new Document(4, array('name' => 'Baden Baden'));
     $type->addDocument($doc);
     $index->refresh();
     $field = 'name';
     $fuzzyQuery = new Fuzzy();
     $fuzzyQuery->setField($field, 'Baden');
     $facet = new \Elastica\Facet\Terms('test');
     $facet->setField('name');
     $query = new \Elastica\Query($fuzzyQuery);
     $query->addFacet($facet);
     $resultSet = $index->search($query);
     // Assert query worked ok
     $this->assertEquals(2, $resultSet->count());
     // Check Facets
     $this->assertTrue($resultSet->hasFacets());
     $facets = $resultSet->getFacets();
     $this->assertEquals(2, $facets['test']['total']);
 }
Пример #11
0
 public function newsAction(Request $request)
 {
     $request = $this->getRequest();
     $pageOffset = 0;
     if ($request->query->get('p')) {
         $page = $request->query->get('p');
         $pageOffset = ($page - 1) * 6;
     }
     $finder = $this->container->get('fos_elastica.finder.search.posts');
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($request->query->get('q')) {
         $search = $request->query->get('q');
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('allField', $search);
         $fieldQuery->setFieldOperator('allField', 'AND');
         $fieldQuery->setFieldMinimumShouldMatch('allField', '80%');
         $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer');
         $boolQuery->addMust($fieldQuery);
     } else {
         $fieldQuery = new \Elastica\Query\MatchAll();
     }
     $query = new \Elastica\Query();
     $query->setQuery($boolQuery);
     $query->setSize(1000);
     $totalpages = ceil(count($finder->find($query)) / 6);
     $query->setSize(6);
     $query->setFrom($pageOffset);
     $posts = $finder->find($query);
     return $this->render('TeamIndexBundle:News:news.html.twig', array('posts' => $posts, 'totalpages' => $totalpages));
 }
 public function searchActiveCategories()
 {
     $query = new \Elastica\Query(new \Elastica\Query\MatchAll());
     $publishedQuery = new \Elastica\Query(new \Elastica\Query\Term(array('published' => true)));
     $hasChildQuery = new \Elastica\Query\HasChild($publishedQuery);
     $hasChildQuery->setType('article');
     $query->setQuery($hasChildQuery);
     return $this->find($query);
 }
Пример #13
0
 /**
  * @param Hackathon_ElasticgentoCore_Model_Resource_Client $searchAdapter
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  *
  * @return Elastica\ResultSet
  */
 protected function fetchSearchResultFromElasticSearch($searchAdapter, $queryText, $query)
 {
     $elasticQuery = new Elastica\Query();
     $queryFuzzyLikeThis = new \Elastica\Query\FuzzyLikeThis();
     $queryFuzzyLikeThis->addFields(Mage::helper('elasticgento_catalogsearch/data')->getSearchableElasticSearchFieldNames());
     $queryFuzzyLikeThis->setLikeText($queryText);
     $elasticQuery->setQuery($queryFuzzyLikeThis);
     $returnFields = ['entity_id', 'name'];
     $elasticQuery->setFields($returnFields);
     return $searchAdapter->getIndex($query->getStoreId())->search($elasticQuery);
 }
Пример #14
0
 /**
  * @group functional
  */
 public function testQueryInsideHasParentSearch()
 {
     $index = $this->prepareSearchData();
     $f = new \Elastica\Query\Term();
     $f->setTerm('user', 'parent1');
     $filter = new HasParent($f, 'parent');
     $searchQuery = new \Elastica\Query();
     $searchQuery->setPostFilter($filter);
     $searchResults = $index->search($searchQuery);
     $this->assertEquals(1, $searchResults->count());
     $result = $searchResults->current()->getData();
     $expected = array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**');
     $this->assertEquals($expected, $result);
 }
 public function testTypeInsideHasChildSearch()
 {
     $index = $this->prepareSearchData();
     $f = new \Elastica\Query\Match();
     $f->setField('alt.name', 'testname');
     $query = new HasChild($f, 'child');
     $searchQuery = new \Elastica\Query();
     $searchQuery->setQuery($query);
     $searchResults = $index->search($searchQuery);
     $this->assertEquals(1, $searchResults->count());
     $result = $searchResults->current()->getData();
     $expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => '*****@*****.**');
     $this->assertEquals($expected, $result);
 }
Пример #16
0
 /**
  * @Route("/app/users/list", name="app_users_list")
  */
 public function listUsersAction()
 {
     $finder = $this->container->get('fos_elastica.finder.app.user');
     $query = new \Elastica\Query();
     $matchAll = new \Elastica\Query\MatchAll();
     $query->setQuery($matchAll);
     $query->setSize(1000);
     $results = $finder->findHybrid($query);
     $elasticaResults = [];
     foreach ($results as $result) {
         $elasticaResults[] = [$result->getResult()->getId() => $result->getResult()->getSource()];
     }
     return new Response(json_encode($elasticaResults));
 }
Пример #17
0
 public function getArticles($user, $searchTerm, $page, $pageOffset, $category, $colors, $finder)
 {
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($category != NULL) {
         $categoryQuery = new \Elastica\Query\Terms();
         $categoryQuery->setTerms('custdata.customCatRef', array($category));
         $boolQuery->addMust($categoryQuery);
     }
     if ($searchTerm) {
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('customerRef', $user->getCustomerRef());
         $boolQuery->addMust($fieldQuery);
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('allField', $searchTerm);
         $fieldQuery->setFieldOperator('allField', 'AND');
         $fieldQuery->setFieldMinimumShouldMatch('allField', '70%');
         $fieldQuery->setFieldFuzziness('allField', '0.8');
         $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer');
         $boolQuery->addMust($fieldQuery);
     } else {
         $fieldQuery = new \Elastica\Query\MatchAll();
         $boolQuery->addMust($fieldQuery);
     }
     $fieldQuery = new \Elastica\Query\Nested();
     $fieldQuery->setPath('custdata.custcat.perm');
     $boolNested = new \Elastica\Query\BoolQuery();
     $fieldNested = new \Elastica\Query\Match();
     $fieldNested->setField('custdata.custcat.perm.permStatus', 1);
     $boolNested->addMust($fieldNested);
     $fieldNested = new \Elastica\Query\Match();
     $fieldNested->setField('custdata.custcat.perm.userRef', $user->getId());
     $boolNested->addMust($fieldNested);
     $fieldQuery->setQuery($boolNested);
     $boolQuery->addMust($fieldQuery);
     if ($colors != NULL) {
         $colorQuery = new \Elastica\Query\Terms();
         $colorQuery->setTerms('variants.variantvalues.otherTerms', $colors);
         $colorNested = new \Elastica\Query\Nested('variants');
         $colorNested->setPath('variants.variantvalues');
         $colorNested->setQuery($colorQuery);
         $boolQuery->addMust($colorNested);
     }
     $query = new \Elastica\Query();
     $query->setQuery($boolQuery);
     $query->setSize(12);
     $query->setFrom($pageOffset);
     $articles = $finder->find($query);
     $result = array("articles" => $articles, "rQuery" => $query);
     return $result;
 }
Пример #18
0
 /**
  * 
  * @return bool
  */
 protected function getTrendExtensions()
 {
     $es = $this->app->container->get('elastica.client');
     $elasticaQueryString = new \Elastica\Query\QueryString();
     $elasticaQueryString->setQuery('*');
     $elasticaQuery = new \Elastica\Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     $elasticaQuery->setSort(["stars" => 'desc']);
     $elasticaIndex = $es->getIndex('packages');
     $results = $elasticaIndex->search($elasticaQuery);
     $hits = [];
     foreach ($results->getResults() as $result) {
         $hits[] = $result->getHit();
     }
     return $hits;
 }
 private function show($filter = null)
 {
     $query = new \Elastica\Query();
     if ($filter) {
         $query->setPostFilter($filter);
     }
     // WHAT ARE YOU DOING TRACKING MORE THAN 5000 INDEXES?!?
     $query->setSize(5000);
     $res = $this->getType()->getIndex()->search($query);
     foreach ($res as $r) {
         $data = $r->getData();
         $this->outputIndented("index name: " . $r->getId() . "\n");
         $this->outputIndented("  analysis version: {$data['analysis_maj']}.{$data['analysis_min']}\n");
         $this->outputIndented("  mapping version: {$data['mapping_maj']}.{$data['mapping_min']}\n");
         $this->outputIndented("  shards: {$data['shard_count']}\n");
     }
 }
Пример #20
0
 public function searchindexAction($searchTerm)
 {
     $client = new \Elastica\Client(array('host' => '192.168.15.102', 'port' => 9200));
     $index = $client->getIndex('coolcities');
     $multiMatchQuery = new \Elastica\Query\MultiMatch();
     $multiMatchQuery->setQuery($searchTerm);
     $multiMatchQuery->setFields(array('title', 'description', 'city^3'));
     // Create the actual search object with some data.
     $esQuery = new \Elastica\Query();
     $esQuery->setQuery($multiMatchQuery);
     //Search on the index.
     $results = $index->search($esQuery);
     foreach ($results as $result) {
         echo "<pre>";
         print_r($result);
         echo "<pre>";
     }
     die;
     return new Response('index search');
 }
 public function testFilter()
 {
     $index = $this->_createIndex('geohash_filter_test');
     $type = $index->getType('test');
     $mapping = new \Elastica\Type\Mapping($type, array('pin' => array('type' => 'geo_point', 'geohash' => true, 'geohash_prefix' => true)));
     $type->setMapping($mapping);
     $type->addDocument(new \Elastica\Document(1, array('pin' => '9q8yyzm0zpw8')));
     $type->addDocument(new \Elastica\Document(2, array('pin' => '9mudgb0yued0')));
     $index->refresh();
     $filter = new GeohashCell('pin', array('lat' => 32.828326, 'lon' => -117.255854));
     $query = new \Elastica\Query();
     $query->setFilter($filter);
     $results = $type->search($query);
     $this->assertEquals(1, $results->count());
     //test precision parameter
     $filter = new GeohashCell('pin', '9', 1);
     $query = new \Elastica\Query();
     $query->setFilter($filter);
     $results = $type->search($query);
     $this->assertEquals(2, $results->count());
     $index->delete();
 }
 public function AetCommunicationSearch($searchText)
 {
     //$finder = $this->container->get('fos_elastica.finder.aetsite.aetusers');
     $baseQuery = new \Elastica\Query();
     $boolQuery = new \Elastica\Query\Bool();
     /*
             if ((strlen($searchText)==4) && ctype_digit($searchText)) {
                 // Your Convert logic
     
                 $boolFilter = new \Elastica\Filter\Bool();
     
                 $from = new \DateTime('01/01/'.$searchText);
                 $to = new \DateTime('12/31/'.$searchText);
                 $boolFilter->addMust(new \Elastica\Filter\Range('promotion',
                     array(
                         'gte' => \Elastica\Util::convertDate($from->getTimestamp()),
                         'lte' => \Elastica\Util::convertDate($to->getTimestamp())
                     )
                 ));
     
                 $baseQuery->setPostFilter($boolFilter);
             }
             else{*/
     $fieldQuery = new \Elastica\Query\Match();
     $fieldQuery->setFieldQuery('title', $searchText);
     //$fieldQuery->setFieldParam('title', 'analyzer', 'custom_search_analyzer');
     $boolQuery->addShould($fieldQuery);
     $field1Query = new \Elastica\Query\Match();
     $field1Query->setFieldQuery('shortDesc', $searchText);
     $boolQuery->addShould($field1Query);
     $field1Query = new \Elastica\Query\Match();
     $field1Query->setFieldQuery('body', $searchText);
     $boolQuery->addShould($field1Query);
     $baseQuery->setQuery($boolQuery);
     //}
     // Option 1. Returns all users who have example.net in any of their mapped fields
     return $this->find($baseQuery);
 }
 public function testLookup()
 {
     $index = $this->_createIndex('terms_filter_test');
     $type1 = $index->getType('musicians');
     $type2 = $index->getType('bands');
     //index some test data
     $type1->addDocument(new \Elastica\Document(1, array('name' => 'robert', 'lastName' => 'plant')));
     $type1->addDocument(new \Elastica\Document(2, array('name' => 'jimmy', 'lastName' => 'page')));
     $type1->addDocument(new \Elastica\Document(3, array('name' => 'john paul', 'lastName' => 'jones')));
     $type1->addDocument(new \Elastica\Document(4, array('name' => 'john', 'lastName' => 'bonham')));
     $type1->addDocument(new \Elastica\Document(5, array('name' => 'jimi', 'lastName' => 'hendrix')));
     $type2->addDocument(new \Elastica\Document('led zeppelin', array('members' => array('plant', 'page', 'jones', 'bonham'))));
     $index->refresh();
     //use the terms lookup feature to query for some data
     $termsFilter = new Terms();
     $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', NULL);
     $query = new \Elastica\Query();
     $query->setFilter($termsFilter);
     $results = $index->search($query);
     $this->assertEquals($results->count(), 4, 'Terms lookup with null index');
     $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index);
     $query->setFilter($termsFilter);
     $results = $index->search($query);
     $this->assertEquals($results->count(), 4, 'Terms lookup with index as object');
     //Query with index given as string
     $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName());
     $query->setFilter($termsFilter);
     $results = $index->search($query);
     $this->assertEquals($results->count(), 4, 'Terms lookup with index as string');
     //Query with array of options
     $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index, 'cache' => false));
     $query->setFilter($termsFilter);
     $results = $index->search($query);
     $this->assertEquals($results->count(), 4, 'Terms lookup with options array');
     $index->delete();
 }
Пример #24
0
 public function searchLoans($conditions = array(), $page = 1, $limit = 20)
 {
     $conditions += ['search' => false];
     $search = $conditions['search'];
     unset($conditions['search']);
     $queryString = new \Elastica\Query\QueryString();
     $loanIndex = $this->getLoanIndex();
     $query = new \Elastica\Query();
     if ($search) {
         $queryString->setDefaultOperator('AND');
         $queryString->setQuery($search);
         $query->setQuery($queryString);
     }
     $filterAnd = new \Elastica\Filter\BoolAnd();
     foreach ($conditions as $field => $value) {
         $termFilter = new \Elastica\Filter\Term();
         $termFilter->setTerm($field, $value);
         $filterAnd->addFilter($termFilter);
     }
     if ($conditions) {
         $query->setFilter($filterAnd);
     }
     $query->setFrom(($page - 1) * $limit);
     $query->setSize($page * $limit);
     $results = $loanIndex->search($query);
     $ids = [];
     foreach ($results as $result) {
         $data = $result->getData();
         $ids[$data['id']] = $data['id'];
     }
     $loans = LoanQuery::create()->filterById($ids)->find();
     $sortedLoans = $ids;
     foreach ($loans as $loan) {
         $sortedLoans[$loan->getId()] = $loan;
     }
     $sortedLoans = array_filter($sortedLoans, function ($l) {
         return !is_scalar($l);
     });
     $paginatorFactory = \App::make('paginator');
     return $paginatorFactory->make($sortedLoans, $results->getTotalHits(), $limit);
 }
Пример #25
0
 /**
  * Creates new record of the given module and returns its formatted representation
  *
  * @param ServiceBase $api
  * @param array       $args API arguments
  *
  * @return array Formatted representation of the bean
  * @throws SugarApiExceptionInvalidParameter
  * @throws SugarApiExceptionMissingParameter
  * @throws SugarApiExceptionNotAuthorized
  */
 public function findNear(ServiceBase $api, array $args)
 {
     if (empty($args['location']) || empty($args['lat_long'])) {
         throw new SugarApiExceptionMissingParameter("Missing location");
     }
     $modules = !empty($args['module']) ? array($args['module']) : $this->getValidModules();
     // Load global search engine
     $engine = $this->getEngine();
     $iName = $engine->getReadIndexName($modules);
     $client = $engine->getClient();
     $index = $client->getIndex($iName);
     $query = new Elastica\Query\MatchAll();
     $search = new Elastica\Search($client);
     $search->addIndex($index);
     foreach ($modules as $module) {
         $search->addType($module);
     }
     if (!empty($args['distance'])) {
         $filter = new Elastica\Filter\GeoDistance('lat_long_c', $args['location'], $args['distance']);
         $query = new Elastica\Query\Filtered($query, $filter);
     }
     //Add sort
     $query = Elastica\Query::create($query);
     $query->addSort(array('_geo_distance' => array("lat_long_c" => $args['location'], "order" => "asc", "unit" => "mi")));
     $results = $search->search($query)->getResults();
     $ret = array();
     foreach ($results as $result) {
         $dist = $result->getParam("sort");
         $module = $result->getType();
         $bean = $this->formatBeanFromResult($api, $args, $result, BeanFactory::getBean($module));
         $bean['_distance'] = $dist[0];
         $bean['_distance_unit'] = 'mi';
         $ret[] = $bean;
     }
     return $ret;
 }
Пример #26
0
     $elasticaTypeOfFilter->setQuery($query_collection);
     $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
     $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
     $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaFilterAnd);
 }
 $query = new Elastica\Query($filteredQuery);
 if (isset($_GET['speaker'])) {
     $boolQuery = new Elastica\Query\Bool();
     $query_collection = new Elastica\Query\Match();
     $query_collection->setFieldQuery('speaker', $_GET['speaker'])->setFieldParam('speaker', 'type', 'phrase');
     $elasticaTypeOfFilter = new Elastica\Filter\Query();
     $elasticaTypeOfFilter->setQuery($query_collection);
     $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
     $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
     $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaFilterAnd);
     $query = new Elastica\Query($filteredQuery);
 }
 //SORT BY
 if (isset($_GET['sort-by'])) {
     if (isset($_GET['sort-by-direction'])) {
         $sortdirection = $_GET['sort-by-direction'];
     } else {
         $sortdirection = "desc";
     }
     $sort = array($_GET['sort-by'] => array("order" => $sortdirection));
     //order by clause
     $query->setSort($sort);
 }
 $query->setFrom($_GET['from'])->setLimit($_GET['size']);
 if (isset($_GET['debug'])) {
     echo '<pre>';
Пример #27
0
 /**
  *   To respect the design, 3 searches will be executed:
  *       1st:    retrieves the main facet (Text / Media / Portfolio / Users / Group) and the count for each of them
  *       2nd:    - retrieves the results of the first non empty facet term for display in the tab
  *               - retrieves the secondary facet to enable / disable the filter items
  *       3nd:    - retrieves the results with all filters applied
  * @param unknown $query_string
  * @param unknown $limit
  * @param unknown $offset
  * @param unknown $options
  * @param unknown $mainfacetterm
  * @param unknown $USER
  * @return multitype:number boolean unknown Ambigous <boolean, NULL> Ambigous <boolean, unknown> multitype:multitype:string number   Ambigous <string, unknown> |multitype:multitype:
  */
 public static function search($query_string, $limit, $offset, $options, $mainfacetterm, $USER)
 {
     $result = array('count' => 0, 'limit' => $limit, 'offset' => $offset, 'data' => false, 'selected' => isset($mainfacetterm) && strlen($mainfacetterm) > 0 ? $mainfacetterm : false, 'totalresults' => 0, 'facets' => array(array('term' => "Text", 'count' => 0, 'display' => "Text"), array('term' => "Media", 'count' => 0, 'display' => "Media"), array('term' => "Portfolio", 'count' => 0, 'display' => "Portfolio"), array('term' => "User", 'count' => 0, 'display' => "Users"), array('term' => "Group", 'count' => 0, 'display' => "Group")), 'content-filter' => array(array('term' => "all", 'count' => 0, 'display' => "All"), array('term' => "Audio", 'count' => 0, 'display' => "Audio"), array('term' => "Comment", 'count' => 0, 'display' => "Comment"), array('term' => "Document", 'count' => 0, 'display' => "Document"), array('term' => "Folder", 'count' => 0, 'display' => "Folder"), array('term' => "Forum", 'count' => 0, 'display' => "Forum"), array('term' => "Forumpost", 'count' => 0, 'display' => "Forum post"), array('term' => "Image", 'count' => 0, 'display' => "Image"), array('term' => "Journal", 'count' => 0, 'display' => "Journal"), array('term' => "Journalentry", 'count' => 0, 'display' => "Journal entry"), array('term' => "Note", 'count' => 0, 'display' => "Note"), array('term' => "Plan", 'count' => 0, 'display' => "Plan"), array('term' => "Profile", 'count' => 0, 'display' => "Profile"), array('term' => "Resume", 'count' => 0, 'display' => "Résumé"), array('term' => "Video", 'count' => 0, 'display' => "Video"), array('term' => "Wallpost", 'count' => 0, 'display' => "Wall post"), array('term' => "Collection", 'count' => 0, 'display' => "Collection"), array('term' => "Page", 'count' => 0, 'display' => "Page")), 'content-filter-selected' => isset($options['secfacetterm']) && strlen($options['secfacetterm']) > 0 ? $options['secfacetterm'] : 'all', 'owner-filter' => array(array('term' => "all", 'count' => 0, 'display' => "All"), array('term' => "me", 'count' => 0, 'display' => "Me"), array('term' => "others", 'count' => 0, 'display' => "Others")), 'owner-filter-selected' => isset($options['owner']) && strlen($options['owner']) > 0 ? $options['owner'] : 'all', 'tagsonly' => isset($options['tagsonly']) && $options['tagsonly'] == true ? true : Null, 'sort' => isset($options['sort']) && strlen($options['sort']) > 0 ? $options['sort'] : 'score', 'license' => isset($options['license']) && strlen($options['license']) > 0 ? $options['license'] : 'all');
     if (strlen($query_string) <= 0) {
         return $result;
     }
     //      1- Get main facet
     // ------------------------------------------------------------------------------------------
     $records = array();
     $elasticaClient = PluginSearchElasticsearch::make_client();
     $elasticaIndex = $elasticaClient->getIndex(get_config_plugin('search', 'elasticsearch', 'indexname'));
     $elasticaQueryString = new \Elastica\Query\QueryString();
     $elasticaAnalyzer = get_config_plugin('search', 'elasticsearch', 'analyzer');
     $elasticaQueryString->setAnalyzer($elasticaAnalyzer);
     $elasticaQueryString->setDefaultOperator('AND');
     $elasticaQueryString->setQuery($query_string);
     // if tags only => set fields to tags
     if ($result['tagsonly'] === true) {
         $elasticaQueryString->setFields(array('tags'));
     }
     // Create the $elasticaQuery object
     $elasticaQuery = new \Elastica\Query();
     $elasticaQuery->setFrom($offset);
     $elasticaQuery->setLimit($limit);
     $elasticaQuery->setQuery($elasticaQueryString);
     $elasticaFilterAnd = new \Elastica\Filter\BoolAnd();
     // Apply ACL filters
     $elasticaFilterACL = new ElasticsearchFilterAcl($USER);
     $elasticaFilterAnd->addFilter($elasticaFilterACL);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     // Define a new facet: mainFacetTerm  - WARNING: don't forget to apply the same filter to the facet
     $elasticaFacet = new \Elastica\Facet\Terms('mainFacetTerm');
     $elasticaFacet->setField('mainfacetterm');
     $elasticaFacet->setOrder('count');
     $elasticaFacet->setFilter($elasticaFilterAnd);
     $elasticaQuery->addFacet($elasticaFacet);
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $result['totalresults'] = $elasticaResultSet->getTotalHits();
     $elasticaFacets = $elasticaResultSet->getFacets();
     $facets = self::process_facets($elasticaFacets['mainFacetTerm']['terms']);
     if (count($facets) == 0) {
         return $result;
     }
     array_walk($result['facets'], 'self::process_tabs', $facets);
     if ($result['selected'] === false || $facets[$result['selected']] == 0) {
         $result['selected'] = self::get_selected_facet($result['facets']);
     }
     //      2- Retrieve results of selected facet
     // ------------------------------------------------------------------------------------------
     $elasticaFilterType = new \Elastica\Filter\Term(array('mainfacetterm' => $result['selected']));
     $elasticaFilterAnd->addFilter($elasticaFilterType);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     // Define a new facet: secFacetTerm  - WARNING: don't forget to apply the same filter to the facet
     $elasticaFacet = new \Elastica\Facet\Terms('secFacetTerm');
     $elasticaFacet->setField('secfacetterm');
     $elasticaFacet->setOrder('count');
     $elasticaFacet->setFilter($elasticaFilterAnd);
     $elasticaQuery->addFacet($elasticaFacet);
     // Sorting
     // Sorting is defined on a per field level, so we must make sure the field exists in the mapping
     $sort = explode('_', $result['sort']);
     if ($sort[0] == 'score') {
         $sort[0] = '_score';
     }
     // set the second column to sort by the score (to break any 'ties').
     $elasticaQuery->setSort(array(array($sort[0] => array('order' => isset($sort[1]) ? $sort[1] : 'desc')), array('_score' => array('order' => 'desc'))));
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $result['count'] = $elasticaResultSet->getTotalHits();
     $elasticaFacets = $elasticaResultSet->getFacets();
     $facets = $elasticaFacets['secFacetTerm']['terms'];
     $facets = self::process_facets($elasticaFacets['secFacetTerm']['terms']);
     array_walk($result['content-filter'], 'self::process_tabs', $facets);
     // set the count of "all" to the total hits
     $result['content-filter'][0]['count'] = $result['count'];
     //      3- Apply filters and retrieve final results
     // ------------------------------------------------------------------------------------------
     // Apply Content filter if different from "all"
     if ($result['content-filter-selected'] != 'all') {
         $elasticaFilterContent = new \Elastica\Filter\Term(array('secfacetterm' => $result['content-filter-selected']));
         $elasticaFilterAnd->addFilter($elasticaFilterContent);
     }
     // Apply Owner filter if different from "all"
     if ($result['owner-filter-selected'] != 'all') {
         $uid = $USER->get('id');
         $elasticaFilterOwner = new \Elastica\Filter\Term(array('owner' => $uid));
         if ($result['owner-filter-selected'] == 'others') {
             $elasticaFilterOwner = new \Elastica\Filter\BoolNot($elasticaFilterOwner);
         }
         $elasticaFilterAnd->addFilter($elasticaFilterOwner);
     }
     // Apply license filter if different from "all"
     if ($result['license'] != 'all') {
         $elasticaFilterLicense = new \Elastica\Filter\Term(array('license' => $result['license']));
         $elasticaFilterAnd->addFilter($elasticaFilterLicense);
     }
     $elasticaQuery->setFilter($elasticaFilterAnd);
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $elasticaResults = $elasticaResultSet->getResults();
     $result['count'] = $elasticaResultSet->getTotalHits();
     foreach ($elasticaResults as $elasticaResult) {
         $tmp = array();
         $tmp['type'] = $elasticaResult->getType();
         $ES_class = 'ElasticsearchType_' . $tmp['type'];
         $tmp = $tmp + $elasticaResult->getData();
         // Get all the data from the DB table
         $dbrec = $ES_class::getRecordDataById($tmp['type'], $tmp['id']);
         if ($dbrec) {
             $tmp['db'] = $dbrec;
             $tmp['db']->deleted = false;
         } else {
             // If the record has been deleted, so just pass the cached data
             // from the search result. Let the template decide how to handle
             // it.
             $tmp['db'] = (object) $tmp;
             $tmp['db']->deleted = true;
         }
         $records[] = $tmp;
     }
     $result['data'] = $records;
     return $result;
 }
Пример #28
0
 public function sortBy($sort)
 {
     $query = new \Elastica\Query('agg_name');
     $query->setSort($sort);
     $this->query[] = $query;
     return $query;
 }
Пример #29
0
 /**
  * @param      $searchTerm
  * @param null $appliedFilters
  * @param      $configuration
  * @param      $preSearchTaxonFilter
  * @param      $types
  *
  * @return mixed
  */
 public function compileElasticSearchStringQuery($searchTerm, $appliedFilters = null, $configuration, $preSearchTaxonFilter, $types = null)
 {
     $elasticaQuery = new \Elastica\Query();
     $boolFilter = new \Elastica\Filter\BoolFilter();
     $query = new \Elastica\Query\QueryString($searchTerm);
     if (!empty($types)) {
         foreach ($types as $type) {
             $typeFilter = new \Elastica\Filter\Type($type);
             $boolFilter->addMust($typeFilter);
         }
         $elasticaQuery->setPostFilter($boolFilter);
     }
     if ($channel = $this->channelContext->getChannel()) {
         $channelFilter = new \Elastica\Filter\Terms();
         $channelFilter->setTerms('channels', array((string) $channel));
         $boolFilter->addMust($channelFilter);
         $elasticaQuery->setPostFilter($boolFilter);
     }
     // this is currently the only pre search filter and it's a taxon
     // this should be abstracted out if other types of pre search filters are desired
     if ('all' !== $preSearchTaxonFilter) {
         $query = new \Elastica\Query\Filtered();
         $query->setQuery(new \Elastica\Query\QueryString($searchTerm));
         $taxonFromRequestFilter = new \Elastica\Filter\Terms();
         $taxonFromRequestFilter->setTerms('taxons', array($preSearchTaxonFilter));
         $boolFilter->addMust($taxonFromRequestFilter);
         $elasticaQuery->setPostFilter($boolFilter);
     }
     $elasticaQuery->setQuery($query);
     return $this->compileElasticsearchQuery($elasticaQuery, $appliedFilters, $configuration);
 }
 /**
  * Run the search against a sanitized query string via ElasticSearch.
  *
  * @param string $string
  * @param array $scriptProperties The scriptProperties array from the SimpleSearch snippet
  * @return array
  */
 public function search($string, array $scriptProperties = array())
 {
     $fields = $this->modx->getOption('sisea.elastic.search_fields', null, 'pagetitle^20,introtext^10,alias^5,content^1');
     $fields = explode(',', $fields);
     $fields = array_map('trim', $fields);
     $fields = array_keys(array_flip($fields));
     $fields = array_filter($fields);
     if (empty($fields)) {
         return false;
     }
     /** @var \Elastica\Query\MultiMatch $query */
     $query = new \Elastica\Query\MultiMatch();
     $query->setFields($fields);
     $query->setQuery($string);
     $customFilterScore = new \Elastica\Query\CustomFiltersScore();
     $customFilterScore->setQuery($query);
     $searchBoosts = $this->modx->getOption('sisea.elastic.search_boost', null, '');
     $searchBoosts = explode('|', $searchBoosts);
     $searchBoosts = array_map('trim', $searchBoosts);
     $searchBoosts = array_keys(array_flip($searchBoosts));
     $searchBoosts = array_filter($searchBoosts);
     $boosts = array();
     foreach ($searchBoosts as $boost) {
         $arr = array('field' => '', 'value' => '', 'boost' => 1.0);
         $field = explode('=', $boost);
         $field = array_map('trim', $field);
         $field = array_keys(array_flip($field));
         $field = array_filter($field);
         if (count($field) != 2) {
             continue;
         }
         $value = explode('^', $field[1]);
         $value = array_map('trim', $value);
         $value = array_keys(array_flip($value));
         $value = array_filter($value);
         if (count($value) != 2) {
             continue;
         }
         $arr['field'] = $field[0];
         $arr['value'] = $value[0];
         $arr['boost'] = $value[1];
         $boosts[] = $arr;
     }
     if (empty($boosts)) {
         $customFilterScore->addFilter(new \Elastica\Filter\Term(array('type' => 'document')), 1);
     } else {
         foreach ($boosts as $boost) {
             $customFilterScore->addFilter(new \Elastica\Filter\Term(array($boost['field'] => $boost['value'])), $boost['boost']);
         }
     }
     /** @var \Elastica\Query $elasticaQuery */
     $elasticaQuery = new \Elastica\Query();
     $elasticaQuery->setQuery($customFilterScore);
     /* set limit */
     $perPage = $this->modx->getOption('perPage', $scriptProperties, 10);
     if (!empty($perPage)) {
         $offset = $this->modx->getOption('start', $scriptProperties, 0);
         $offsetIndex = $this->modx->getOption('offsetIndex', $scriptProperties, 'sisea_offset');
         if (isset($_REQUEST[$offsetIndex])) {
             $offset = (int) $_REQUEST[$offsetIndex];
         }
         $elasticaQuery->setFrom($offset);
         $elasticaQuery->setSize($perPage);
     }
     $elasticaFilterAnd = new \Elastica\Filter\BoolAnd();
     /* handle hidemenu option */
     $hideMenu = $this->modx->getOption('hideMenu', $scriptProperties, 2);
     if ($hideMenu != 2) {
         $elasticaFilterHideMenu = new \Elastica\Filter\Term();
         $elasticaFilterHideMenu->setTerm('hidemenu', $hideMenu ? 1 : 0);
         $elasticaFilterAnd->addFilter($elasticaFilterHideMenu);
     }
     /* handle contexts */
     $contexts = $this->modx->getOption('contexts', $scriptProperties, '');
     $contexts = !empty($contexts) ? $contexts : $this->modx->context->get('key');
     $contexts = explode(',', $contexts);
     $elasticaFilterContext = new \Elastica\Filter\Terms();
     $elasticaFilterContext->setTerms('context_key', $contexts);
     $elasticaFilterAnd->addFilter($elasticaFilterContext);
     /* handle restrict search to these IDs */
     $ids = $this->modx->getOption('ids', $scriptProperties, '');
     if (!empty($ids)) {
         $idType = $this->modx->getOption('idType', $this->config, 'parents');
         $depth = $this->modx->getOption('depth', $this->config, 10);
         $ids = $this->processIds($ids, $idType, $depth);
         $elasticaFilterId = new \Elastica\Filter\Term();
         $elasticaFilterId->setTerm('id', $ids);
         $elasticaFilterAnd->addFilter($elasticaFilterId);
     }
     /* handle exclude IDs from search */
     $exclude = $this->modx->getOption('exclude', $scriptProperties, '');
     if (!empty($exclude)) {
         $exclude = $this->cleanIds($exclude);
         $exclude = explode(',', $exclude);
         $elasticaFilterExcludeId = new \Elastica\Filter\Term();
         $elasticaFilterExcludeId->setTerm('id', $exclude);
         $elasticaFilterNotId = new \Elastica\Filter\BoolNot($elasticaFilterExcludeId);
         $elasticaFilterAnd->addFilter($elasticaFilterNotId);
     }
     /* basic always-on conditions */
     $elasticaFilterPublished = new \Elastica\Filter\Term();
     $elasticaFilterPublished->setTerm('published', 1);
     $elasticaFilterAnd->addFilter($elasticaFilterPublished);
     $elasticaFilterSearchable = new \Elastica\Filter\Term();
     $elasticaFilterSearchable->setTerm('searchable', 1);
     $elasticaFilterAnd->addFilter($elasticaFilterSearchable);
     $elasticaFilterDeleted = new \Elastica\Filter\Term();
     $elasticaFilterDeleted->setTerm('deleted', 0);
     $elasticaFilterAnd->addFilter($elasticaFilterDeleted);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     /* sorting */
     if (!empty($scriptProperties['sortBy'])) {
         $sortDir = $this->modx->getOption('sortDir', $scriptProperties, 'desc');
         $sortDirs = explode(',', $sortDir);
         $sortBys = explode(',', $scriptProperties['sortBy']);
         $dir = 'desc';
         $sortArray = array();
         for ($i = 0; $i < count($sortBys); $i++) {
             if (isset($sortDirs[$i])) {
                 $dir = $sortDirs[$i];
             }
             $sortArray[] = array($sortBys[$i] => $dir);
         }
         $elasticaQuery->setSort($sortArray);
     }
     /* prepare response array */
     $response = array('total' => 0, 'start' => !empty($offset) ? $offset : 0, 'limit' => $perPage, 'status' => 0, 'query_time' => 0, 'results' => array());
     $elasticaResultSet = $this->index->search($elasticaQuery);
     $elasticaResults = $elasticaResultSet->getResults();
     $totalResults = $elasticaResultSet->getTotalHits();
     if ($totalResults > 0) {
         $response['total'] = $totalResults;
         $response['query_time'] = $elasticaResultSet->getTotalTime();
         $response['status'] = 1;
         $response['results'] = array();
         foreach ($elasticaResults as $doc) {
             $d = $doc->getData();
             /** @var modResource $resource */
             $resource = $this->modx->newObject($d['class_key']);
             if ($resource->checkPolicy('list')) {
                 $response['results'][] = $d;
             }
         }
     }
     return $response;
 }