Storing custom data within the data model
Background
This article describes how to add custom data to the data model using a hook script. Once the custom values are in the data model they can be accessed from a Freemarker template.
Custom data elements in the data model
If you need to store variables within the data model there are several customData maps that exist for this purpose:
-
transaction.question.customData[]
: Custom data to attach to the search question. Values can be added in a pre-process hook script. -
transaction.response.customData[]
: Custom data to attach to the search response. Values can be added in a post-process hook script. -
transaction.response.results[n].customData[]
: Result-specific custom data. Per result values can be added in a post-process hook script. -
transaction.customData[]
: Global custom data. Note: this is not currently accessible from Freemarker so don’t use this element.
Adding custom data
The following example shows how to set a few different types of variables in a post process hook script and write these to the response.customData[]
list in the data model:
hook_post_process.groovy
// Define some variables of different types
def number = 3
def string = "This is a string value"
def map=[ident1:"val1", ident2:"val2"]
def list=["item","another item"]
// Add these to custom data in the data model
transaction.response.customData["number"] = number
transaction.response.customData["string"] = string
transaction.response.customData["list"] = list
transaction.response.customData["map"] = map
This results in a data model structure like the following (note: some of the data model elements have been collapsed for clarity in the example:
search.json
{
question: { ... },
extraSearchesTerminated: false,
response: {
resultPacket: { ... },
returnCode: 0,
untruncatedPadreOutputSize: 11906,
facets: [ ... ],
customData: {
number: 3,
string: "This is a string value",
list: [
"item",
"another item"
],
map: {
ident1: "val1",
ident2: "val2"
}
},
optimiserModel: null,
curator: { ... },
performanceMetrics: { ... }
},
session: { ... },
QueryString: "collection=mycollection&query=!showall",
error: null
}