Populating a select field using values from a metadata class
Background
This article outlines how to create advanced search form fields that provide dropdowns containing values sourced from a metadata field in the search index.
Process
Step 1. Extract list of metadata values
Create a shell script that extracts the list of unique metadata values as a post-swap operation.
The list_meta_vals.sh
script below implements this for Linux. Save this file into a folder called @workflow
under the collection’s configuration folder. (e.g. save this to $SEARCH_HOME/conf/<COLLECTION-ID>/@workflow/list_meta_vals.sh
).
#!/bin/bash
#
# list_meta_vals.sh
#
# Takes a collection name and metaedata class as a parameter and writes out a meta_<CLASS>.txt file containing each unique metadata value defined
# in the index for the specified class, sorted alphabetically, into the conf folder of the specified collection.
#
if [ $# -ne 2 ]
then
echo "Error in $0 - Invalid Argument Count"
echo "Syntax: $0 collection_name metadata_class"
exit 1
fi
COLLECTION=$1
METACLASS=$2
# Check SEARCH_HOME is defined
[ "$SEARCH_HOME" ] || { echo -e "\n$0: Error: \$SEARCH_HOME environment variable is not defined, cannot continue.\n" >&2; exit 1; }
echo "Generating metadata listing for field $METACLASS"
CMD="$SEARCH_HOME/bin/padre-di $SEARCH_HOME/data/$COLLECTION/live/idx/index -meta | grep '^$METACLASS:' | sed -e 's/$METACLASS: //g' -e '\$d'| tr '|' '\n' | sort -u > $SEARCH_HOME/conf/$COLLECTION/meta_$METACLASS.txt"
echo $CMD
eval $CMD
exit 0
Ensure the collection workflow runs the script as a post swap operation.
e.g. generate a list of author metadata for mycollection. In the collection.cfg
:
post_swap_command=$SEARCH_HOME/conf/mycollection/@workflow/list_meta_vals.sh mycollection a
Step 2. Add the values to the data model
Add the values to the data model using a hook_post_process.groovy
script.
e.g.
// read in meta value lists to populate drop downs.
def metaA_file = "/opt/funnelback/conf/mycollection/meta_A.txt";
def metaA = new File(metaA_file).readLines();
transaction.response.customData["metaA"] = [""] + metaA;