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.

rene
  • 41,474
  • 78
  • 114
  • 152
Byte Lab
  • 1,576
  • 2
  • 17
  • 42
  • Did you ever manage to get this right? Trying to figure out the same thing at the moment. – iLikeBreakfast Jun 17 '14 at 13:57
  • 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! – Byte Lab Jun 19 '14 at 13:08

2 Answers2

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.

SidewaysGravity
  • 562
  • 5
  • 14
  • 5
    To make it a bit easier to copy-paste into the ElasticSearch 'head' plugin, do: print json_encode(array('query' => $query->getQuery()))."\n"; – Justin Finkelstein Mar 24 '15 at 16:25
  • 2
    Seems we must use $query->getQuery()->toArray() now as getQuery can return a Query object. – COil May 24 '16 at 08:40
  • 1
    Check out: https://github.com/ruflin/Elastica/commit/da852653c484f87e19ca10c8255eb68847bbde2d – COil May 25 '16 at 09:23
  • but in this kind of query the types will be missing so it is NOT the exact representation of the executed query:-( – Gizzmo Jun 10 '16 at 12:59
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.

COil
  • 7,201
  • 2
  • 50
  • 98