convertDate() public static method

This is the lucene date format
public static convertDate ( integer | string $date ) : string
$date integer | string Date input (could be string etc.) -> must be supported by strtotime
return string Converted date string
 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;
 }
 public function searchAdvanced($q = '')
 {
     if ($q == '') {
         $baseQuery = new \Elastica\Query\MatchAll();
     } else {
         $baseQuery = new \Elastica\Query\Match();
         $baseQuery->setFieldQuery('description', $q);
         $baseQuery->setFieldFuzziness('description', 0.7);
         $baseQuery->setFieldMinimumShouldMatch('description', '80%');
     }
     $boolFilter = new \Elastica\Filter\BoolFilter();
     $dateFrom = new \DateTime();
     $dateFrom->sub(new \DateInterval('P7D'));
     $dateTo = new \DateTime();
     $dateTo->add(new \DateInterval('P1D'));
     $boolFilter->addMust(new \Elastica\Filter\Range('createdAt', array('gte' => \Elastica\Util::convertDate($dateFrom->getTimestamp()), 'lte' => \Elastica\Util::convertDate($dateTo->getTimestamp()))));
     /*
      * $boolFilter->addMust(
      *     new \Elastica\Filter\Term(array('isPublished' => true))
      * );
      * $boolFilter->addMust(
      *     new \Elastica\Filter\Terms('isPublished', array('1', '2', '3'))
      * );
      */
     /*
      * $baseQuery = new \Elastica\Filter\Bool();
      * $baseQuery->addShould(
      *     new \Elastica\Filter\Term(array('id' => intval($q)))
      * );
      * $baseQuery->addShould(
      *     new \Elastica\Filter\Term(array('amount' => floatval($q)))
      * );
      * $filtered = new \Elastica\Query\Filtered();
      * $filtered->setFilter($baseQuery);
      * return $this->finder->find($filtered);
      */
     /*
      * $baseQuery  = new \Elastica\Query\Bool;
      * $idQueryTerm = new \Elastica\Query\Term;
      * $idQueryTerm->setTerm('id', intval($q));
      * $baseQuery->addShould($idQueryTerm);
      */
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     $query->addSort(array('id' => array('order' => 'asc')));
     $query->setSize(1);
     return $this->find($query);
 }
 public function AetUserSearch($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('firstname', $searchText);
         //$fieldQuery->setFieldParam('title', 'analyzer', 'custom_search_analyzer');
         $boolQuery->addShould($fieldQuery);
         $field1Query = new \Elastica\Query\Match();
         $field1Query->setFieldQuery('lastname', $searchText);
         $boolQuery->addShould($field1Query);
         $field1Query = new \Elastica\Query\Match();
         $field1Query->setFieldQuery('pays', $searchText);
         $boolQuery->addShould($field1Query);
         $field1Query = new \Elastica\Query\Match();
         $field1Query->setFieldQuery('ville', $searchText);
         $boolQuery->addShould($field1Query);
         $field1Query = new \Elastica\Query\Match();
         $field1Query->setFieldQuery('codePostale', $searchText);
         $boolQuery->addShould($field1Query);
         $field1Query = new \Elastica\Query\Match();
         $field1Query->setFieldQuery('activitePrincipale', $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);
 }
Beispiel #4
0
 /**
  * Converts given time to format: 1995-12-31T23:59:59Z.
  *
  * This is the lucene date format
  *
  * @param int $date Date input (could be string etc.) -> must be supported by strtotime
  *
  * @return string Converted date string
  */
 public function convertDate($date)
 {
     return Util::convertDate($date);
 }
