diff --git a/elastic/scripts/es_boot.sh b/elastic/scripts/es_boot.sh new file mode 100755 index 0000000..5e5f2eb --- /dev/null +++ b/elastic/scripts/es_boot.sh @@ -0,0 +1,208 @@ +#!/usr/bin/env bash + +__isint='^[0-9]+$' +__isvalidstr='^[a-z0-9]+$' +__isvalidaddr='^[a-z]+$|^((25[0-5]|2[0-4]?[0-9]|1[0-9]?[0-9]|[3-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4]?[0-9]|1[0-9]?[0-9]|[3-9][0-9]|[0-9])$' + +create_template() { + curl -H "Content-Type: application/json" -X PUT "$1:$2/_index_template/papers_t" -d ' + { + "index_patterns": ["papers*"], + "priority": 1, + "template": { + "aliases": { + "papers": {} + }, + "mappings": { + "properties": { + "id": { + "type": "keyword" + }, + "title": { + "type": "text", + "analyzer": "title_analyzer" + }, + "authors": { + "type": "text" + }, + "topic": { + "type": "text" + }, + "summary": { + "type": "text" + }, + "tags": { + "type": "keyword" + }, + "content": { + "type": "text" + }, + "publisher": { + "type": "text" + } + } + }, + "settings": { + "analysis": { + "analyzer": { + "title_analyzer": { + "type": "custom", + "tokenizer": "title_engram_tokenizer" + }, + "content_analyzer_i": { + "type": "custom", + "tokenizer": "content_onchar_tokenizer", + "char_filter": [ + "markdown_token_filter" + ] + }, + "content_analyzer_s": { + "type": "custom", + "tokenizer": "content_onchar_tokenizer", + "char_filter": [ + "markdown_token_filter" + ], + "filter": [ + ] + }, + "basic_analyzer": { + + } + }, + "tokenizer": { + "title_engram_tokenizer": { + "type": "edge_ngram", + "min_gram": 2, + "max_gram": 10, + "token_chars": [ + "letter", + "digit" + ] + }, + "content_onchar_tokenizer": { + "type": "char_group", + "tokenize_on_chars": [ + "whitespace", + ".", ",", "(", ")", "-", "[", "]", "{", + "}", "#", ":", ";", "`", "!", "*" + ] + } + }, + "char_filter": { + "markdown_token_filter": { + "type": "pattern_replace", + "pattern": "[[a-z][0-9]]*://[[a-z][0-9]]*.[a-z]*", + "replacement": "" + } + }, + "filter": { + } + } + } + } + } + ' +} + +#============================================================================================================================================================================= + +create_index() { + curl -X PUT "$1:$2/papers-$3?pretty" +} + +#============================================================================================================================================================================= + +__usage=" + Usage: $(basename $0) + --------------------------------------------------------------------------- + | -c, --create-only | Skip template initialization and only create | + | | specified index. Result index name will be | + | | 'papers-{specified name}' | + | | + | -h | Help information | + | | + | -a | Specifies the address | + | | + | -p, --port | Specifies the port | + | | + | -i, --index-name | Specifies the index name: | + | | Must be lowercase, cannot include [\/*?\"<>| ,#:], | + | | cannot start with [.-_+], cannot be \".\" or \"..\" | + | | cannot be longer than 255 bytes (note: multi-byte | + | | characters will count towards the limit faster) | + | | Result index name will be 'papers-{specified name}' | + --------------------------------------------------------------------------- +" +#============================================================================================================================================================================= + +CTRL=0 + +if [[ "$1" == "-h" ]]; then + echo "$__usage" +else + while [[ $# -gt 0 ]]; do + case "$1" in + -p | --port) + if [[ -n "$2" && $2 =~ $__isint && "$2" -ge 1 && "$2" -le 65535 ]]; then + PORT="$2" + shift + else + echo "Invalid port number!" + fi + ;; + -a | --address) + if [[ -n "$2" ]]; then + IP="$2" + shift + else + echo "Address is not specified!" + fi + ;; + -i | --index-name) + if [[ -n "$2" && $2 =~ $__isvalidstr ]]; then + IND="$2" + shift + else + echo "Index name is not specified!" + fi + ;; + -c | --create-only) + CTRL=2 + ;; + -*) + echo "Option '$1' is not supported" + exit + ;; + *) + if [[ $1 =~ $__isvalidaddr ]]; then + IP="$1" + elif [[ $1 =~ $__isint && "$1" -ge 1 && "$1" -le 65535 ]]; then + PORT="$1" + elif [[ $1 =~ $__isvalidstr ]]; then + IND="$1" + else + echo "Invalid argument!" + exit; + fi + ;; + esac + shift + done + + echo "Specified: $IP:$PORT | Index name: $IND" + + case $CTRL in + 0) # Default behaviour - full initialization (template creation and index creation) + create_template "$IP" "$PORT" + echo "Elasticsearch index template created" + create_index "$IP" "$PORT" "$IND" + echo "Elasticsearch index (papers-$IND) created" + exit + ;; + 2) # Create index, skip creating the template + create_index "$IP" "$PORT" "$IND" + echo "Elasticsearch index (papers-$IND) created" + exit + ;; + esac +fi \ No newline at end of file