Exemplo n.º 1
0
 public function testSetSort()
 {
     $index = $this->_createIndex();
     $type = $index->getType('test');
     $doc = new Elastica_Document(1, array('name' => 'hello world'));
     $type->addDocument($doc);
     $doc = new Elastica_Document(2, array('firstname' => 'guschti', 'lastname' => 'ruflin'));
     $type->addDocument($doc);
     $doc = new Elastica_Document(3, array('firstname' => 'nicolas', 'lastname' => 'ruflin'));
     $type->addDocument($doc);
     $queryTerm = new Elastica_Query_Term();
     $queryTerm->setTerm('lastname', 'ruflin');
     $index->refresh();
     $query = Elastica_Query::create($queryTerm);
     // ASC order
     $query->setSort(array(array('firstname' => array('order' => 'asc'))));
     $resultSet = $type->search($query);
     $this->assertEquals(2, $resultSet->count());
     $first = $resultSet->current()->getData();
     $second = $resultSet->next()->getData();
     $this->assertEquals('guschti', $first['firstname']);
     $this->assertEquals('nicolas', $second['firstname']);
     // DESC order
     $query->setSort(array('firstname' => array('order' => 'desc')));
     $resultSet = $type->search($query);
     $this->assertEquals(2, $resultSet->count());
     $first = $resultSet->current()->getData();
     $second = $resultSet->next()->getData();
     $this->assertEquals('nicolas', $first['firstname']);
     $this->assertEquals('guschti', $second['firstname']);
 }
Exemplo n.º 2
0
 public function testMappingStoreFields()
 {
     $client = new Elastica_Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     $mapping = new Elastica_Type_Mapping($type, array('firstname' => array('type' => 'string', 'store' => 'yes'), 'lastname' => array('type' => 'string')));
     $mapping->disableSource();
     $type->setMapping($mapping);
     $firstname = 'Nicolas';
     $doc = new Elastica_Document(1, array('firstname' => $firstname, 'lastname' => 'Ruflin'));
     $type->addDocument($doc);
     $index->refresh();
     $queryString = new Elastica_Query_QueryString('ruflin');
     $query = Elastica_Query::create($queryString);
     $query->setFields(array('*'));
     $resultSet = $type->search($query);
     $result = $resultSet->current();
     $fields = $result->getFields();
     $this->assertEquals($firstname, $fields['firstname']);
     $this->assertArrayNotHasKey('lastname', $fields);
     $this->assertEquals(1, count($fields));
     $index->flush();
     $document = $type->getDocument(1);
     $this->assertEmpty($document->getData());
 }
Exemplo n.º 3
0
 /**
  * Counts results of query
  *
  * @param  string|array|Elastica_Query $query Array with all query data inside or a Elastica_Query object
  * @return int                         number of documents matching the query
  * @see Elastica_Searchable::count
  */
 public function count($query = '')
 {
     $query = Elastica_Query::create($query);
     $path = '_search';
     $response = $this->request($path, Elastica_Request::GET, $query->toArray(), array('search_type' => 'count'));
     $resultSet = new Elastica_ResultSet($response);
     return $resultSet->getTotalHits();
 }
Exemplo n.º 4
0
 /**
  * Sets query object
  *
  * @param string|Elastica_Query|Elastica_Query_Abstract $query
  * @return Elastica_Filter_HasChild Current object
  */
 public function setQuery($query)
 {
     $query = Elastica_Query::create($query);
     $data = $query->toArray();
     return $this->setParam('query', $data['query']);
 }
Exemplo n.º 5
0
 /**
  * Search in the set indices, types
  *
  * @param mixed $query
  * @return Elastica_ResultSet
  */
 public function search($query)
 {
     $query = Elastica_Query::create($query);
     $path = $this->getPath();
     $response = $this->getClient()->request($path, Elastica_Request::GET, $query->toArray());
     return new Elastica_ResultSet($response);
 }
Exemplo n.º 6
0
	public function testSetTypeArraySearchSingle() {
		$filter = new Elastica_Filter_Ids();
		$filter->setIds('4');
		$filter->setType(array('helloworld1', 'helloworld2'));

		$query = Elastica_Query::create($filter);
		$resultSet = $this->_index->search($query);

		$this->assertEquals(2, $resultSet->count());
	}
