Beispiel #1
0
	public function testFilter() {
		$client = new Elastica_Client();
		$index = $client->getIndex('test');
		$index->create(array(), true);
		$type = $index->getType('helloworld');
		
		$type->addDocument(new Elastica_Document(1, array('color' => 'red')));
		$type->addDocument(new Elastica_Document(2, array('color' => 'green')));
		$type->addDocument(new Elastica_Document(3, array('color' => 'blue')));
		
		$index->refresh();
		
		$termQuery = new Elastica_Query_Term(array('color' => 'red'));
		
		$facet = new Elastica_Facet_Query('test');
		$facet->setQuery($termQuery);
		
		$query = new Elastica_Query();
		$query->addFacet($facet);
		
		$resultSet = $type->search($query);
		
		$facets = $resultSet->getFacets();
		
		$this->assertEquals(1, $facets['test']['count']);
	}
Beispiel #2
0
 /**
  * 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();
 }