FOS Elastica -- getting string representation of query
Asked Answered
H

2

6

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.

Huonghupeh answered 24/2, 2014 at 21:29 Comment(2)
Did you ever manage to get this right? Trying to figure out the same thing at the moment.Gonyea
Nope, sorry. We ended up not using FOS Elastica because it doesn't handle creating (or populating, updating, etc) indices with mappings between more than one entity very well. Good luck!Huonghupeh
W
16

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.

Wraf answered 20/8, 2014 at 14:54 Comment(4)
To make it a bit easier to copy-paste into the ElasticSearch 'head' plugin, do: print json_encode(array('query' => $query->getQuery()))."\n";Whacking
Seems we must use $query->getQuery()->toArray() now as getQuery can return a Query object.Antimatter
Check out: github.com/ruflin/Elastica/commit/…Antimatter
but in this kind of query the types will be missing so it is NOT the exact representation of the executed query:-(Nullification
A
1

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.

Antimatter answered 20/11, 2015 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.