/**
  * @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))));
 }
 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;
 }
Пример #3
0
 /**
  *
  * @Route("", name="centrale_lille_searchuser")
  * @Method("GET")
  */
 public function searchAction(Request $request)
 {
     $search = new SearchString();
     $searchForm = $this->get('form.factory')->createNamed('', 'string_search_type', $search, array('action' => $this->generateUrl('centrale_lille_searchuser'), 'method' => 'GET'));
     $searchForm->handleRequest($request);
     $search = $searchForm->getData();
     if (is_null($search->getStringSearch())) {
         $result_machine = [];
         $result_user = [];
     } else {
         //Recherche User
         $typeUser = $this->get('fos_elastica.index.fablab.User');
         $query_part_user = new \Elastica\Query\BoolQuery();
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery2 = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('email', $search->getStringSearch());
         $fieldQuery->setFieldQuery('email', $search->getStringSearch());
         $fieldQuery->setFieldFuzziness('email', 0.7);
         $query_part_user->addShould($fieldQuery);
         $fieldQuery2->setFieldQuery('username', $search->getStringSearch());
         $fieldQuery2->setFieldQuery('username', $search->getStringSearch());
         $fieldQuery2->setFieldFuzziness('username', 0.7);
         $query_part_user->addShould($fieldQuery2);
         $filters = new \Elastica\Filter\Bool();
         $query_user = new \Elastica\Query\Filtered($query_part_user, $filters);
         $result_user = $typeUser->search($query_user);
         //Recherche Machine
         $typeMachine = $this->get('fos_elastica.index.fablab.Machine');
         $query_part_machine = new \Elastica\Query\Bool();
         $fieldQuery3 = new \Elastica\Query\Match();
         $fieldQuery4 = new \Elastica\Query\Match();
         $fieldQuery3->setFieldQuery('machine_name', $search->getStringSearch());
         $fieldQuery3->setFieldFuzziness('machine_name', 0.7);
         $fieldQuery3->setFieldMinimumShouldMatch('machine_name', '80%');
         $query_part_machine->addShould($fieldQuery3);
         $fieldQuery4->setFieldQuery('description', $search->getStringSearch());
         $fieldQuery4->setFieldFuzziness('description', 2);
         $fieldQuery4->setFieldMinimumShouldMatch('description', '100%');
         $query_part_machine->addShould($fieldQuery4);
         $filters = new \Elastica\Filter\Bool();
         $query_machine = new \Elastica\Query\Filtered($query_part_machine, $filters);
         $result_machine = $typeMachine->search($query_machine);
         //Recherche Machine
         $typeMachine = $this->get('fos_elastica.index.fablab.Machine');
         $query_part_machine = new \Elastica\Query\Bool();
         $fieldQuery3 = new \Elastica\Query\Match();
         $fieldQuery4 = new \Elastica\Query\Match();
         $fieldQuery3->setFieldQuery('machine_name', $search->getStringSearch());
         // $fieldQuery3->setFieldFuzziness('machine.machine_name', 0.7);
         //$fieldQuery3->setFieldMinimumShouldMatch('machine_name', '80%');
         $query_part_machine->addShould($fieldQuery3);
         $fieldQuery4->setFieldQuery('description', $search->getStringSearch());
         // $fieldQuery4->setFieldFuzziness('machine.machine_name', 0.7);
         //$fieldQuery4->setFieldMinimumShouldMatch('machine_name', '100%');
         $query_part_machine->addShould($fieldQuery4);
     }
     return $this->render('CentraleLilleSearchBundle:Default:search.html.twig', array('result_user' => $result_user, 'result_machine' => $result_machine, 'form' => $searchForm->createView(), 'search' => $search->getStringSearch()));
 }
 /**
  * Lists all Demo entities.
  *
  * @Route("/", name="demo_index")
  * @Method("GET")
  */
 public function indexAction()
 {
     $type = $this->get('fos_elastica.index.fablab.User');
     $query_part = new \Elastica\Query\Bool();
     $query_part->addShould(new \Elastica\Query\Term(array('email' => array('value' => 'cyprien'))));
     $query_part->addShould(new \Elastica\Query\Term(array('username' => array('value' => 'cyprien'))));
     $filters = new \Elastica\Filter\Bool();
     $query = new \Elastica\Query\Filtered($query_part, $filters);
     $result = $type->search($query);
     return $this->render('demo/index.html.twig', array('demos' => $result));
 }
