PHP search query fields


(Rashed Karim) #1

I have this search query which works fine, but I am unable to translate this into a PHP $params associative array.

POST /index/type/_search
{
   "fields": [
       "url"
       ],
   "query": {
      "query_string": {
         "default_field": "content.content",
         "query": "german"
      }
   },
   "highlight": {
      "fields": {
         "content.content": {}
      }
   }
}

What I have so far of the PHP associative array:

$params = [
			    'index' => $index_name,
			    'type' => 'type_name',
			    'body' => [
			    	'fields' => 'url'      /* error */ 
			        'query' => [
			            'query_string' => [
			            	'default_field' => 'content.content',
			                'query' => '$term'
			            ]
			        ],
			        'highlight' => [
			        	'fields' => ['content.content'=> []]   /*   error  */
			        ]
			    ]
			];

(Marlin Forbes) #2

You've even marked the area where the error is with 'error'.

'fields' => [ 'url' ]

not

'fields' => 'url'

'fields' is plural, which hints at an array.


(Marlin Forbes) #3

You could also copy paste the JSON into php and use json_decode() to get a PHP array.


(Marlin Forbes) #4

Don't use 'query' => '$term'. Just use 'query' => $term.


(system) #5