Esempio n. 1
0
 /**
  * Tests integration of the FiltersAggregation anonymous example from the documentation.
  *
  * @link https://github.com/ongr-io/ElasticsearchDSL/blob/master/docs/Aggregation/Filters.md#anonymous-example
  */
 public function testFiltersAggregationAnonymousExample()
 {
     $errorTermFilter = new TermQuery('body', 'error');
     $warningTermFilter = new TermQuery('body', 'warning');
     $histogramAggregation = new HistogramAggregation('monthly', 'timestamp');
     $histogramAggregation->setInterval('1M');
     $filterAggregation = new FiltersAggregation('grades_stats', ['error' => $errorTermFilter, 'warning' => $warningTermFilter], true);
     $filterAggregation->addAggregation($histogramAggregation);
     $search = new Search();
     $search->addAggregation($filterAggregation);
     $this->assertSame(['aggregations' => ['grades_stats' => ['filters' => ['filters' => [['term' => ['body' => 'error']], ['term' => ['body' => 'warning']]]], 'aggregations' => ['monthly' => ['histogram' => ['field' => 'timestamp', 'interval' => '1M']]]]]], $search->toArray());
 }
Esempio n. 2
0
 private function searchBlog(Criteria $criteria, Struct\ProductContextInterface $context)
 {
     /**@var $condition SearchTermCondition*/
     $condition = $criteria->getCondition('search');
     $query = $this->createMultiMatchQuery($condition);
     $search = new Search();
     $search->addQuery($query);
     $search->setFrom(0)->setSize(5);
     $index = $this->indexFactory->createShopIndex($context->getShop());
     $params = ['index' => $index->getName(), 'type' => 'blog', 'body' => $search->toArray()];
     $raw = $this->client->search($params);
     return $this->createBlogStructs($raw);
 }
Esempio n. 3
0
 /**
  * @param array  $expected
  * @param Search $search
  *
  * @dataProvider getTestToArrayData()
  */
 public function testToArray($expected, $search)
 {
     $this->assertEquals($expected, $search->toArray());
     // Double check
     $this->assertEquals($expected, $search->toArray());
 }
Esempio n. 4
0
 /**
  * This test checks if parameters are correctly set into Search object.
  *
  * @dataProvider getTestSettingParamsData()
  *
  * @param Search    $search
  * @param array     $expected
  */
 public function testSettingParams($search, $expected)
 {
     $this->assertEquals($expected, $search->toArray());
 }
Esempio n. 5
0
 /**
  * Return the DSL query.
  *
  * @return array
  */
 public function toDSL()
 {
     return $this->query->toArray();
 }
 /**
  * Delete by query.
  *
  * @param Search $search
  *
  * @return array
  */
 public function deleteByQuery(Search $search)
 {
     $params = ['index' => $this->getManager()->getIndexName(), 'type' => $this->types, 'body' => $search->toArray()];
     return $this->getManager()->getClient()->deleteByQuery($params);
 }
Esempio n. 7
0
 /**
  * Return the DSL query.
  *
  * @return array
  */
 public function toDSL()
 {
     return $this->query->toArray()['suggest'];
 }
Esempio n. 8
0
 /**
  * Executes given search.
  *
  * @param array  $types
  * @param Search $search
  * @param string $resultsType
  *
  * @return DocumentIterator|RawIterator|array
  */
 public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT)
 {
     foreach ($types as &$type) {
         $type = $this->resolveTypeName($type);
     }
     $results = $this->search($types, $search->toArray(), $search->getQueryParams());
     return $this->parseResult($results, $resultsType, $search->getScroll());
 }
 /**
  * Counts documents by given search.
  *
  * @param Search $search
  * @param array  $params
  * @param bool   $returnRaw If set true returns raw response gotten from client.
  *
  * @return int|array
  */
 public function count(Search $search, array $params = [], $returnRaw = false)
 {
     $body = array_merge(['index' => $this->getManager()->getIndexName(), 'type' => $this->type, 'body' => $search->toArray()], $params);
     $results = $this->getManager()->getClient()->count($body);
     if ($returnRaw) {
         return $results;
     } else {
         return $results['count'];
     }
 }
 protected function executeSearch(Search $search, $type = null, $returnRaw = false)
 {
     $response = $this->client->search(array_filter(['index' => self::INDEX_NAME, 'type' => $type, 'body' => $search->toArray()]));
     if ($returnRaw) {
         return $response;
     }
     $documents = [];
     try {
         foreach ($response['hits']['hits'] as $document) {
             $documents[$document['_id']] = $document['_source'];
         }
     } catch (\Exception $e) {
         return $documents;
     }
     return $documents;
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function hydrate(array $elasticResult, ProductNumberSearchResult $result, Criteria $criteria, ShopContextInterface $context)
 {
     if (!isset($elasticResult['aggregations'])) {
         return;
     }
     if (!isset($elasticResult['aggregations']['agg_properties'])) {
         return;
     }
     $data = $elasticResult['aggregations']['agg_properties']['buckets'];
     $ids = array_column($data, 'key');
     if (empty($ids)) {
         return;
     }
     $groupIds = $this->getGroupIds($ids);
     $search = new Search();
     $search->addFilter(new IdsFilter($groupIds));
     $search->addFilter(new TermFilter('filterable', 1));
     $search->addSort(new FieldSort('name'));
     $index = $this->indexFactory->createShopIndex($context->getShop());
     $data = $this->client->search(['index' => $index->getName(), 'type' => PropertyMapping::TYPE, 'body' => $search->toArray()]);
     $data = $data['hits']['hits'];
     $properties = $this->hydrateProperties($data, $ids);
     $actives = $this->getFilteredValues($criteria);
     $criteriaPart = $this->createCollectionResult($properties, $actives);
     $result->addFacet($criteriaPart);
 }