Пример #5
0
 /**
  * @Route("/search/",name="search")
  * @Template()
  */
 public function searchAction(Request $request)
 {
     $finder = $this->container->get('fos_elastica.finder.pages.page');
     $boolQuery = new \Elastica\Query\Bool();
     $boolQuery->addShould(new \Elastica\Query\Fuzzy('title', $request->get('q')));
     $boolQuery->addShould(new \Elastica\Query\Fuzzy('content', $request->get('q')));
     // $fuzzy->addField('content', array('value'=>$request->get('q')));
     $pages = $finder->find($boolQuery);
     /** var array of Acme\UserBundle\Entity\User limited to 10 results */
     // $users = $finder->find('bob', 10);
     return array('pages' => $pages);
 }
 /**
  * @param $searchText
  * @return array<Article>
  */
 public function findSousTitres($searchText)
 {
     $query_part = new \Elastica\Query\Bool();
     $query_part->addShould(new \Elastica\Query\Term(array('value' => array('value' => $searchText, 'boost' => 3))));
     $query_part->addShould(new \Elastica\Query\Term(array('intitule' => array('value' => $searchText))));
     $filters = new \Elastica\Filter\Bool();
     $filters->addMust(new \Elastica\Filter\Term(array('langue' => 'fr')));
     $query = new \Elastica\Query\Filtered($query_part, $filters);
     // return $this->findHybrid($query); pour avoir aussi un ES Result
     $res = $this->find($query);
     return $res;
 }
Пример #7
0
 public function search($q = '')
 {
     if ($q == '') {
         $baseQuery = new \Elastica\Query\MatchAll();
     } else {
         $baseQuery = new \Elastica\Query\Bool();
         $baseQuery->addShould(new \Elastica\Query\Term(array('id' => intval($q))));
         $baseQuery->addShould(new \Elastica\Query\Term(array('amount' => floatval($q))));
         $baseQuery->addShould(new \Elastica\Query\MatchPhrasePrefix('description', $q));
     }
     $query = \Elastica\Query::create($baseQuery);
     $count = $this->finder->createPaginatorAdapter($query, array('search_type' => 'count'))->getTotalHits();
     $query->addSort(array('id' => array('order' => 'asc')));
     $query->setSize($count);
     return $this->find($query);
 }
 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);
 }
 private function configureHighlightingForSource(&$config, $highlightSource)
 {
     global $wgCirrusSearchRegexMaxDeterminizedStates;
     $patterns = array();
     $locale = null;
     $caseInsensitive = false;
     foreach ($highlightSource as $part) {
         if (isset($part['pattern'])) {
             $patterns[] = $part['pattern'];
             $locale = $part['locale'];
             $caseInsensitive |= $part['insensitive'];
         }
     }
     if (count($patterns)) {
         $options = array('regex' => $patterns, 'locale' => $locale, 'regex_flavor' => 'lucene', 'skip_query' => true, 'regex_case_insensitive' => (bool) $caseInsensitive, 'max_determinized_states' => $wgCirrusSearchRegexMaxDeterminizedStates);
         $config['fields']['source_text.plain']['options'] = array_merge($config['fields']['source_text.plain']['options'], $options);
         return;
     }
     $queryStrings = array();
     foreach ($highlightSource as $part) {
         if (isset($part['query'])) {
             $queryStrings[] = $part['query'];
         }
     }
     if (count($queryStrings)) {
         $bool = new \Elastica\Query\Bool();
         foreach ($queryStrings as $queryString) {
             $bool->addShould($queryString);
         }
         $config['fields']['source_text.plain']['highlight_query'] = $bool->toArray();
     }
 }
Пример #10
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);
 }
Пример #11
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);
 }
 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);
 }
 /**
  * Attach the query for the stem field. It will build a set of common
  * terms query if use_common_terms is true for the stems clause or a
  * multi match (cross_fields) if false.
  *
  * @param \Elastica\Query\Bool $query the boolean query to attach the new
  * clause
  * @param array $stemFields of boost field
  * @param string $queryString the query
  */
 private function attachMultiFieldsStemClause(\Elastica\Query\Bool $query, array $stemFields, $queryString)
 {
     if ($this->profile['stems_clause']['use_common_terms'] === true) {
         $bool = new \Elastica\Query\Bool();
         $bool->setMinimumNumberShouldMatch(1);
         $this->attachCommonTermsClause($bool, $stemFields, $queryString, $this->profile['stems_clause']);
         $query->addShould($bool);
     } else {
         $query->addShould($this->buildCrossFields($stemFields, $queryString, $this->profile['stems_clause']['min_should_match']));
     }
 }
