Exemplo n.º 1
0
 /**
  * @param string                      $indexName
  * @param string                      $typeName
  * @param CM_Elasticsearch_Query|null $query
  * @return int
  * @throws CM_Exception_Invalid
  */
 public function count($indexName, $typeName, CM_Elasticsearch_Query $query = null)
 {
     $requestParams = ['index' => $indexName, 'type' => $typeName];
     if (null !== $query) {
         $requestParams['body']['query'] = $query->getQuery();
     }
     $responseCount = $this->_getClient()->count($requestParams);
     if (!isset($responseCount['count'])) {
         throw new CM_Exception_Invalid('Count query to index returned invalid value', null, ['indexName' => $indexName, 'typeName' => $typeName]);
     }
     return (int) $responseCount['count'];
 }
Exemplo n.º 2
0
 public function testUpdateItemWithJob()
 {
     $query = new CM_Elasticsearch_Query();
     $query->queryMatchMulti(array('name'), 'foo');
     $source = new CM_PagingSource_Elasticsearch($this->_type, $query);
     $this->assertSame(0, $source->getCount());
     $id1 = $this->_type->createEntry('foo');
     $id2 = $this->_type->createEntry('foo bar');
     $this->assertSame(2, $source->getCount());
     $this->assertEquals(array($id1, $id2), $source->getItems());
     CM_Db_Db::update('index_mock', array('name' => 'bar'), array('id' => $id2));
     $this->_type->updateItemWithJob(array('id' => $id2));
     $this->assertSame(1, $source->getCount());
     $this->assertEquals(array($id1), $source->getItems());
 }
Exemplo n.º 3
0
 /**
  * @param int|null $offset
  * @param int|null $count
  * @return array
  */
 private function _getResult($offset = null, $count = null)
 {
     $cacheKey = array($this->_query->getSort(), $offset, $count);
     if (($result = $this->_cacheGet($cacheKey)) === false) {
         $data = array('query' => $this->_query->getQuery(), 'sort' => $this->_query->getSort());
         if ($this->_fields) {
             $data['fields'] = $this->_fields;
         }
         if ($offset !== null) {
             $data['from'] = $offset;
         }
         if ($count !== null) {
             $data['size'] = $count;
         }
         $searchResult = CM_Service_Manager::getInstance()->getElasticsearch()->query($this->_types, $data);
         $result = array('items' => array(), 'total' => 0);
         if (isset($searchResult['hits'])) {
             foreach ($searchResult['hits']['hits'] as $hit) {
                 if ($this->_fields && array_key_exists('fields', $hit)) {
                     if ($this->_returnType) {
                         $idArray = array('id' => $hit['_id'], 'type' => $hit['_type']);
                     } else {
                         $idArray = array('id' => $hit['_id']);
                     }
                     $fields = $hit['fields'];
                     $fields = Functional\map($fields, function ($field) {
                         if (is_array($field) && 1 == count($field)) {
                             $field = reset($field);
                         }
                         return $field;
                     });
                     $result['items'][] = array_merge($idArray, $fields);
                 } else {
                     if ($this->_returnType) {
                         $item = array('id' => $hit['_id'], 'type' => $hit['_type']);
                     } else {
                         $item = $hit['_id'];
                     }
                     $result['items'][] = $item;
                 }
             }
             $result['total'] = $searchResult['hits']['total'];
         }
         $this->_cacheSet($cacheKey, $result);
     }
     return $result;
 }
Exemplo n.º 4
0
 public function testSearch()
 {
     $cmClient = $this->_getCmClient();
     $indexName = 'index1';
     $typeName = 'typeName';
     $cmClient->createIndex($indexName, $typeName, [], [], false);
     $documentId = '2';
     $singleDocumentList = [new CM_Elasticsearch_Document($documentId, ['name' => 'fooboo'])];
     $cmClient->bulkAddDocuments($singleDocumentList, $indexName, $typeName);
     $cmClient->refreshIndex($indexName);
     $query = new CM_Elasticsearch_Query();
     $query->filterTerm('_id', $documentId);
     $response = $cmClient->search([$indexName], [$typeName], ['query' => $query->getQuery()]);
     $this->assertArrayHasKey('hits', $response);
     $this->assertSame(1, $response['hits']['total']);
     $this->assertNotEmpty($response['hits']['hits']);
     $foundDocument = $response['hits']['hits'][0];
     $this->assertSame($documentId, $foundDocument['_id']);
     $this->assertSame($indexName, $foundDocument['_index']);
     $this->assertSame($typeName, $foundDocument['_type']);
     $cmClient->putIndexSettings($indexName, ['blocks.read' => 1]);
     $exception = $this->catchException(function () use($cmClient, $indexName, $typeName, $query) {
         $cmClient->search([$indexName], [$typeName], ['query' => $query->getQuery()]);
     });
     $this->assertInstanceOf('\\Elasticsearch\\Common\\Exceptions\\ElasticsearchException', $exception);
     $this->assertContains('ClusterBlockException', $exception->getMessage());
     $this->assertContains('"status":403', $exception->getMessage());
     $cmClient->deleteIndex($indexName);
 }