Multi-layer Paths are Supported in LRestClient

 Published On July 27, 2015

Recently I’ve asked to use Elastic Search directly to improve search performance. Besides the funny compiler bug I’ve spotted, the JSON format of the response data has been significantly changed to something like this:

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"failed": 0
	},
	"hits": {
		"total": 15950,
		"max_score": 1,
		"hits": [
			{
				"_index": "business",
				"_type": "entity",
				"_id": "2001",
				"_score": 1,
				"_source": {
					"id": 2001,
					"name": "Business Name 001",
					...
				}
			}
		]
...

Which means to parse the data, you have to call something like obj["hits"]["hits"][X]["_source"] and loop through X to get the actual data:

{
	"id": 2001,
	"name": "Business Name 001",
	...
}, {
	...
}

Paths and Subpaths are introduced to handle this kind of situations. Simply set them like this:

client.paths = ["hits", "hits"]
client.subpaths = ["_source"]

And the client will look for obj["hits"]["hits"] to get the array, and parse each objects in the array using subpath (in this case only one layer, but multi-layer has been supported as well). For now they are only available in func_array, and func_model only supports the only one-layer path parameter. It would be easy to add it though, and I’ll do it when it’s required in real life.


Tags:

Comments:

comments powered by Disqus