private function searchCombination(SearchParameterBag $searchParams) { $query = new \Elastica_Query_Text(); $query->setFieldQuery('name', $searchParams->get('term')); $query->setFieldParam('name', 'type', 'phrase_prefix'); $this->results = $this->container->get('foq_elastica.finder.treatments')->find($query); $client = $this->container->get('foq_elastica.client'); $search = new \Elastica_Search($client); $postType = $this->get('foq_elastica.index.acme.post'); $tagType = $this->get('foq_elastica.index.acme.tag'); // add the types to the search $search->addType($postType)->addType($tagType); $index = $this->get('foq_elastica.index'); $search->addIndex($index); $searchTerm = $request->query->get('terms'); $postSubjectQuery = new \Elastica_Query_Text(); $postSubjectQuery->setFieldQuery('subject', $searchTerm); $postSubjectQuery->setFieldParam('subject', 'analyzer', 'snowball'); $tagQuery = new \Elastica_Query_Text(); $tagQuery->setFieldQuery('tagname', $searchTerm); $tagQuery->setFieldParam('tagname', 'analyzer', 'snowball'); $boolQuery = new \Elastica_Query_Bool(); $boolQuery->addShould($nameQuery); $boolQuery->addShould($keywordsQuery); $results = $search->search($boolQuery); return array('results' => $results); }
/** * Test to resolve the following issue * * https://groups.google.com/forum/?fromgroups#!topic/elastica-php-client/zK_W_hClfvU */ public function testToArrayStructure() { $boolQuery = new Elastica_Query_Bool(); $term1 = new Elastica_Query_Term(); $term1->setParam('interests', 84); $term2 = new Elastica_Query_Term(); $term2->setParam('interests', 92); $boolQuery->addShould($term1)->addShould($term2); $jsonString = '{"bool":{"should":[{"term":{"interests":84}},{"term":{"interests":92}}]}}'; $this->assertEquals($jsonString, json_encode($boolQuery->toArray())); }
public function testToArray() { $query = new Elastica_Query_Bool(); $idsQuery1 = new Elastica_Query_Ids(); $idsQuery1->setIds(1); $idsQuery2 = new Elastica_Query_Ids(); $idsQuery2->setIds(2); $idsQuery3 = new Elastica_Query_Ids(); $idsQuery3->setIds(3); $boost = 1.2; $minMatch = 2; $query->setBoost($boost); $query->setMinimumNumberShouldMatch($minMatch); $query->addMust($idsQuery1); $query->addMustNot($idsQuery2); $query->addShould($idsQuery3->toArray()); $expectedArray = array('bool' => array('must' => array($idsQuery1->toArray()), 'should' => array($idsQuery3->toArray()), 'minimum_number_should_match' => $minMatch, 'must_not' => array($idsQuery2->toArray()), 'boost' => $boost)); $this->assertEquals($expectedArray, $query->toArray()); }
/** * Handles search and facets. * * @param string $q * @param array $params * @param string $type * @return Elastica_ResultSet * @throws Exception */ public function search($q, $params = array(), $type = 'product') { if ($this->getStatus()->indexExists($this->_index)) { if (empty($params['filters'])) { $params['filters'] = '*'; } $queryFilter = new Elastica_Filter_Query(new Elastica_Query_QueryString($params['filters'])); if (isset($params['range_filters']) && !empty($params['range_filters'])) { $andFilter = new Elastica_Filter_And(); $andFilter->addFilter($queryFilter); $filter = new Elastica_Filter_Range(); foreach ($params['range_filters'] as $field => $rangeFilter) { $filter->addField($field, $rangeFilter); } $andFilter->addFilter($filter); $queryFilter = $andFilter; } if (empty($q)) { $baseQuery = new Elastica_Query_MatchAll(); } else { $baseQuery = new Elastica_Query_Bool(); if ($this->isFuzzyQueryEnabled()) { $fields = $this->_getSearchFields(true, $q); $queryFuzzy = new Elastica_Query_FuzzyLikeThis(); $queryFuzzy->addFields($fields); $queryFuzzy->setLikeText($q); $queryFuzzy->setMinSimilarity($this->getFuzzyMinSimilarity()); $queryFuzzy->setPrefixLength($this->getFuzzyPrefixLength()); $queryFuzzy->setMaxQueryTerms($this->getFuzzyMaxQueryTerms()); $queryFuzzy->setBoost($this->getFuzzyQueryBoost()); $baseQuery->addShould($queryFuzzy); } $queryString = new Elastica_Query_QueryString($q); $queryString->setFields($this->_getSearchFields(false, $q)); $baseQuery->addShould($queryString); } $filteredQuery = new Elastica_Query_Filtered($baseQuery, $queryFilter); $query = Elastica_Query::create($filteredQuery)->setFrom($params['offset'])->setLimit($params['limit']); if (isset($params['facets']['queries']) && !empty($params['facets']['queries'])) { foreach ($params['facets']['queries'] as $facetQuery) { $facet = new Elastica_Facet_Query($facetQuery); $facet->setParam('query_string', array('query' => $facetQuery)); $query->addFacet($facet); } } if (isset($params['stats']['fields']) && !empty($params['stats']['fields'])) { foreach ($params['stats']['fields'] as $field) { $facet = new Elastica_Facet_Statistical($field); $facet->setParam('field', $field); $query->addFacet($facet); } } else { if (isset($params['facets']['fields']) && !empty($params['facets']['fields'])) { $properties = $this->_getIndexProperties(); foreach ($params['facets']['fields'] as $field) { if (array_key_exists($field, $properties)) { $facet = new Elastica_Facet_Terms($field); if ($properties[$field]['type'] == 'multi_field') { $field .= '.untouched'; } $facet->setField($field); $facet->setParam('all_terms', true); $facet->setSize($this->getFacetsMaxSize()); $query->addFacet($facet); } } } if (isset($params['facets']['ranges']) && !empty($params['facets']['ranges'])) { foreach ($params['facets']['ranges'] as $field => $ranges) { $facet = new Elastica_Facet_Range($field); $facet->setField($field); $facet->setRanges($ranges); $query->addFacet($facet); } } } if (isset($params['sort']) && !empty($params['sort'])) { foreach ($params['sort'] as $sort) { $query->addSort($sort); } } $result = $this->getIndex($this->_index)->getType($type)->search($query); return $result; } return array(); }