Пример #14
0
 /**
  * @param string $q
  * @return \Elastica\Query\AbstractQuery
  */
 protected function _getBaseQuery($q)
 {
     if (empty($q)) {
         $baseQuery = new \Elastica\Query\MatchAll();
     } else {
         $q = $this->_helper->_searchQuery($q);
         $baseQuery = new \Elastica\Query\Bool();
         if ($this->isFuzzyQueryEnabled()) {
             $fuzzy = new \Elastica\Query\FuzzyLikeThis();
             $fuzzy->addFields($this->_getSearchFields(true, $q))->setLikeText($q)->setMinSimilarity($this->getFuzzyMinSimilarity())->setPrefixLength($this->getFuzzyPrefixLength())->setMaxQueryTerms($this->getFuzzyMaxQueryTerms())->setBoost($this->getFuzzyQueryBoost());
             $baseQuery->addShould($fuzzy);
         }
         $queryString = new \Elastica\Query\QueryString($q);
         $queryString->setFields($this->_getSearchFields(true, $q));
         $queryString->setDefaultOperator($this->getQueryOperator());
         $queryString->setBoost('2');
         $baseQuery->addShould($queryString);
     }
     return $baseQuery;
 }
Пример #15
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;
 }
 /**
  *
  * @Route("", name="centrale_lille_search")
  */
 public function searchAction()
 {
     $jsonContentUser = '';
     $jsonContentMachine = '';
     $jsonContentProjet = '';
     $jsonContentSkills = '';
     $jsonTotal = '';
     $datauser = [];
     $dataprojet = [];
     $dataskills = [];
     $datamachine = [];
     $search = $this->get('request')->query->get('searchForm');
     if (is_null($search)) {
         $result_machine = [];
         $result_user = [];
         $result_skills = [];
         $result_projet = [];
     } else {
         //User Json
         $queryall = new \Elastica\Query\MatchAll();
         $typeUser = $this->get('fos_elastica.index.fablab.User');
         $result_alluser = $typeUser->search($queryall)->getResults();
         foreach ($result_alluser as $result) {
             $source = $result->getSource();
             $datauser[] = array('name' => $source['username'], 'link' => 'userId');
         }
         $jsonContentUser = new JsonResponse($datauser, 200, array('Cache-Control' => 'no-cache'));
         //Project Json
         $queryall = new \Elastica\Query\MatchAll();
         $typeProjet = $this->get('fos_elastica.index.fablab.Projet');
         $result_allprojet = $typeProjet->search($queryall)->getResults();
         foreach ($result_allprojet as $result) {
             $source = $result->getSource();
             $dataprojet[] = array('name' => $source['name'], 'link' => 'projectId');
         }
         $jsonContentProjet = new JsonResponse($dataprojet, 200, array('Cache-Control' => 'no-cache'));
         //Skills Json
         $queryall = new \Elastica\Query\MatchAll();
         $typeSkills = $this->get('fos_elastica.index.fablab.Competence');
         $result_allskills = $typeSkills->search($queryall)->getResults();
         foreach ($result_allskills as $result) {
             $source = $result->getSource();
             $dataskills[] = array('name' => $source['name'], 'link' => 'skillsId');
         }
         $jsonContentSkills = new JsonResponse($dataskills, 200, array('Cache-Control' => 'no-cache'));
         //Machine Json
         $queryall = new \Elastica\Query\MatchAll();
         $typeMachine = $this->get('fos_elastica.index.fablab.Machine');
         $result_allmachine = $typeMachine->search($queryall)->getResults();
         foreach ($result_allmachine as $result) {
             $source = $result->getSource();
             $type = $result->getType();
             $datamachine[] = array('name' => $source['name'], 'link' => 'machineId');
         }
         $jsonContentMachine = new JsonResponse($datamachine, 200, array('Cache-Control' => 'no-cache'));
         $jsonContentMachine = '"machine": ' . substr($jsonContentMachine, 83);
         $jsonContentUser = '******' . substr($jsonContentUser, 83);
         $jsonContentSkills = '"competence": ' . substr($jsonContentSkills, 83);
         $jsonContentProjet = '"projet": ' . substr($jsonContentProjet, 83);
         $jsonTotal = '{' . $jsonContentUser . ',' . $jsonContentMachine . ',' . $jsonContentProjet . ',' . $jsonContentSkills . '}';
         //Recherche User
         $typeUser = $this->get('fos_elastica.index.fablab.User');
         $query_part_user = new \Elastica\Query\BoolQuery();
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery2 = new \Elastica\Query\Match();
         $fieldQuery9 = new \Elastica\Query\Match();
         $fieldQuery10 = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('email', $search);
         $fieldQuery->setFieldFuzziness('email', 0.2);
         $query_part_user->addShould($fieldQuery);
         $fieldQuery9->setFieldQuery('firstname', $search);
         $fieldQuery9->setFieldFuzziness('firstname', 0.2);
         $query_part_user->addShould($fieldQuery9);
         $fieldQuery10->setFieldQuery('lastname', $search);
         $fieldQuery10->setFieldFuzziness('lastname', 0.2);
         $query_part_user->addShould($fieldQuery10);
         $fieldQuery2->setFieldQuery('username', $search);
         $fieldQuery2->setFieldFuzziness('username', 0.2);
         $query_part_user->addShould($fieldQuery2);
         $filters = new \Elastica\Filter\Bool();
         $query_user = new \Elastica\Query\Filtered($query_part_user, $filters);
         $result_user = $typeUser->search($query_user);
         //Recherche Machine
         $typeMachine = $this->get('fos_elastica.index.fablab.Machine');
         $query_part_machine = new \Elastica\Query\Bool();
         $fieldQuery3 = new \Elastica\Query\Match();
         $fieldQuery4 = new \Elastica\Query\Match();
         $fieldQuery3->setFieldQuery('name', $search);
         $fieldQuery3->setFieldFuzziness('name', 0.2);
         $fieldQuery3->setFieldMinimumShouldMatch('name', '80%');
         $query_part_machine->addShould($fieldQuery3);
         $fieldQuery4->setFieldQuery('description', $search);
         $fieldQuery4->setFieldFuzziness('description', 2);
         $fieldQuery4->setFieldMinimumShouldMatch('description', '100%');
         $query_part_machine->addShould($fieldQuery4);
         $filters = new \Elastica\Filter\Bool();
         $query_machine = new \Elastica\Query\Filtered($query_part_machine, $filters);
         $result_machine = $typeMachine->search($query_machine);
         //Recherche Skills
         $typeSkills = $this->get('fos_elastica.index.fablab.Competence');
         $query_part_skills = new \Elastica\Query\Bool();
         $fieldQuery5 = new \Elastica\Query\Match();
         $fieldQuery5->setFieldQuery('name', $search);
         $fieldQuery5->setFieldFuzziness('name', 2);
         $fieldQuery5->setFieldMinimumShouldMatch('name', '40%');
         $query_part_skills->addShould($fieldQuery5);
         $filters = new \Elastica\Filter\Bool();
         $query_skills = new \Elastica\Query\Filtered($query_part_skills, $filters);
         $result_skills = $typeSkills->search($query_skills);
         //Recherche projet
         $typeProjet = $this->get('fos_elastica.index.fablab.Projet');
         $query_part_projet = new \Elastica\Query\Bool();
         $fieldQuery6 = new \Elastica\Query\Match();
         $fieldQuery7 = new \Elastica\Query\Match();
         $fieldQuery6->setFieldQuery('name', $search);
         $fieldQuery6->setFieldFuzziness('name', 0.2);
         $fieldQuery6->setFieldMinimumShouldMatch('name', '80%');
         $query_part_projet->addShould($fieldQuery6);
         $fieldQuery7->setFieldQuery('summary', $search);
         $fieldQuery7->setFieldFuzziness('summary', 2);
         $fieldQuery7->setFieldMinimumShouldMatch('summary', '100%');
         $query_part_projet->addShould($fieldQuery7);
         $filters = new \Elastica\Filter\Bool();
         $query_projet = new \Elastica\Query\Filtered($query_part_projet, $filters);
         $result_projet = $typeProjet->search($query_projet);
         //
         $encoder = array(new JsonEncoder());
         $normalizer = array(new ObjectNormalizer());
         $serializer = new Serializer($normalizer, $encoder);
     }
     return $this->render('CentraleLilleSearchBundle:Default:search.html.twig', array('result_user' => $result_user, 'result_machine' => $result_machine, 'result_competence' => $result_skills, 'result_project' => $result_projet, 'search' => $search, 'userjson' => $jsonContentUser, 'machinejson' => $jsonContentMachine, 'jsonTotal' => $jsonTotal));
 }
Пример #17
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);
 }