FOS Elastica -- getting string representation of query

elasticsearch, foselasticabundle, symfony-2.3

Solution

I see you no longer are using this but I ended up needing the same thing.

Right before return $query you can use json_encode($query->getQuery()->toArray()) and that should give you what you need as a single line string.

Problem

I am unit-testing a repository that uses FOS Elastica and I was wondering if anyone knows how to get the string version of a query, rather than in array form. Here is my repository method: ``` /** * Creates query object by either first or last name, with given parameters * * @param $name * * @param array $params * * @return Query */ public function findByFirstOrLast($name, array $params) { $queryString = new QueryString(); $queryString->setQuery($name); $queryString->setFields(array('firstName', 'lastName')); $query = new Query(); $query->setQuery($queryString); $query->setSort(array($params['sort'] => array('order' => $params['direction']))); return $query; } ``` Assuming `$name = 'foo';` (and that I am sorting on id), I believe the corresponding FOS Elastica query should be ``` { "query": { "query_string": { "query": "foo", "fields":["firstName","lastName"] } }, "sort": { "id": { "order":"asc" } } } ``` Does anyone know how to get this json-string representation of the query? It doesn't necessarily have to be in this pretty format either, it can be a one-line string.

Original source