Skip to content
Advertisement

ElasticSearch query is malformed. Elastic-PHP Client

I am facing an issue which is causing the query to malfunctioned. My query is as follows

 $params = [
                              'index'=>'us_data_master',
                              'body'  => [
                                  'query' => [
                                    'bool'=>[
                                        'must'=>[
                                            "terms" => [
                                                          "city_code" => [
                                                              "Los Angeles",
                                                              "Aguara Hills",
                                                          ]
                                                      ],
                                        [
                                          "term"=>['state_code'=>'CA']
                                        ],
                                      'multi_match'=>[
                                          'query'=>'*app*',
                                          'fields'=>['name','contact_no','zip_code','city_code']
                                        ]
                                      ],
                                    "must_not"=>[
                                      "term"=>["contact_no"=>"0"]
                                    ]
                                  ]
                                  ]
                              ]
                    ];  

It is returning the error as

"error":{"root_cause":[{"type":"parsing_exception","reason":"[terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":65}],"type":"parsing_exception","reason":"[terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":65},"status":400} 

I have also installed Kibana the query there works perfectly. I am trying to replicate it to PHP Client but it gives me error. The working query in Kibana is as follows

  {
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "city_code.keyword": [
              "Los Angeles",
              "Aguara Hills"
            ]
          }
        },
        {
          "term": {
            "state_code.keyword": "CA"
          }
        },
        {
          "multi_match": {
            "query": "*appliance*",
            "fields": ["name","city_code","address","contact_no"]
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "contact_no.keyword": "0"
          }
        }
      ]
    }
  }
}

Advertisement

Answer

You seem to have missed a bracket after the Terms Query and there were other formatting issues too, but I believe the below query should help you:

$params = [
            'index'=>'us_data_master',
            'body'  => [
                'query' => [
                    'bool'=>[
                        'must'=>[
                            [
                                'terms' => [
                                     'city_code.keyword' => [ 'Los Angeles', 'Aguara Hills' ]
                                 ]
                            ],
                            [
                                'term'=>['state_code.keyword'=>'CA']
                            ],
                            [
                                'multi_match'=>[
                                        'query'=>'*app*',
                                        'fields'=>['name','contact_no','zip_code','city_code']
                                 ]
                            ]
                         ],
                         'must_not'=>[
                            [
                                   'term'=>['contact_no.keyword'=>'0']
                            ]
                         ]
                     ]
                 ]
            ]
       ];  

Hope this helps!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement