FOS Elastica - getting a string representation of a request

I am testing a unit repository that uses FOS Elastica, and I was wondering if anyone knows how to get a string version of the request, and not in an 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 sort by id), I believe that the corresponding FOS Elastica request 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 a request? It does not have to be in this nice format, it can be a single line string.

+3
source share
2 answers

I see that you are no longer using this, but I still need the same thing.

$query json_encode ($ query- > getQuery() → toArray()), , , .

+11

, . , found.no, elasticsearch, YAML, :

query:
    filtered:
    query:
        multi_match:
            query: php
            operator: AND
            fields:
                - field1^30
                - field2
                - field3                    
                - _all

:

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();
}

, .

+1

All Articles