Skip to content

Commit 5383211

Browse files
authored
Merge pull request #225 from geonetwork/api-loopoverresults
API / Example to loop over search results
2 parents 92fe7cc + 039d328 commit 5383211

File tree

1 file changed

+91
-5
lines changed

1 file changed

+91
-5
lines changed

source/api/the-geonetwork-api.rst

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ This is an example to trigger an XSL process on a set of records. It illustrates
1717

1818
.. code-block:: shell
1919
20-
export CATALOG=http://localhost:8080/geonetwork
21-
export CATALOGUSER=admin
22-
export CATALOGPASS=admin
23-
export PROCESS=migrate-201904
20+
CATALOG=http://localhost:8080/geonetwork
21+
CATALOGUSER=admin
22+
CATALOGPASS=admin
23+
PROCESS=migrate-201904
2424
2525
rm -f /tmp/cookie;
2626
curl -s -c /tmp/cookie -o /dev/null \
2727
-X GET \
2828
-H "Accept: application/json" \
2929
"$CATALOG/srv/api/me";
30-
export TOKEN=`grep XSRF-TOKEN /tmp/cookie | cut -f 7`;
30+
TOKEN=`grep XSRF-TOKEN /tmp/cookie | cut -f 7`;
3131
curl \
3232
-X GET \
3333
-H "Accept: application/json" \
@@ -56,6 +56,92 @@ This is an example to trigger an XSL process on a set of records. It illustrates
5656
5757
5858
59+
60+
61+
Loop on search results and apply changes (processing and batch editing)
62+
=======================================================================
63+
64+
This is an example to highlight how to loop over specific search results (here only series) and apply various changes:
65+
66+
67+
.. code-block:: shell
68+
69+
SERVER=http://localhost:8080/geonetwork
70+
CATALOGUSER=admin
71+
CATALOGPASS=admin
72+
73+
type=series
74+
from=0
75+
size=1000
76+
77+
rm results.json
78+
rm -f /tmp/cookie;
79+
80+
curl -s -c /tmp/cookie -o /dev/null \
81+
-X GET \
82+
--user $CATALOGUSER:$CATALOGPASS \
83+
-H "Accept: application/json" \
84+
"$SERVER/srv/api/me";
85+
86+
TOKEN=`grep XSRF-TOKEN /tmp/cookie | cut -f 7`;
87+
JSESSIONID=`grep JSESSIONID /tmp/cookie | cut -f 7`;
88+
89+
curl "$SERVER/srv/api/search/records/_search" \
90+
-X 'POST' \
91+
-H 'Accept: application/json, text/plain, */*' \
92+
-H 'Content-Type: application/json;charset=UTF-8' \
93+
--data-raw "{\"query\":{\"query_string\":{\"query\": \"+isHarvested:false +resourceType: $type\"}},\"from\":$from, \"size\":$size, \"_source\": {\"include\": [\"resourceTitleObject.default\"]}, \"sort\": [{\"resourceTitleObject.default.keyword\": \"asc\"}]}" \
94+
-H "X-XSRF-TOKEN: $TOKEN" -H "Cookie: XSRF-TOKEN=$TOKEN; JSESSIONID=$JSESSIONID" \
95+
--compressed \
96+
-o results.json
97+
98+
for hit in $(jq -r '.hits.hits[] | @base64' results.json); do
99+
_jq() {
100+
echo "${hit}" | base64 --decode | jq -r "${1}"
101+
}
102+
103+
title=$(_jq '._source.resourceTitleObject.default')
104+
uuid=$(_jq '._id')
105+
echo "__________"
106+
echo "### $uuid"
107+
108+
# Update series from its members using XSL process
109+
curl $AUTH "$SERVER/srv/api/records/$uuid/processes/collection-updater" \
110+
-X 'POST' \
111+
-H 'Accept: application/json, text/plain, */*' \
112+
-H "X-XSRF-TOKEN: $TOKEN" \
113+
-H "Cookie: XSRF-TOKEN=$TOKEN; JSESSIONID=$JSESSIONID" \
114+
--compressed
115+
116+
curl $AUTH "$SERVER/srv/api/selections/s101" \
117+
-X 'DELETE' \
118+
-H 'Accept: application/json, text/javascript, */*; q=0.01' \
119+
-H "X-XSRF-TOKEN: $TOKEN" \
120+
-H "Cookie: XSRF-TOKEN=$TOKEN; JSESSIONID=$JSESSIONID" \
121+
--compressed
122+
123+
curl $AUTH "$SERVER/srv/api/selections/s101?uuid=$uuid" \
124+
-X 'PUT' \
125+
-H 'Accept: application/json, text/javascript, */*; q=0.01' \
126+
-H "X-XSRF-TOKEN: $TOKEN" \
127+
-H "Cookie: XSRF-TOKEN=$TOKEN; JSESSIONID=$JSESSIONID" \
128+
--compressed
129+
130+
# Keep only the first 2 resource identifiers using batch editing
131+
curl $AUTH "$SERVER/srv/api/records/batchediting?bucket=s101" \
132+
-X 'PUT' \
133+
-H 'Accept: application/json, text/plain, */*' \
134+
-H 'Content-Type: application/json;charset=UTF-8' \
135+
-H "X-XSRF-TOKEN: $TOKEN" \
136+
-H "Cookie: XSRF-TOKEN=$TOKEN; JSESSIONID=$JSESSIONID" \
137+
--data-raw "[{\"xpath\":\"/gmd:identificationInfo/*/gmd:citation/*/gmd:identifier[position() > 2]\",\"value\":\"<gn_delete/>\"}]" \
138+
--compressed
139+
done;
140+
141+
142+
143+
144+
59145
Using the search API in Google sheet
60146
====================================
61147

0 commit comments

Comments
 (0)