How to add metadata for better searches
Swarm Search is made powerful by the use of metadata to help you organize your data. But how do you write metadata into your objects? Let's look at an example using curl.
Here is a named write operation directly to a Swarm node (not hitting a proxy). The domain is c-csn2.example.com and was already created in the Swarm Admin Console. The bucket is called "bucket1" and does not yet exist so first we need to create the bucket. The node IP address is 192.168.202.84.
Create the bucket:
curl -i --location-trusted -XPOST --data-binary "" "http://192.168.202.84/bucket1"
Example output:
HTTP/1.1 201 CreatedLocation: http://192.168.202.86:80/bucket1?domain=c-csn2.example.comVolume: 711578efa80e74a7977500b9afa5512aLocation: http://192.168.202.92:80/bucket1?domain=c-csn2.example.comVolume: ddf04417655c2a5203f69c0854e9080eEntity-MD5: pxGQhM7wTPBgzjplyV5dJA==Stored-Digest: a7119084cef04cf060ce3a65c95e5d24Last-Modified: Tue, 02 Feb 2016 22:00:42 GMTContent-UUID: 77009875f227a7dba5daa572b89387f4Castor-System-Version: 1454450442.476Etag: "f3f0e2136b282bd91ff1c2829c8b13ba"Castor-System-Alias: 77009875f227a7dba5daa572b89387f4Replica-Count: 2Date: Tue, 02 Feb 2016 22:00:42 GMTServer: CAStor Cluster/8.0.0Content-Length: 46Content-Type: text/htmlKeep-Alive: timeout=14400<html><body>New stream created</body></html>
Now, we need to write the object with a local file called uuids.txt with real, usable metadata:
# curl -i --location-trusted -XPOST --data-binary @uuids.txt -H "CAStor-application: manual" -H "x-band-meta-llica: unforgiven" "http://192.168.202.84/bucket1/uuids.txt"
HTTP/1.1 201 CreatedLocation: http://192.168.202.92:80/bucket1/uuids.txt?domain=c-csn2.example.comVolume: eebf1d49c3018c49e5bd8a9193fae1b0Location: http://192.168.202.84:80/bucket1/uuids.txt?domain=c-csn2.example.comVolume: 04b2b33a65e3c94b6298a1cad06a8f1dEntity-MD5: +sXQs75gA96HGppViMkrpQ==Stored-Digest: fac5d0b3be6003de871a9a5588c92ba5Last-Modified: Tue, 02 Feb 2016 22:10:57 GMTCastor-System-Version: 1454451057.124Etag: "97d226be57d95a31f8f01e10a2df3ae0"Replica-Count: 2Date: Tue, 02 Feb 2016 22:10:57 GMTServer: CAStor Cluster/8.0.0Content-Length: 46Content-Type: text/htmlKeep-Alive: timeout=14400<html><body>New stream created</body></html>
To see the metadata, we can INFO that object:
# curl -iI --location-trusted "http://192.168.202.84/bucket1/uuids.txt"
HTTP/1.1 200 OKCAStor-application: manualCastor-System-CID: 77009875f227a7dba5daa572b89387f4Castor-System-Cluster: c-csn2.example.comCastor-System-Created: Tue, 02 Feb 2016 22:10:57 GMTCastor-System-Name: uuids.txtCastor-System-Version: 1454451057.124Content-Length: 561Content-Type: application/x-www-form-urlencodedLast-Modified: Tue, 02 Feb 2016 22:10:57 GMTx-band-meta-llica: unforgivenEtag: "97d226be57d95a31f8f01e10a2df3ae0"Castor-System-Path: /c-csn2.example.com/bucket1/uuids.txtCastor-System-Domain: c-csn2.example.comVolume: 04b2b33a65e3c94b6298a1cad06a8f1dDate: Tue, 02 Feb 2016 22:11:49 GMTServer: CAStor Cluster/8.0.0Keep-Alive: timeout=14400
Provided that Swarm Search is configured, we can easily search for any objects that were written with that metadata.
Example searching for the "CAStor-application: manual" metadata:
# curl --location-trusted 'http://192.168.202.84/?size=10000&format=json &stype=all&domain=c-csn2.example.com &fields=name,context&sort=context,name&CAStor-application=manual'
[ {"name":"uuids.txt", "context":"c-csn2.example.com/bucket1"}]
Example searching for the "x-band-meta-llica: unforgiven" metadata using regex inside the search. This will match any value, assuming the object has the header x-band-meta-llica:
# curl --location-trusted 'http://192.168.202.84/?size=10000&format=json &stype=all&domain=c-csn2.example.com &fields=name,context&sort=context,name&x-band-meta-llica=*'
[ {"name":"uuids.txt", "context":"c-csn2.example.com/bucket1"}]
Now, assume that we wrote another object with the header x-band-meta-llica but a different value, we can show that our search still matches. This way we can make distinct values in a particular header but still match on any objects given that header.
Let us change the metadata of the first header so that it doesn't match (and change the filename to uuids2.txt), just as an example:
#curl -i --location-trusted -XPOST --data-binary @uuids.txt -H "CAStor-application: curl" -H "x-band-meta-llica: sad but true" "http://192.168.202.84/bucket1/uuids2.txt"HTTP/1.1 201 CreatedLocation: http://192.168.202.91:80/bucket1/uuids.txt?domain=c-csn2.example.comVolume: 4186c53996d5f1511d4c8dfe969dd5a3Location: http://192.168.202.84:80/bucket1/uuids.txt?domain=c-csn2.example.comVolume: 04b2b33a65e3c94b6298a1cad06a8f1dEntity-MD5: Vw2Sokb1t1c+VHxhP/3AFQ==Stored-Digest: 570d92a246f5b7573e547c613ffdc015Last-Modified: Tue, 02 Feb 2016 22:24:26 GMTCastor-System-Version: 1454451866.709Etag: "c8f0d1ee9779735b42acfe23947b1c13"Replica-Count: 2Date: Tue, 02 Feb 2016 22:24:26 GMTServer: CAStor Cluster/8.0.0Content-Length: 46Content-Type: text/htmlKeep-Alive: timeout=14400<html><body>New stream created</body></html>
Now, here are the same searches again:
# curl --location-trusted 'http://192.168.202.84/?size=10000&format=json &stype=all&domain=c-csn2.example.com &fields=name,context&sort=context,name&CAStor-application=manual'
[ {"name":"uuids.txt", "context":"c-csn2.example.com/bucket1"}]
This only matches one object because only the first object we wrote has that specific header value.
This search matches both objects because we used a wildcard in the value and they have the same header:
# curl --location-trusted 'http://192.168.202.84/?size=10000&format=json &stype=all&domain=c-csn2.example.com &fields=name,context&sort=context,name&x-band-meta-llica=*'
[ {"name":"uuids.txt", "context":"c-csn2.example.com/bucket1"}, {"name":"uuids2.txt", "context":"c-csn2.example.com/bucket1"}]