public function testSetDefaultField() { $default = 'field1'; $query = new QueryString('test'); $query->setDefaultField($default); $data = $query->toArray(); $this->assertEquals($data['query_string']['default_field'], $default); }
/** * simple search with an operator and words * * @param string $words data * @param integer $start the begining of the paging * @param integer $limit the interval of the paging * @param bool $aggregation parameter the search to be aggregated or not. * @param integer $sejour_id the id of the sejour * @param string $specific_user the ids of users selected * @param bool $details details of query * @param string $date date of query * @param bool $fuzzy_search fuzzy the query * * @return \Elastica\Query */ function searchQueryString($words, $start = 0, $limit = 30, $aggregation = false, $sejour_id = null, $specific_user = null, $details = null, $date = null, $fuzzy_search = null) { // Initialisation des mots pour la recherche $prats = $this->constructWordsWithPrat($specific_user, $sejour_id); $sejour = $this->constructWordsWithSejour($sejour_id); $words = CmbString::normalizeUtf8(stripcslashes($words)); $query_bool = new Elastica\Query\Bool(); //query date if ($date) { $query_date = new Elastica\Query\QueryString(); $query_date->setQuery($date); $query_date->setDefaultOperator("and"); $query_bool->addMust($query_date); } //query mots if ($words) { if ($fuzzy_search) { $query_fuzzy = new Elastica\Query\FuzzyLikeThis(); $query_fuzzy->addFields(array("body", "title")); $query_fuzzy->setLikeText($words); $query_fuzzy->setMinSimilarity(0.3); $query_fuzzy->setMaxQueryTerms(3); $query_bool->addMust($query_fuzzy); } else { $query_words = new Elastica\Query\QueryString($words); $query_words->setFields(array("body", "title")); $query_words->setDefaultOperator("and"); $query_bool->addMust($query_words); } } //query détails if ($details) { $query_details = new Elastica\Query\QueryString(); $query_details->setQuery($details); $query_details->setDefaultOperator("and"); $query_bool->addMust($query_details); } else { // query prat_id $query_prat = new Elastica\Query\QueryString(); $query_prat->setQuery("prat_id:({$prats})"); $query_prat->setDefaultField("prat_id"); $query_bool->addMust($query_prat); //query sejour if ($sejour) { $query_sejour = new Elastica\Query\QueryString(); $query_sejour->setQuery($sejour); $query_sejour->setDefaultOperator("and"); $query_bool->addMust($query_sejour); } } $query = new Query($query_bool); //create aggregation if ($aggregation && $aggregation != "by_type") { // 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_class = new CSearchAggregation("Terms", "ref_class", "object_ref_class", 10); $sub_agg_by_id = new CSearchAggregation("Terms", "sub_ref_id", "object_ref_id", 100); $sub_agg_by_type = new CSearchAggregation("Terms", "sub_ref_type", "_type", 100); $sub_agg_by_id->_aggregation->addAggregation($sub_agg_by_type->_aggregation); $agg_by_class->_aggregation->addAggregation($sub_agg_by_id->_aggregation); $query->addAggregation($agg_by_class->_aggregation); } else { if (!$aggregation) { // Pagination $query->setFrom($start); // Where to start $query->setLimit($limit); } else { $agg_by_type = new CSearchAggregation("Terms", "ref_type", "_type", 100); $query->addAggregation($agg_by_type->_aggregation); } } //Highlight if ($words) { $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" => $words))), "minimum_should_match" => 1)))))); } return $query; }