If no operator is set, OR is chosen
public setDefaultOperator ( string $operator ) | ||
$operator | string | Operator |
/** * @return ElasticaQuery */ private function prepareQuery($params, $start = null, $limit = null) { $query = null; $filter = null; $sort = ['_score' => 'desc']; // We'd like to search in both title and description for keywords if (!empty($params['keywords'])) { $query = new QueryString($params['keywords']); $query->setDefaultOperator('AND')->setFields(['title', 'description']); } // Add location filter is location is selected from autosuggest if (!empty($params['location_id'])) { $location = Location::find($params['location_id']); $filter = new GeoDistance('location', ['lat' => $location->lat, 'lon' => $location->lon], $params['radius'] . 'mi'); // Sort by nearest hit $sort = ['_geo_distance' => ['jobs.location' => [(double) $location->lon, (double) $location->lat], 'order' => 'asc', 'unit' => 'mi']]; } // If neither keyword nor location supplied, then return all if (empty($params['keywords']) && empty($params['location_id'])) { $query = new MatchAll(); } // We need a filtered query $elasticaQuery = new ElasticaQuery(new Filtered($query, $filter)); $elasticaQuery->addSort($sort); // Offset and limits if (!is_null($start) && !is_null($limit)) { $elasticaQuery->setFrom($start)->setSize($limit); } // Set up the highlight $elasticaQuery->setHighlight(['order' => 'score', 'fields' => ['title' => ['fragment_size' => 100], 'description' => ['fragment_size' => 200]]]); return $elasticaQuery; }
public function testSetDefaultOperator() { $operator = 'AND'; $query = new QueryString('test'); $query->setDefaultOperator($operator); $data = $query->toArray(); $this->assertEquals($data['query_string']['default_operator'], $operator); }
public function searchAction(Request $request) { $queryRaw = $request->query->get('q'); if (!$queryRaw) { throw $this->createNotFoundException('No query provided.'); } $query = new QueryString($queryRaw); $query->setDefaultOperator('AND'); try { $finder = $this->get('fos_elastica.finder.accard'); $result = $finder->findHybrid($query, 25); $results = new SearchCollection($query, $result); $down = false; } catch (HttpException $e) { $queryRaw = 'Search is down'; $results = new SearchCollection($query, array()); $down = true; } return $this->render('AccardWebBundle:Frontend:search.html.twig', array('last_search' => $queryRaw, 'results' => $results, 'search_down' => $down)); }
/** * @param Escaper $escaper * @param SearchContext $context * @param string $value * @param bool $updateHighlightSourceRef * @param callable $fieldF * @return callable */ private static function insourceOrIntitle($escaper, $context, $value, $updateHighlightSourceRef, $fieldF) { list($queryString, $fuzzyQuery) = $escaper->fixupWholeQueryString($escaper->fixupQueryStringPart($value)); $field = $fieldF($queryString); $query = new \Elastica\Query\QueryString($queryString); $query->setFields(array($field)); $query->setDefaultOperator('AND'); $query->setAllowLeadingWildcard($escaper->getAllowLeadingWildcard()); $query->setFuzzyPrefixLength(2); $query->setRewrite('top_terms_boost_1024'); $wrappedQuery = $context->wrapInSaferIfPossible($query, false); $updateReferences = function (&$fuzzyQueryRef, &$filterDestinationRef, &$highlightSourceRef, &$searchContainedSyntaxRef) use($fuzzyQuery, $wrappedQuery, $updateHighlightSourceRef) { $fuzzyQueryRef = $fuzzyQuery; $filterDestinationRef[] = new \Elastica\Filter\Query($wrappedQuery); if ($updateHighlightSourceRef) { $highlightSourceRef[] = array('query' => $wrappedQuery); } $searchContainedSyntaxRef = true; }; return $updateReferences; }
/** * Search contents. * * @return array $elasticsearches Combine of all results, total and aggregations * * @since 1.5.0 */ public function searchContents() { //Return array $return = array('query' => array('search' => '', 'type' => '', 'paged' => 0, 'perpage' => 0), 'total' => 0, 'types' => array(), 'results' => array()); //Check page if (!is_search()) { return $return; } //Get query vars $request = isset($_REQUEST) ? $_REQUEST : array(); $results = array(); $types = array(); //Check request if (empty($request)) { return $return; } //Get Elasticsearch datas $index = $this->getIndex(); //Check index if (null === $index || empty($index)) { return $return; } //Get search datas $search = isset($request['s']) ? str_replace('\\"', '"', $request['s']) : ''; //Return everything if (empty($search)) { return $return; } //Get search datas $type = isset($request['type']) ? $request['type'] : ''; $paged = isset($request['paged']) && !empty($request['paged']) ? $request['paged'] - 1 : 0; $perpage = isset($request['perpage']) ? $request['perpage'] : TeaThemeOptions::getOption('posts_per_page', 10); //Build query string $es_querystring = new QueryString(); //'And' or 'Or' default: 'Or' $es_querystring->setDefaultOperator('OR'); $es_querystring->setQuery($search); //Create the actual search object with some data. $es_query = new Query(); $es_query->setQuery($es_querystring); //Define options $es_query->setFrom($paged); //Start $es_query->setLimit($perpage); //How many //Search! $es_resultset = $index->search($es_query); //Retrieve data $es_results = $es_resultset->getResults(); //Check results if (null == $es_results || empty($es_results)) { $return['query']['search'] = str_replace(' ', '+', $search); return $return; } //Iterate to retrieve all IDs foreach ($es_results as $res) { $typ = $res->getType(); //Save type $types[$typ] = $typ; //Save datas $results[$typ][] = array('id' => $res->getId(), 'score' => $res->getScore(), 'source' => $res->getSource()); } //Get total $total = $es_resultset->getTotalHits(); //Return everything $return = array('query' => array('search' => str_replace(' ', '+', $search), 'type' => $type, 'paged' => $paged, 'perpage' => $perpage), 'total' => $total, 'types' => $types, 'results' => $results); return $return; }
/** * method to search log details * * @param string $operator the operator for the query * @param string $words the words * @param string $names_types the types to search * * @return \Elastica\ResultSet */ function searchQueryLogDetails($operator, $words, $names_types = null) { $words = CmbString::normalizeUtf8(stripcslashes($words)); // Define a Query. We want a string query. $elasticaQueryString = new QueryString(); //'And' or 'Or' default : 'Or' $elasticaQueryString->setDefaultOperator($operator); $elasticaQueryString->setQuery($words); // Create the actual search object with some data. $elasticaQuery = new Query(); $elasticaQuery->setQuery($elasticaQueryString); //Search on the index. $index = CAppUI::conf("search index_name") . "_log"; $this->_index = $this->loadIndex($index); $search = new \Elastica\Search($this->_client); $search->addIndex($this->_index); if ($names_types) { $search->addTypes($names_types); } $elasticaQuery->setFrom(0); // Where to start $elasticaQuery->setLimit(1000); return $search->search($elasticaQuery); }
/** * simple search with an operator and words * * @param string $operator 'And' or 'Or' default : 'Or' * @param string $words data * @param integer $start the begining of the paging * @param integer $limit the interval of the paging * @param array $names_types the restrictive type(s) where the search take place. * @param bool $aggregation parameter the search to be aggregated or not. * * @return \Elastica\ResultSet */ function searchQueryString($operator, $words, $start = 0, $limit = 30, $names_types = null, $aggregation = false) { $words = CSearch::normalizeEncoding($words); // Define a Query. We want a string query. $queryString = new Elastica\Query\QueryString($words); $queryString->setDefaultOperator("and"); // Create the actual search object with some data. $query = new Elastica\Query($queryString); //create aggregation if ($aggregation) { // on aggrège d'abord par class d'object référents // on effectue un sous aggrégation par id des objets référents. $agg_by_date = new CSearchAggregation("Terms", "date_log", "date", 10); $sub_agg_by_user = new CSearchAggregation("Terms", "user_id", "user_id", 10); $sub_agg_by_contexte = new CSearchAggregation("Terms", "contexte", "_type", 10); $sub_agg_by_user->_aggregation->addAggregation($sub_agg_by_contexte->_aggregation); $agg_by_date->_aggregation->addAggregation($sub_agg_by_user->_aggregation); $query->addAggregation($agg_by_date->_aggregation); } else { // Pagination $query->setFrom($start); // Where to start $query->setLimit($limit); } //Highlight $query->setHighlight(array("fields" => array("body" => array("pre_tags" => array(" <em> <strong> "), "post_tags" => array(" </strong> </em>"), "fragment_size" => 80, "number_of_fragments" => 10)))); //Search on the index. $index = CAppUI::conf("search index_name") . "_log"; $index = $this->loadIndex($index); $search = new \Elastica\Search($this->_client); $search->addIndex($index); if ($names_types) { $search->addTypes($names_types); } return $search->search($query); }
/** * @param string[] $fields * @param string $queryString * @param int $phraseSlop * @param boolean $isRescore * @return \Elastica\Query\Simple */ private function buildSearchTextQueryForFields(array $fields, $queryString, $phraseSlop, $isRescore) { $query = new \Elastica\Query\QueryString($queryString); $query->setFields($fields); $query->setAutoGeneratePhraseQueries(true); $query->setPhraseSlop($phraseSlop); $query->setDefaultOperator('AND'); $query->setAllowLeadingWildcard($this->config->get('CirrusSearchAllowLeadingWildcard')); $query->setFuzzyPrefixLength(2); $query->setRewrite('top_terms_boost_1024'); $states = $this->config->get('CirrusSearchQueryStringMaxDeterminizedStates'); if (isset($states)) { // Requires ES 1.4+ $query->setParam('max_determinized_states', $states); } return $this->wrapInSaferIfPossible($query, $isRescore); }
/** * 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; }