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;
 }
 /**
  * @param mixed  $query
  * @param string $lang
  * @param string $type
  *
  * @return mixed|void
  */
 public function defineSearch($query, $lang, $type)
 {
     $query = \Elastica\Util::escapeTerm($query);
     $elasticaQueryLang = new \Elastica\Query\Term();
     $elasticaQueryLang->setTerm('lang', $lang);
     $elasticaQueryString = new \Elastica\Query\Match();
     $elasticaQueryString->setFieldQuery('content', $query)->setFieldMinimumShouldMatch('content', '80%');
     $elasticaQueryTitle = new \Elastica\Query\QueryString();
     $elasticaQueryTitle->setDefaultField('title')->setBoost(2.0)->setQuery($query);
     $elasticaQueryBool = new \Elastica\Query\Bool();
     $elasticaQueryBool->addMust($elasticaQueryLang)->addShould($elasticaQueryTitle)->addShould($elasticaQueryString)->setMinimumNumberShouldMatch(1);
     $this->applySecurityContext($elasticaQueryBool);
     if (!is_null($type)) {
         $elasticaQueryType = new \Elastica\Query\Term();
         $elasticaQueryType->setTerm('type', $type);
         $elasticaQueryBool->addMust($elasticaQueryType);
     }
     $rootNode = $this->domainConfiguration->getRootNode();
     if (!is_null($rootNode)) {
         $elasticaQueryRoot = new \Elastica\Query\Term();
         $elasticaQueryRoot->setTerm('root_id', $rootNode->getId());
         $elasticaQueryBool->addMust($elasticaQueryRoot);
     }
     $this->query->setQuery($elasticaQueryBool);
     $this->query->setHighlight(array('pre_tags' => array('<strong>'), 'post_tags' => array('</strong>'), 'fields' => array('content' => array('fragment_size' => 150, 'number_of_fragments' => 3))));
 }
Example #3
0
 public function search($category)
 {
     // we create a query to return all the articles
     // but if the criteria title is specified, we use it
     $boolQuery = new \Elastica\Query\Bool();
     /*Fetch only VALIDATED place*/
     $queryStatus = new \Elastica\Query\Match();
     $queryStatus->setFieldQuery('event.status', StatusType::VALIDATED);
     $boolQuery->addMust($queryStatus);
     if ($category !== null) {
         $queryCategory = new \Elastica\Query\Match();
         $queryCategory->setFieldQuery('event.categories.slug', $category);
         $boolQuery->addMust($queryCategory);
     } else {
         $query = new \Elastica\Query\MatchAll();
     }
     $baseQuery = $boolQuery;
     // then we create filters depending on the chosen criterias
     $boolFilter = new \Elastica\Filter\Bool();
     /*
         Dates filter
         We add this filter only the getIspublished filter is not at "false"
     */
     //        if("false" != $articleSearch->getIsPublished()
     //           && null !== $articleSearch->getDateFrom()
     //           && null !== $articleSearch->getDateTo())
     //        {
     //            $boolFilter->addMust(new \Elastica\Filter\Range('publishedAt',
     //                array(
     //                    'gte' => \Elastica\Util::convertDate($articleSearch->getDateFrom()->getTimestamp()),
     //                    'lte' => \Elastica\Util::convertDate($articleSearch->getDateTo()->getTimestamp())
     //                )
     //            ));
     //        }
     //
     // Published or not filter
     //        if($placeSearch->getIs24h() !== null && $placeSearch->getIs24h()){
     //                        //var_dump($placeSearch->getIs24h());die();
     //            $boolFilter->addMust(
     //                new \Elastica\Filter\Term(['is24h' => $placeSearch->getIs24h()])
     //                //new \Elastica\Filter\Term(['isWifi' => $placeSearch->getIsWifi()])
     //            );
     //
     //            //$boolFilter->addMust('is24h', $placeSearch->getIs24h());
     //        }
     //
     //        if($placeSearch->getIsWifi() !== null && $placeSearch->getIsWifi()){
     //            $boolFilter->addMust(
     //                new \Elastica\Filter\Term(['isWifi' => $placeSearch->getIsWifi()])
     //            );
     //        }
     //
     //        if($placeSearch->getIsDelivery() !== null && $placeSearch->getIsDelivery()){
     //            $boolFilter->addMust(
     //                new \Elastica\Filter\Term(['isDelivery' => $placeSearch->getIsDelivery()])
     //            );
     //        }
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     return $this->find($query);
 }
