Elastica icon indicating copy to clipboard operation
Elastica copied to clipboard

Migrating from elasticsearch client to Elastica

Open louis-celier-nrj opened this issue 3 months ago • 1 comments

Hello,

We are trying to migrate from the official elasticsearch client to elastica.

For a first step, we wanted to use a facade for the search method of the official client but using the Elastica\Search behind the scene.

But we are not sure to know what the proper equivalency for some of our older queries.

For example, we have

        $data = $this->client->search(
            [
                'index' => $index,
                '_source' => '*',
                'sort' => 'updatedAt:desc',
                'size' => 10000,
                'from' => 0,
                'body' => ['query' => ['bool' => ['must' => ['term' => ['statut' => 'published']]]]],
            ]

We understood from the documentation that we should use

$search = new \Elastica\Search($elasticaClient);
$search->addIndexByName("/" . $index);

for the index

or

$query = new \Elastica\Query(['query' => ['bool' => ['must' => ['term' => ['statut' => 'published']]]]]);

for the body

But it is less clear for the other options. We are confused to how Elastica is managing the options that should/could in the url.

Is there a documentation more precise on this kind of details ?

louis-celier-nrj avatar Oct 14 '25 08:10 louis-celier-nrj

The documentation on https://elastica.io/ is not too detailed, but I recommend to take a look at the test suite which has lots of examples: https://github.com/ruflin/Elastica/tree/9.x/tests

There are 2 ways how you can use Elastica. Basically as a pass through for the official client or using all the convenience function and objects. For example to do a bool query, you can find an example here: https://github.com/ruflin/Elastica/blob/9.x/tests/Query/BoolQueryTest.php#L23

Instead of writing all the arrays, you can use the classes. And if you use it in the IDE, for most things you should have auto-complete.

ruflin avatar Oct 22 '25 19:10 ruflin