Last active
April 23, 2024 21:14
-
-
Save CesarCapillas/a796c0e7cba10ac02213c7f3485d6e90 to your computer and use it in GitHub Desktop.
SOLR bash recipes for creating, deleting or truncating collections, monitoring and searching.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SERVER=${1:-localhost} | |
| PORT=${2:-8983} | |
| NUMSERVERS=${3:-1} | |
| if [ "$PORT" = "443" ]; then | |
| PROTOCOL="https" | |
| else | |
| PROTOCOL="http" | |
| fi | |
| ENDPOINT="$PROTOCOL://${SERVER}:${PORT}/solr/admin/collections?action=clusterstatus&wt=json" | |
| if [[ "$1" == "" ]]; then | |
| echo "Usage:" | |
| echo " check_solr.sh <SOLRSERVER=localhost> [<PORT=8983> <NUMSERVERS=1>]" | |
| exit | |
| fi | |
| CURL=`curl --silent -X GET ${ENDPOINT}` | |
| CHCK=`echo $CURL | grep "live_nodes"` | |
| if [[ "$CHCK" == "" ]]; then | |
| CHECK="Failed" | |
| else | |
| CHECK="OK" | |
| #SOLR_RES=`echo $CURL | jq ".cluster.live_nodes" | tr -d '\r\n'` | |
| SOLR_RES=`echo $CURL | jshon -e cluster | jshon -e live_nodes | tr -d '\r\n'` | |
| # tricky | |
| #SOLR_RES=`echo $CURL | jq ".cluster.live_nodes" | wc -w` | |
| SOLR_NUM=`echo $CURL | jshon -e cluster | jshon -e live_nodes | wc -w` | |
| SOLR_VAR=`expr $SOLR_NUM - 2` | |
| fi | |
| if [[ "$CHECK" == "OK" ]]; then | |
| if (($SOLR_VAR < $NUMSERVERS));then | |
| echo "CRITICAL: SOLR ($SOLR_VAR live nodes) = $SOLR_RES (<$NUMSERVERS)" | |
| exit 2 | |
| fi | |
| echo "INFO: SOLR ($SOLR_VAR live nodes) = $SOLR_RES " | |
| exit 0 | |
| elif [[ "$CHECK" == "Failed" ]]; then | |
| echo "CRITICAL: ${SERVER}" | |
| exit 2 | |
| else | |
| echo "Check failed." | |
| exit 3 | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #./bin/solr create -c zylk -shards 1 -replicationFactor 2 -p 8983 -d server/solr/configsets/nutchconfig-base | |
| SOLRHOME=/opt/solr6/solr-6.6.0 | |
| CONFIGSET=server/solr/configsets/nutchconfig-base | |
| COLLECTION=${1:-zylk} | |
| PORT=${2:-8983} | |
| if [ -z "$1" ]; then | |
| # Usage | |
| echo 'Usage: create-collection.sh <collection-name> [ <port=8080> ]' | |
| else | |
| echo "Creating SOLR collection ${COLLECTION}" | |
| (cd ${SOLRHOME} && ./bin/solr create -c ${COLLECTION} -shards 1 -replicationFactor 2 -p ${PORT} -d ${CONFIGSET}) | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| urlencode() { | |
| # urlencode <string> | |
| old_lc_collate=$LC_COLLATE | |
| LC_COLLATE=C | |
| local length="${#1}" | |
| for (( i = 0; i < length; i++ )); do | |
| local c="${1:i:1}" | |
| case $c in | |
| [a-zA-Z0-9.~_-]) printf "$c" ;; | |
| *) printf '%%%02X' "'$c" ;; | |
| esac | |
| done | |
| LC_COLLATE=$old_lc_collate | |
| } | |
| # Default parameters | |
| # Encode parameters (as blank spaces) | |
| TERM=`urlencode "$1"` | |
| # Default fields | |
| FL=${2:-url,title,lang,score} | |
| FQ=$3 | |
| # Default query term | |
| QT=${4:-select} | |
| # Filter query | |
| COLLECTION=${5:-zylk} | |
| SERVER=${6:-localhost} | |
| PORT=${7:-8983} | |
| if [ -z "$1" ]; then | |
| # Usage | |
| echo 'Usage: do-search.sh <search-terms> [<fl-params=url,h1,title,score> <fq-params=lang:es> <qt-params=select|elevate> <collection-name=> <solr-server=localhost> <port=8383>]' | |
| else | |
| NUMROWS=`curl -s "http://${SERVER}:${PORT}/solr/${COLLECTION}/${QT}?fq=$FQ&indent=on&q=${TERM}&wt=json" | jq '.response.numFound'` | |
| ROWS=10000 | |
| COUNT=0 | |
| START=0 | |
| while [ $START -lt $NUMROWS ]; do | |
| # Only 10 results finally | |
| #curl -s "http://${SERVER}:${PORT}/solr/${COLLECTION}/${QT}?fl=$FL&indent=on&q=$TERM&rows=$ROWS&start=$START&wt=json" | jq '.response.docs[]' | |
| echo "curl -s \"http://${SERVER}:${PORT}/solr/${COLLECTION}/${QT}?fq=$FQ&fl=$FL&indent=on&q=$TERM&rows=10&start=$START&wt=json\"" | |
| curl -s "http://${SERVER}:${PORT}/solr/${COLLECTION}/${QT}?fq=$FQ&fl=$FL&indent=on&q=$TERM&rows=10&start=$START&wt=json" | jq '.response.docs[]' | |
| COUNT=$((COUNT+1)) | |
| START=$((ROWS*COUNT)) | |
| done | |
| echo "Number of results for search: $1 --> $NUMROWS" | |
| fi | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Default parameters | |
| COLLECTION=${1:-zylk} | |
| SERVER=${2:-localhost} | |
| PORT=${3:-8983} | |
| if [ -z "$1" ]; then | |
| # Usage | |
| echo 'Usage: reload-collection.sh <collection-name> [<solr-server=localhost> <port=8983>]' | |
| else | |
| # Reload | |
| curl "http://${SERVER}:${PORT}/solr/admin/collections?action=RELOAD&name=${COLLECTION}" | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #./bin/solr delete -c zylk | |
| SOLRHOME=/opt/solr6/solr-6.6.0 | |
| COLLECTION=${1:-zylk} | |
| if [ -z "$1" ]; then | |
| # Usage | |
| echo 'Usage: remove-collection.sh <collection-name>' | |
| else | |
| echo "Removing SOLR collection ${COLLECTION}" | |
| (cd ${SOLRHOME} && ./bin/solr delete -c ${COLLECTION}) | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Default parameters | |
| COLLECTION=${1:-zylk} | |
| SERVER=${2:-localhost} | |
| PORT=${3:-8983} | |
| if [ -z "$1" ]; then | |
| # Usage | |
| echo 'Usage: truncate-collection.sh <collection-name> [<solr-server=localhost> <port=8983>]' | |
| else | |
| # Truncate | |
| curl -X POST http://${SERVER}:${PORT}/solr/${COLLECTION}/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>' | |
| # Reload | |
| curl "http://${SERVER}:${PORT}/solr/admin/collections?action=RELOAD&name=${COLLECTION}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment