Metadata class types as boolean?

Its is possible to have the default JSON endpoint output booleans in metadata fields - there doesn’t seem to be a class type for boolean

Hi, I don’t really follow do you want to have a metadata class that maps something which can only be true or false and then you want the json end point to present that as a boolean?

Yep that is correct.

Ah I see, may I ask why?

If you super wanted something similar, you could use a groovy hook script to place a Boolean into a customData for each result based on what is in some metadata.

Boolean’s are super useful when integrating the search into other applications.

So the XML push collection has the Boolean values and I can create a custom ftl views and print Boolean from these metadata fields, but the default search.json endpoint is great as everything you need to build a front-end is already in that endpoint,the query object, the facets object, pagination etc

You’re suggesting “Read values from a custom configuration file” from the docs here?

No, @LukeButters is referring to the data model custom data elements.

See this KB article on how to access those:

Excellent thank you.

If anyone needs booleans from metadata…

This code in a post process hook script will simply map any metaData that has a true or false value to the customData object.

transaction?.response?.resultPacket?.results.each() {
// In Groovy, "it" represents the item being iterated  

//Map any metaData that has string values "true" or "false" to customData
def map =  it.metaData
 
map.each { entry ->
    def entryString = "$entry.value"
 
    if ( entryString == "true" || entryString == "false"){
        
        it.customData["$entry.key"] = it.metaData["$entry.key"].toBoolean() 
    }
  }  
}

FYI, you can streamline that a little:

transaction?.response?.resultPacket?.results.each() { result ->
    // Map any metaData that has string values "true" or "false" to customData
    result.metaData.each { key, value ->
        // toBoolean() returns a true if this value (case-insensitve) represents a true boolean, otherwise false
        // The second branch is needed to check for 'real' false boolean values
        if (  ['1', 'true', '0', 'false'].contains(value.toLowerCase()) ){
            result.customData.put(key, value.toBoolean())
        }
    }  
}
1 Like