Exemplo n.º 7
0
 /**
  * More like this query based on the given object
  *
  * The id in the given object has to be set
  *
  * @param Elastica_Document      $doc  Document to query for similar objects
  * @param array                  $params OPTIONAL Additional arguments for the query
  * @param Elastica_Query         $query OPTIONAL Query to filter the moreLikeThis results
  * @return Elastica_ResultSet    ResultSet with all results inside
  * @link http://www.elasticsearch.org/guide/reference/api/more-like-this.html
  */
 public function moreLikeThis(Elastica_Document $doc, $params = array(), $query = array())
 {
     $path = $doc->getId() . '/_mlt';
     $query = Elastica_Query::create($query);
     $response = $this->request($path, Elastica_Request::GET, $query->toArray(), $params);
     return new Elastica_ResultSet($response);
 }
Exemplo n.º 8
0
 /**
  * {@inheritDoc}
  */
 public function count($query)
 {
     $query = Elastica_Query::create($query);
     $path = '_count';
     $data = $this->request($path, Elastica_Request::GET, $query->getQuery())->getData();
     return (int) $data['count'];
 }
Exemplo n.º 9
0
 /**
  * Deletes entries in the db based on a query
  *
  * @param Elastica_Query|string $query Query object
  * @link http://www.elasticsearch.org/guide/reference/api/delete-by-query.html
  */
 public function deleteByQuery($query)
 {
     $query = Elastica_Query::create($query);
     return $this->request('_query', Elastica_Request::DELETE, $query->getQuery());
 }
Exemplo n.º 10
0
 /**
  * Search in the set indices, types
  *
  * @param  mixed              $query
  * @param  int|array          $options OPTIONAL Limit or associative array of options (option=>value)
  * @return Elastica_ResultSet
  */
 public function search($query, $options = null)
 {
     $query = Elastica_Query::create($query);
     $path = $this->getPath();
     $params = array();
     if (is_int($options)) {
         $query->setLimit($options);
     } else {
         if (is_array($options)) {
             foreach ($options as $key => $value) {
                 switch ($key) {
                     case 'limit':
                         $query->setLimit($value);
                         break;
                     case 'routing':
                         $params['routing'] = $value;
                         break;
                     case 'search_type':
                         $params['search_type'] = $value;
                         break;
                     default:
                         throw new Elastica_Exception_Invalid('Invalid option ' . $key);
                         break;
                 }
             }
         }
     }
     $response = $this->getClient()->request($path, Elastica_Request::GET, $query->toArray(), $params);
     return new Elastica_ResultSet($response);
 }
Exemplo n.º 11
0
 /**
  * Search in the set indices, types
  *
  * @param mixed $query
  * @param int|array $options OPTIONAL Limit or associative array of options (option=>value)
  * @return Elastica_ResultSet
  */
 public function search($query, $options = null)
 {
     $query = Elastica_Query::create($query);
     $path = $this->getPath();
     if (is_int($options)) {
         $query->setLimit($options);
     } else {
         if (is_array($options)) {
             foreach ($options as $key => $value) {
                 if (empty($value)) {
                     throw new Elastica_Exception_Invalid('Invalid value ' . $value . ' for option ' . $key);
                 } else {
                     $path_separator = strpos($path, '?') ? '&' : '?';
                     switch ($key) {
                         case 'limit':
                             $query->setLimit($value);
                             break;
                         case 'routing':
                             if (!empty($value)) {
                                 $path .= $path_separator . 'routing=' . $value;
                             }
                             break;
                         case 'search_type':
                             if (!empty($value)) {
                                 $path .= $path_separator . 'search_type=' . $value;
                             }
                             break;
                         default:
                             throw new Elastica_Exception_Invalid('Invalid option ' . $key);
                             break;
                     }
                 }
             }
         }
     }
     $response = $this->getClient()->request($path, Elastica_Request::GET, $query->toArray());
     return new Elastica_ResultSet($response);
 }
Exemplo n.º 12
0
 /**
  * Registers a percolator query
  *
  * @param  string                                        $name  Query name
  * @param  string|Elastica_Query|Elastica_Query_Abstract $query Query to add
  * @return Elastica_Response
  */
 public function registerQuery($name, $query)
 {
     $path = '_percolator/' . $this->_index->getName() . '/' . $name;
     $query = Elastica_Query::create($query);
     return $this->_index->getClient()->request($path, Elastica_Request::PUT, $query->toArray());
 }
Exemplo n.º 13
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();
 }