FOS Elastica -- getting string representation of query
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.
Not a direct answer to the question but very related. When using a tool like found.no to test your elasticsearch queries, it can be interesting to have the output as YAML so you can paste in the found.no editor like this:
query: filtered: query: multi_match: query: php operator: AND fields: - field1^30 - field2 - field3 - _all
You can have this kind of output with the following function:
use Elastica\Query;use Symfony\Component\Yaml\Dumper;/** * @param Query $query * @param bool $asYaml */protected function debugQuery(Query $query, $asYaml = false){ echo '<pre>'; $debug = ['query' => $query->getQuery()->toArray()]; if (false === $asYaml) { echo json_encode($debug, JSON_PRETTY_PRINT); die(); } $dumper = new Dumper(); $yaml = $dumper->dump($debug, 100); echo $yaml; die();}
So you can choose either format.