Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample Elasticsearch responses and parsing in ABAP - code to accompany http://ceronio.net/2015/03/yet-another-abap-json-parser-and-some-other-stuff/
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "4",
"_score" : 1.0,
"_source":{"account_number":4,"balance":27658,"firstname":"Rodriquez","lastname":"Flores","age":31,"gender":"F","address":"986 Wyckoff Avenue","employer":"Tourmania","email":"rodriquezflores@tourmania.com","city":"Eastvale","state":"HI"}
} ]
}
}
* Elasticsearch search response definition
data: begin of hit,
_index type string,
_type type string,
_id type string,
_score type p decimals 2,
_source type ref to json_value, "object,
end of hit.
data: begin of search_response,
took type i,
timed_out type string,
begin of _shards,
total type i,
successful type i,
failed type i,
end of _shards,
begin of hits,
total type i,
max_score type p decimals 2,
hits like table of hit,
end of hits,
end of search_response.
call method jdoc->map_data
exporting
json_value = hit-_source
changing
data = account.
* Shows you how to use the JSON parser/mapper located at https://gist.github.com/mydoghasworms/4888a832e28491c3fe47
try.
* Parse JSON string into an objectified document in memory
jdoc = json_document=>parse( json ).
* Map parsed document to a predefined ABAP structure
call method jdoc->map_root
changing
data = search_response.
* Map a substructure contained in the mapped data to some different data
loop at search_response-hits-hits into hit.
call method jdoc->map_data
exporting
json_value = hit-_source
changing
data = account.
append account to accounts.
endloop.
catch json_error into lx_jerr.
* You're on your own here ...
catch cx_root.
* ... and here
endtry.
@harryparmar
Copy link

harryparmar commented Jun 26, 2016

Hi
My es return looks like this..
{
"took": 31,
"errors": false,
"items": [{
"index": {
"_index": "orders",
"_type": "order_item_manuscript",
"_id": "161001030000000000000000000002",
"_version": 4,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
}]
}

My ABAP structures look like this
DATA: BEGIN OF index ,
_index TYPE STRING,
_type TYPE STRING,
_id TYPE STRING,
_version type i,
begin of _shards,
total type i,
successful type i,
failed type i,
end of _shards,
status TYPE i,
END OF INDEX.

data: begin of es_return,
took type i,
errors type string,
begin of items,
index LIKE TABLE OF index,
end of items,
end of es_return.

I only capture took and errors when converting json to es_return but not items...any ideas what could be wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment