예제 #1
0
 /**
  *
  * MyItems filter
  * @see FacetFilter
  */
 protected function getBoolFilter()
 {
     $docOwner = new \Elastica\Filter\Term(array('doc_owner' => $this->userId));
     $filter = new \Elastica\Filter\Bool();
     $filter->addShould($docOwner);
     return $filter;
 }
예제 #2
0
 /**
  * @see FacetInterface::getFilter
  */
 public function getFilter($fieldName, array $values)
 {
     // combine selected filters in an or clause
     $filter = new \Elastica\Filter\Bool();
     $rangeDefs = $this->getRangeDefinitions();
     foreach ($values as $filterId) {
         if (isset($rangeDefs[$filterId])) {
             $rangeFilter = new \Elastica\Filter\Range();
             $rangeFilter->addField($fieldName, $rangeDefs[$filterId]);
             $filter->addShould($rangeFilter);
         }
     }
     return $filter;
 }
예제 #3
0
 /**
  * Small unit test to check if also the old object name works.
  *
  * @group unit
  * @expectedException \Elastica\Exception\InvalidException
  */
 public function testOldObject()
 {
     if (version_compare(phpversion(), 7, '>=')) {
         self::markTestSkipped('These objects are not supported in PHP 7');
     }
     $filter = new \Elastica\Filter\Bool();
     $filter->addShould('fail!');
 }
예제 #4
0
 public function getArticles($user, $searchTerm, $page, $pageOffset, $category, $orderby, $colors, $finder, $elasticIndex)
 {
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($category != NULL) {
         $query = $this->getEntityManager()->createQuery("SELECT c.id FROM OrthIndexBundle:Categories c WHERE c.id LIKE :category")->setParameter('category', $category . "%");
         $queryResult = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
         foreach ($queryResult as $categoryId) {
             $categoryArray[] = $categoryId['id'];
         }
         $categoryQuery = new \Elastica\Query\Terms();
         $categoryQuery->setTerms('catRef', $categoryArray);
         $boolQuery->addMust($categoryQuery);
     }
     if ($searchTerm) {
         $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);
     }
     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);
     }
     $agg = new \Elastica\Aggregation\Terms("catRef");
     $agg->setSize(5000);
     $agg->setField('catRef');
     $boolFilter = new \Elastica\Filter\Bool();
     if ($user == "anon.") {
         $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0)));
     } else {
         $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0, $user->getCustomerRef())));
     }
     $filtered = new \Elastica\Query\Filtered($boolQuery, $boolFilter);
     $query = new \Elastica\Query();
     $query->setQuery($filtered);
     //if( $colors != NULL) {
     //  $query->setFilter($colorNested);
     //}
     if ($orderby == 'desc') {
         $query->setSort(array('variants.price' => array('order' => 'desc')));
     } elseif ($orderby == 'asc') {
         $query->setSort(array('variants.price' => array('order' => 'asc')));
     }
     $query->addAggregation($agg);
     $query->setSize(12);
     $query->setFrom($pageOffset);
     $articles = $finder->find($query);
     $aggregations = $elasticIndex->search($query);
     $result = array("articles" => $articles, "aggs" => $aggregations, "rQuery" => $query);
     return $result;
 }
예제 #5
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);
 }
예제 #6
0
 /**
  * @param $appliedFilters
  * @param $elasticaQuery
  *
  * @return array
  */
 public function applyFilterToElasticaQuery($appliedFilters, $elasticaQuery)
 {
     $termFilters = new \Elastica\Filter\Terms();
     $rangeFilters = new \Elastica\Filter\Range();
     $boolFilter = new \Elastica\Filter\Bool();
     $filters = array();
     foreach ($appliedFilters as $facet) {
         if (strpos($facet[key($facet)], "|") !== false) {
             $filters[key($facet)][] = array('range' => explode('|', $facet[key($facet)]));
         } else {
             $filters[key($facet)][] = $facet[key($facet)];
         }
     }
     foreach ($filters as $name => $value) {
         if (is_array($value[0])) {
             foreach ($value as $range) {
                 $rangeFilters->addField($name, array('gte' => $range['range'][0], 'lte' => $range['range'][1]));
                 $boolFilter->addShould($rangeFilters);
             }
         } else {
             $termFilters->setTerms($name, $value);
             $boolFilter->addShould($termFilters);
         }
     }
     $elasticaQuery->setFilter($boolFilter);
     return array($termFilters, $rangeFilters, $boolFilter, $filters);
 }
예제 #7
0
 /**
  * Small unit test to check if also the old object name works.
  *
  * @group unit
  * @expectedException \Elastica\Exception\InvalidException
  */
 public function testOldObject()
 {
     if (version_compare(phpversion(), 7, '>=')) {
         self::markTestSkipped('These objects are not supported in PHP 7');
     }
     $err = array();
     set_error_handler(function () use(&$err) {
         $err[] = func_get_args();
     });
     $filter = new \Elastica\Filter\Bool();
     restore_error_handler();
     $this->assertCount(1, $err);
     $this->assertEquals(E_USER_DEPRECATED, $err[0][0]);
     $filter->addShould('fail!');
 }
 /**
  * This function constructs and returns main filter for elasticsearch query.
  *
  * @return \Elastica\Filter\BoolOr
  */
 protected function constructMainFilter($finalTypes, $options = array())
 {
     $mainFilter = new \Elastica\Filter\Bool();
     foreach ($finalTypes as $module) {
         $moduleFilter = $this->constructModuleLevelFilter($module, $options);
         // if we want myitems add more to the module filter
         if (isset($options['my_items']) && $options['my_items'] !== false) {
             $moduleFilter = $this->constructMyItemsFilter($moduleFilter);
         }
         if (isset($options['filter']) && $options['filter']['type'] == 'range') {
             $moduleFilter = $this->constructRangeFilter($moduleFilter, $options['filter']);
         }
         // we only want JUST favorites if the option is 2
         // if the option is 1 that means we want all including favorites,
         // which in FTS is a normal search parameter
         if (isset($options['favorites']) && $options['favorites'] == 2) {
             $moduleFilter = $this->constructMyFavoritesFilter($moduleFilter);
         }
         $mainFilter->addShould($moduleFilter);
     }
     return $mainFilter;
 }