Nested query in querybuilder not working

103 views
Skip to first unread message

karthikeyan r

unread,
Feb 22, 2017, 10:20:33 AM2/22/17
to Elastica - Elasticsearch PHP Client

I am creating a querybuilder to find records containing first names like jon, leo inside a nested group, but i endup getting an exception.

{"query":{"bool":{"must":[{"nested":{"path":"author","query":{"bool":{"must":[{"terms":{"author.firstName":["jon","leo"]}}]}}}}]}}}

Here is the exception

Fatal error: Uncaught exception 'Elastica\Exception\ResponseException' with message 'failed to create query: {
  "bool" : {
    "must" : [
      {
        "nested" : {
          "query" : {
            "bool" : {
              "must" : [
                {
                  "terms" : {
                    "author.firstNa in C:\Workspace\kcp\vendor\ruflin\elastica\lib\Elastica\Transport\Http.php on line 172


Here is my querybuilder query that i wrote

$firstNames = ['Jon', 'Leo']

$queryBuilder = new QueryBuilder(new Version240());

$queryBuilder->query()
        ->bool()
->addMust($queryBuilder->query()->nested()
->setPath('author')
->setQuery(
$queryBuilder->query()->bool()
->addMust($queryBuilder->query()->terms(
'author.firstName',
$firstNames
))
)
);

Even the regular query doesnt seems to work

$query = new Query();
$search = new Search($elasticSearchClient);

$authorTerms= new Terms();
$authorTerms->setTerms('author.firstName', ['jon', 'leo']);
$authorTermsBoolQuery = new BoolQuery();
$authorTermsBoolQuery ->addMust($cityState);


$nestedQuery = new \Elastica\Query\Nested();
$nestedQuery->setPath("author");
$nestedQuery->setQuery($authorTermsBoolQuery );

$boolQuery = new BoolQuery();
$boolQuery->addMust($nestedQuery);
$query->setQuery($boolQuery);

$search->setQuery($query);

//echo $numberOfEntries = $search->count();
print_r(json_encode($query->toArray()));

Am i missing something? 
Appreciate your help and support

Thanks,
Karthik

ruflin

unread,
Mar 2, 2017, 8:26:06 AM3/2/17
to Elastica - Elasticsearch PHP Client
Which version of elasticsearch are you using?
Reply all
Reply to author
Forward
0 new messages