Here we assume there is a field in our schema like this:
field titles type array<string> {
indexing: attribute | summary
}
Again we process each hit, this time traversing the array and building a string which contains all the titles,
transforming a field looking like this:
"titles":["Bond","James Bond"]
into this output:
"titles":"Bond, James Bond"
Use Case: accessing weighted set attributes
The following example illustrates accessing data held in a weighted set.
Note that the Data Access API doesn't have a "set" or "weighted set" concept;
the weighted set is represented as an unordered array of objects
where each object has an "item" and a "weight" field.
The weight is a long integer value,
while the item type will vary according to the field type as declared in the schema.
Here we assume there is a field in the schema like:
field titles type weightedset<string> {
indexing: attribute | summary
}
Again we process each hit, and format each element of the weighted set, transforming this input:
"titles":{"Bond":15,"James Bond":89}
into this output:
"alternates":"title: Bond[15], title: James Bond[89]"
Unit testing with structured data
For unit testing it is useful to be able to create structured data fields programmatically.
This case be done using Slime:
importcom.yahoo.slime.*;importcom.yahoo.data.access.slime.SlimeAdapter;// Struct example:Slimeslime=newSlime();Cursorstruct=slime.setObject();struct.setString("foo","bar");struct.setDouble("number",1.0);myHit.setField("mystruct",newSlimeAdapter(struct));// Array example:Slimeslime=newSlime();Cursorarray=slime.setArray();array.addString("foo");array.addString("bar");myHit.setField("myarray",newSlimeAdapter(array));// Arrays and objects can be arbitrarily nested// Alternatively, create the slime structure from a JSON string:Slimeslime=SlimeUtils.jsonToSlime(myJsonString.getBytes(StandardCharsets.UTF_8));myHit.setField("myfield",newSlimeAdapter(slime.get()));