Beispiel #5
0
 public function search(SiteSearch $siteSearch)
 {
     // We create a query to return all the articles but if the criteria text is specified, we use it
     if ($siteSearch->getText() != null && $siteSearch != '') {
         $baseQuery = new \Elastica\Query\MultiMatch();
         $baseQuery->setQuery($siteSearch->getText())->setFields(array('title', 'subtitle', 'courseContent', 'content'));
         $baseQuery->setFuzziness(0.7);
         $baseQuery->setMinimumShouldMatch('80%');
     } else {
         $baseQuery = new \Elastica\Query\MatchAll();
     }
     // Then we create filters depending on the chosen criterias
     // Filter courses only if type is not "product"
     $productTypeFilter = new \Elastica\Filter\Type();
     $productTypeFilter->setType('product');
     $productNotFilter = new \Elastica\Filter\BoolNot($productTypeFilter);
     // Filter for products with available courses
     $nestedFilter = new \Elastica\Filter\Nested();
     $nestedFilter->setPath('courses');
     $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp()))));
     // Filter not(products) OR products with available courses
     $orFilter = new \Elastica\Filter\BoolOr();
     $orFilter->addFilter($productNotFilter);
     $orFilter->addFilter($nestedFilter);
     // Create a bool filter to put everything together
     $boolFilter = new \Elastica\Filter\Bool();
     $boolFilter->addMust($orFilter);
     // Filter type
     if ($siteSearch->getIsProduct() || $siteSearch->getIsInfoEvent() || $siteSearch->getIsContent()) {
         // Create OR filter to put together the types
         $typeOrFilter = new \Elastica\Filter\BoolOr();
         // Filter products
         if ($siteSearch->getIsProduct()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter info events if isProduct is not selected
         if ($siteSearch->getIsInfoEvent()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter content
         if ($siteSearch->getIsContent()) {
             $typeOrFilter->addFilter($productNotFilter);
         }
         $boolFilter->addMust($typeOrFilter);
     }
     // Filter product type
     if ($siteSearch->getProductType()) {
         $productTypeFilter = new \Elastica\Filter\Nested();
         $productTypeFilter->setPath('productType');
         $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $siteSearch->getProductType()->getId())));
         $boolFilter->addMust($productTypeFilter);
     }
     // Filter day time
     if ($siteSearch->getDayTime()) {
         $dayTimeFilter = new \Elastica\Filter\Nested();
         $dayTimeFilter->setPath('courses');
         $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $siteSearch->getDayTime())));
         $boolFilter->addMust($dayTimeFilter);
     }
     // Filter category
     if ($siteSearch->getCategory()) {
         $categoryFilter = new \Elastica\Filter\BoolOr();
         $mainCategoryFilter = new \Elastica\Filter\Nested();
         $mainCategoryFilter->setPath('category');
         $mainCategoryFilter->setFilter(new \Elastica\Filter\Term(array('category._id' => $siteSearch->getCategory()->getId())));
         $subCategoryFilter = new \Elastica\Filter\Nested();
         $subCategoryFilter->setPath('subcategory');
         $subCategoryFilter->setFilter(new \Elastica\Filter\Term(array('subcategory._id' => $siteSearch->getCategory()->getId())));
         $additionalCategoryFilter = new \Elastica\Filter\Nested();
         $additionalCategoryFilter->setPath('additionalCategories');
         $additionalCategoryFilter->setFilter(new \Elastica\Filter\Term(array('additionalCategories._id' => $siteSearch->getCategory()->getId())));
         $categoryFilter->addFilter($mainCategoryFilter);
         $categoryFilter->addFilter($subCategoryFilter);
         $categoryFilter->addFilter($additionalCategoryFilter);
         $boolFilter->addMust($categoryFilter);
     }
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     $sort = $siteSearch->getSort();
     if (!empty($sort)) {
         $sort = explode(' ', $sort);
         $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc')));
     }
     $paginated = $this->finder->findPaginated($query);
     $paginated->setMaxPerPage($siteSearch->getPerPage())->setCurrentPage($siteSearch->getPage());
     return $paginated;
 }
 public function search(Category $currentCategory, CategorySearch $categorySearch)
 {
     // We create a query to return all the products
     $baseQuery = new \Elastica\Query\MatchAll();
     // Then we create filters depending on the chosen criterias
     // Filter products
     $productTypeFilter = new \Elastica\Filter\Type();
     $productTypeFilter->setType('product');
     // Filter for products with available courses
     $nestedFilter = new \Elastica\Filter\Nested();
     $nestedFilter->setPath('courses');
     $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp()))));
     // Create a bool filter to put everything together
     $boolFilter = new \Elastica\Filter\Bool();
     $boolFilter->addMust($productTypeFilter);
     $boolFilter->addMust($nestedFilter);
     // Show only products
     // Filter type
     if ($categorySearch->getIsProduct() || $categorySearch->getIsInfoEvent()) {
         // Create OR filter to put together the types
         $typeOrFilter = new \Elastica\Filter\BoolOr();
         // Filter products
         if ($categorySearch->getIsProduct()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter info events if isProduct is not selected
         if ($categorySearch->getIsInfoEvent()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         $boolFilter->addMust($typeOrFilter);
     }
     // Filter product type
     if ($categorySearch->getProductType()) {
         $productTypeFilter = new \Elastica\Filter\Nested();
         $productTypeFilter->setPath('productType');
         $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $categorySearch->getProductType()->getId())));
         $boolFilter->addMust($productTypeFilter);
     }
     // Filter day time
     if ($categorySearch->getDayTime()) {
         $dayTimeFilter = new \Elastica\Filter\Nested();
         $dayTimeFilter->setPath('courses');
         $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $categorySearch->getDayTime())));
         $boolFilter->addMust($dayTimeFilter);
     }
     // Filter categories
     $categoryIds = array();
     if ($categorySearch->getSubcategories() instanceof \Traversable) {
         foreach ($categorySearch->getSubcategories() as $category) {
             if (is_object($category)) {
                 $categoryIds[] = $category->getId();
             } else {
                 $categoryIds[] = $category;
             }
         }
     }
     if (empty($categoryIds)) {
         $categoryIds[] = $currentCategory->getId();
         foreach ($currentCategory->getChildren() as $child) {
             $categoryIds[] = $child->getId();
         }
     }
     $categoryFilter = new \Elastica\Filter\BoolOr();
     $mainCategoryFilter = new \Elastica\Filter\Nested();
     $mainCategoryFilter->setPath('category');
     $mainCategoryFilter->setFilter(new \Elastica\Filter\Terms('category._id', array($categoryIds)));
     $subCategoryFilter = new \Elastica\Filter\Nested();
     $subCategoryFilter->setPath('subcategory');
     $subCategoryFilter->setFilter(new \Elastica\Filter\Terms('subcategory._id', array($categoryIds)));
     $additionalCategoryFilter = new \Elastica\Filter\Nested();
     $additionalCategoryFilter->setPath('additionalCategories');
     $additionalCategoryFilter->setFilter(new \Elastica\Filter\Terms('additionalCategories._id', array($categoryIds)));
     $categoryFilter->addFilter($mainCategoryFilter);
     $categoryFilter->addFilter($subCategoryFilter);
     $categoryFilter->addFilter($additionalCategoryFilter);
     $boolFilter->addMust($categoryFilter);
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     $sort = $categorySearch->getSort();
     if (!empty($sort)) {
         $sort = explode(' ', $sort);
         $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc')));
     }
     $paginated = $this->finder->findPaginated($query);
     $paginated->setMaxPerPage($categorySearch->getPerPage())->setCurrentPage($categorySearch->getPage());
     return $paginated;
 }