Example #4
0
 public function facet($filter, $sort = 'membership')
 {
     $boolQuery = new \Elastica\Query\Bool();
     $queryStatus = new \Elastica\Query\Match();
     $queryStatus->setFieldQuery('place.status', StatusType::VALIDATED);
     $boolQuery->addMust($queryStatus);
     if ($filter->getCategory()) {
         $queryCategory = new \Elastica\Query\Match();
         $queryCategory->setFieldQuery('place.categories.slug', $filter->getCategory()->getSlug());
         $boolQuery->addMust($queryCategory);
     }
     $queryCity = new \Elastica\Query\Match();
     $queryCity->setFieldQuery('place.city.slug', $filter->getCity()->getSlug());
     $boolQuery->addMust($queryCity);
     ##AGGREGATION - FACETED##
     $now = new \DateTime();
     $this->addCollections($boolQuery, $filter);
     $boolFilter = new \Elastica\Filter\Bool();
     //Filters
     $businessHoursDayFilter = new \Elastica\Filter\Term(['businessHours.day' . date('l') => true]);
     $businessHoursStartsAtFilter = new \Elastica\Filter\Range('businessHours.startsAtFormatted', array('lte' => $now->format('H:i:s')));
     $businessHoursEndsAtFilter = new \Elastica\Filter\Range('businessHours.endsAtFormatted', array('gte' => $now->format('H:i:s')));
     $businessHoursIs24HFilter = new \Elastica\Filter\Term(['is24h' => true]);
     $businessHoursExceptionDateFilter = new \Elastica\Filter\Term(['businessHoursException.dayFormatted' => date('Y-m-d')]);
     $businessHoursExceptionStartsAtFilter = new \Elastica\Filter\Range('businessHoursException.startsAtFormatted', array('lte' => $now->format('H:i:s')));
     $businessHoursExceptionEndsAtFilter = new \Elastica\Filter\Range('businessHoursException.endsAtFormatted', array('gte' => $now->format('H:i:s')));
     $businessHoursExceptionStartsAtMissingFilter = new \Elastica\Filter\Missing('businessHoursException.startsAtFormatted');
     $businessHoursExceptionEndsAtMissingFilter = new \Elastica\Filter\Missing('businessHoursException.endsAtFormatted');
     $businessHoursExceptionDateTimeFilter = new \Elastica\Filter\Bool();
     $businessHoursExceptionDateTimeFilter->addMust($businessHoursExceptionDateFilter)->addMust($businessHoursExceptionStartsAtFilter)->addMust($businessHoursExceptionEndsAtFilter);
     $businessHoursExceptionAllDayClosedFilter = new \Elastica\Filter\Bool();
     $businessHoursExceptionAllDayClosedFilter->addMust($businessHoursExceptionDateFilter)->addMust($businessHoursExceptionStartsAtMissingFilter)->addMust($businessHoursExceptionEndsAtMissingFilter);
     $businessHoursDayTimeFilter = new \Elastica\Filter\Bool();
     $businessHoursDayTimeFilter->addMust($businessHoursDayFilter)->addMust($businessHoursStartsAtFilter)->addMust($businessHoursEndsAtFilter);
     #BusinessHours Filter
     $businessHoursFilter = new \Elastica\Filter\Bool();
     $businessHoursFilter->addShould($businessHoursDayTimeFilter);
     #BusinessHoursException Filter
     $businessHoursExceptionFilter = new \Elastica\Filter\Bool();
     $businessHoursExceptionFilter->addShould($businessHoursExceptionDateTimeFilter);
     $businessHoursNestedFilter = new \Elastica\Filter\Nested();
     $businessHoursNestedFilter->setFilter($businessHoursFilter)->setPath('place.businessHours');
     $businessHoursExceptionNestedFilter = new \Elastica\Filter\Nested();
     $businessHoursExceptionNestedFilter->setFilter($businessHoursExceptionFilter)->setPath('place.businessHoursException');
     $businessHoursExceptionMissingNestedFilter = new \Elastica\Filter\Nested();
     $businessHoursExceptionMissingNestedFilter->setFilter($businessHoursExceptionAllDayClosedFilter)->setPath('place.businessHoursException');
     $workingNowFilter = new \Elastica\Filter\Bool();
     $workingNowFilter->addShould($businessHoursNestedFilter)->addShould($businessHoursExceptionNestedFilter)->addShould($businessHoursIs24HFilter)->addMustNot($businessHoursExceptionMissingNestedFilter);
     if ($filter->getBusinessHours()) {
         foreach ($filter->getBusinessHours() as $value) {
             if ($value == 'workingNow') {
                 $boolFilter->addMust($workingNowFilter);
             }
             if ($value == '24/7') {
                 $boolFilter->addMust($businessHoursIs24HFilter);
             }
         }
     }
     //Aggregation
     $aggregFilters = new \Elastica\Aggregation\Terms('filters');
     $aggregFilters->setField('placeFilterValues.slug');
     //$aggregFilters->setSize(0);
     $aggregCategories = new \Elastica\Aggregation\Terms('categories');
     $aggregCategories->setField('categories.slug');
     //        $aggregBusinessHoursDay = new \Elastica\Aggregation\Filter('businessHoursDay');
     //        $aggregBusinessHoursDay->setFilter($businessHoursDayFilter);
     //
     //        $aggregBusinessHoursStartsAt = new \Elastica\Aggregation\Filter('businessHoursStartsAt');
     //        $aggregBusinessHoursStartsAt->setFilter($businessHoursStartsAtFilter);
     //
     //        $aggregBusinessHoursEndsAtFilter = new \Elastica\Aggregation\Filter('businessHoursEndsAt');
     //        $aggregBusinessHoursEndsAtFilter->setFilter($businessHoursEndsAtFilter);
     //
     //        $aggregBusinessHoursStartsAt->addAggregation($aggregBusinessHoursEndsAtFilter);
     //        $aggregBusinessHoursDay->addAggregation($aggregBusinessHoursStartsAt);
     $aggregBusinessHours = new \Elastica\Aggregation\Filters('businessHours');
     $aggregBusinessHours->addFilter($workingNowFilter, 'workingNow');
     $aggregBusinessHours->addFilter($businessHoursIs24HFilter, '24/7');
     $filtered = new \Elastica\Query\Filtered($boolQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     //set aggregations type
     foreach ($filter->getAggregations() as $aggregation) {
         $aggtype = 'aggreg' . ucfirst($aggregation);
         $query->addAggregation(${$aggtype});
     }
     //$query->addAggregation($aggregFilters);
     //$query->addAggregation($aggregBusinessHours);
     $sortMembership = array('membershipSubscriptions.membership.score' => array('nested_filter' => array('term' => array('membershipSubscriptions.m_status' => MembershipStatusType::ACTIVE)), 'order' => 'desc'));
     $sortRating = array('rating' => array('order' => 'desc'));
     $viewsCount = array('viewsCount' => array('order' => 'desc'));
     $sortTypes = array('membership' => array($sortMembership, $sortRating), 'rating' => array($sortRating), 'views' => array($viewsCount));
     //        if(!isset($sortTypes[$sort])){
     //            $sort = 'membership';
     //        }
     foreach ($sortTypes[$sort] as $s) {
         $query->addSort($s);
     }
     //$query->setFrom(4);
     //$query->addSort(array('rating' => array('order' => 'desc')));
     //var_dump(json_encode($query->getQuery(), JSON_PRETTY_PRINT));die();
     return $this->findPaginated($query);
 }
Example #5
0
 /**
  * @SWG\Api(
  *   path="/posts/:id",
  *   @SWG\Operation(
  *     method="GET",
  *     summary="Gets a post",
  *     nickname="posts_get"
  *   )
  * )
  */
 function getById($id)
 {
     $result = array();
     $body = $this->getBodyAsArray();
     $required_parameters = array();
     $optional_parameters = array('post_type');
     $this->check_req_opt_param($required_parameters, $optional_parameters, $body);
     $index = $this->elastica->getIndex($this->getElasticsearchIndex());
     $query = new \Elastica\Query();
     $boolean = new \Elastica\Query\Bool();
     $added = false;
     $type = isset($body['post_type']) ? $body['post_type'] : null;
     if ($type) {
         $q = new \Elastica\Query\Term(array("_type" => $type));
         $boolean->addMust($q);
     }
     $q = new \Elastica\Query\Term(array("_id" => $id));
     $boolean->addMust($q);
     $query->setQuery($boolean);
     $query->setSize(1);
     $elasticaResultSet = $index->search($query);
     $elasticaResults = $elasticaResultSet->getResults();
     foreach ($elasticaResults as $elasticaResult) {
         $result[] = $elasticaResult->getData();
     }
     echo json_encode($result);
 }
Example #6
0
 /**
  * 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;
 }
 protected function parseQueryString($queryString, array $opts)
 {
     $fields = $highlights = array();
     $terms = preg_split('/\\s+/', $queryString);
     $match = $opts['match'];
     $case = $opts['case'];
     // Map each word in the query string with its corresponding field
     foreach ($terms as $term) {
         $prefix = strstr($term, '*', true);
         if ($prefix) {
             // For wildcard search
             $fields['content.prefix_complete'][] = $prefix;
         } elseif ($case === '1') {
             // For case sensitive search
             $fields['content.case_sensitive'][] = $term;
         } else {
             $fields['content'][] = $term;
         }
     }
     // Allow searching either by message content or message id (page name
     // without language subpage) with exact match only.
     $searchQuery = new \Elastica\Query\Bool();
     foreach ($fields as $analyzer => $words) {
         foreach ($words as $word) {
             $boolQuery = new \Elastica\Query\Bool();
             $contentQuery = new \Elastica\Query\Match();
             $contentQuery->setFieldQuery($analyzer, $word);
             $boolQuery->addShould($contentQuery);
             $messageQuery = new \Elastica\Query\Term();
             $messageQuery->setTerm('localid', $word);
             $boolQuery->addShould($messageQuery);
             if ($match === 'all') {
                 $searchQuery->addMust($boolQuery);
             } else {
                 $searchQuery->addShould($boolQuery);
             }
             // Fields for highlighting
             $highlights[$analyzer] = array('number_of_fragments' => 0);
             // Allow searching by exact message title (page name with
             // language subpage).
             $title = Title::newFromText($word);
             if (!$title) {
                 continue;
             }
             $handle = new MessageHandle($title);
             if ($handle->isValid() && $handle->getCode() !== '') {
                 $localid = $handle->getTitleForBase()->getPrefixedText();
                 $boolQuery = new \Elastica\Query\Bool();
                 $messageId = new \Elastica\Query\Term();
                 $messageId->setTerm('localid', $localid);
                 $boolQuery->addMust($messageId);
                 $searchQuery->addShould($boolQuery);
             }
         }
     }
     return array($searchQuery, $highlights);
 }