0

I want to put the half of query in variable $query and put it in $params in ElasticSearch.

This is my $query :

if (Input::has('_id')) {
          $_id = $this->input['_id'];
          $path = 'data';
          $field = 'data.city';

          // Querying city
          if (Input::has('city')) {
              $city = $this->input['city'];
              $query = array('query'=> [
                      'bool' => [
                        'must' => [
                          [
                            'match' => [
                              '_id' => $_id
                            ]
                          ],
                          [
                            'nested' => [
                              'path' => $path,
                              'query' => [
                                'bool' => [
                                  'must' => [
                                    [
                                      'match' => [
                                        $field => $city
                                      ]
                                    ]
                                  ]
                                ]
                              ]
                            ]
                          ]
                        ]
                      ]
                    ]);
          }

And I want to put that variable in $params like this:

$params = [
            'index' => 'channels',
            'type' => 'users',
            'body' => [
                'from' => $defaultPerPage * ($page - 1),
                'size' => $defaultPerPage,
                $query
            ]
      ];

I try to print_r($params) and got the right result. But I always got the error notification when try do query. Anyone knows how to do it ?

Thanks

Error :

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[6fZ0ud3kSTSYsMI-o-mg4w][channels][0]: SearchParseException[[channels][0]: from[0],size[10]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":10,\"0\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"_id\":\"9121200asda121\"}},{\"nested\":{\"path\":\"data\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"data.city\":\"california\"}}]}}}}]}}}}]]]; nested: SearchParseException[[channels][0]: from[0],size[10]: Parse Failure [No parser for element [0]]]; }{[W5E62t0EQ16lckCUnf8atw][channels][1]: RemoteTransportException[[Bima][inet[/10.200.7.69:9300]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[channels][1]: from[0],size[10]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":10,\"0\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"_id\":\"9121200asda121\"}},{\"nested\":{\"path\":\"data\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"data.city\":\"california\"}}]}}}}]}}}}]]]; nested: SearchParseException[[channels][1]: from[0],size[10]: Parse Failure [No parser for element [0]]]; }{[6fZ0ud3kSTSYsMI-o-mg4w][channels][2]: SearchParseException[[channels][2]: from[0],size[10]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":10,\"0\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"_id\":\"9121200asda121\"}},{\"nested\":{\"path\":\"data\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"data.city\":\"california\"}}]}}}}]}}}}]]]; nested: SearchParseException[[channels][2]: from[0],size[10]: Parse Failure [No parser for element [0]]]; }]","status":400}
Dau
  • 8,578
  • 4
  • 23
  • 48
Lazy Guy
  • 101
  • 2
  • 14
  • What error are you getting? – Val Feb 29 '16 at 08:20
  • 'error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[6fZ0ud3kSTSYsMI-o-mg4w][channels][0]: ' – Lazy Guy Feb 29 '16 at 08:23
  • Can you update your question with the full error you're getting, please? – Val Feb 29 '16 at 08:23
  • It seems you need to build the body of the `$params` array like this instead: `'from' => ..., 'size' => ..., 'query' => $query['query']` – Val Feb 29 '16 at 08:32
  • so I cant't keep the query in variable ? I just want to make it customizable – Lazy Guy Feb 29 '16 at 08:37
  • another option is to merge `$query` into `$params['body']`, like this: `$params['body'] = array_merge($params['body'], $query)` – Val Feb 29 '16 at 08:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104835/discussion-between-lazy-guy-and-val). – Lazy Guy Feb 29 '16 at 09:06

0 Answers0