ongr-io / ElasticsearchDSL

Query DSL library for Elasticsearch
MIT License
461 stars 200 forks source link

Collapse #314

Open patie opened 4 years ago

patie commented 4 years ago

Can you please add collapse?

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-collapse.html

SomethingWrong commented 4 years ago

Just put it to the body before sending a request.

Here's a rewritten example from readme.md:

<?php
require 'vendor/autoload.php'; //Composer autoload

$client = ClientBuilder::create()->build(); //elasticsearch-php client

$matchAll = new ONGR\ElasticsearchDSL\Query\MatchAllQuery();

$search = new ONGR\ElasticsearchDSL\Search();
$search->addQuery($matchAll);

$params = [
    'index' => 'your_index',
    'body' => array_merge(
        $search->toArray(),
        ['collapse' => ['field' => 'field_to_collapse_on']], // Here your collapse param goes
    ),
];

$results = $client->search($params);
maluramichael commented 3 years ago

Just FYI how i did it

if ($collapseResults) {
    $client             = $index->getClient();
    $params             = [
        'index' => $index->getIndexName(),
        'body'  => array_merge(
            $search->toArray(),
            ['collapse' => ['field' => 'my_field']]
        ),
    ];

    $results  = $client->search($params);
    $iterator = new RawIterator(
        $results,
        $index,
        $index->getConverter(),
        $index->getScrollConfiguration($results, $search->getScroll())
    );

    $elasticSearchResults = iterator_to_array($iterator);
} else {
    $elasticSearchResults = iterator_to_array($index->findRaw($search));
}