Last active
August 22, 2017 23:36
-
-
Save hectorgool/0b51dac4e0b7b377b25269d9c5092fc4 to your computer and use it in GitHub Desktop.
Delete and create an elasticsearch mapping
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
/* | |
twitter@hector_gool | |
*/ | |
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
) | |
const( | |
ELASTICSEARCH_INDEX = "mx" | |
) | |
func main() { | |
delete_index := | |
` | |
curl -u $ELASTICSEARCH_USERNAME:$ELASTICSEARCH_PASSWORD -X DELETE $ELASTICSEARCH_HOSTS:$ELASTICSEARCH_PORT/`+ ELASTICSEARCH_INDEX +` | |
` | |
create_index := | |
` | |
curl -u $ELASTICSEARCH_USERNAME:$ELASTICSEARCH_PASSWORD -X PUT "http://$ELASTICSEARCH_HOSTS:$ELASTICSEARCH_PORT/`+ ELASTICSEARCH_INDEX +`" -d ' | |
{ | |
"settings": { | |
"index": { | |
"analysis": { | |
"analyzer": { | |
"autocomplete": { | |
"tokenizer": "whitespace", | |
"filter": [ | |
"lowercase", | |
"engram" | |
] | |
} | |
}, | |
"filter": { | |
"engram": { | |
"type": "edgeNGram", | |
"min_gram": 1, | |
"max_gram": 10 | |
} | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"postal_code": { | |
"properties": { | |
"cp": { | |
"type": "text", | |
"fields": { | |
"cp": { | |
"type" : "float", | |
"store" : "yes" | |
} | |
} | |
}, | |
"colonia": { | |
"type": "text", | |
"fields": { | |
"colonia": { | |
"type": "string", | |
"index": "not_analyzed", | |
"store": "yes" | |
} | |
} | |
}, | |
"ciudad": { | |
"type": "text", | |
"fields": { | |
"ciudad": { | |
"type": "string", | |
"index": "not_analyzed", | |
"store": "yes" | |
} | |
} | |
}, | |
"delegacion": { | |
"type": "text", | |
"fields": { | |
"delegacion": { | |
"type": "string", | |
"index": "not_analyzed", | |
"store": "yes" | |
} | |
} | |
}, | |
"location": { | |
"type": "geo_point" | |
} | |
} | |
} | |
} | |
} | |
' | |
` | |
/* | |
show_mapping := | |
` | |
curl -u elastic:changeme -X GET $ELASTICSEARCH_HOSTS:$ELASTICSEARCH_PORT/`+ ELASTICSEARCH_INDEX +`/_mapping?pretty | |
` | |
*/ | |
out1, err := exec.Command("sh", "-c", delete_index).Output() | |
printError(err) | |
fmt.Printf("Delete index: %v\n\n", string(out1)) | |
out2, err := exec.Command("sh", "-c", create_index).Output() | |
printError(err) | |
fmt.Printf("Create index: %v\n\n", string(out2)) | |
/* | |
out3, err := exec.Command("sh", "-c", show_mapping).Output() | |
printError(err) | |
fmt.Printf("Show mapping: \n%v\n\n", string(out3)) | |
*/ | |
} | |
func printError(err error) { | |
if err != nil { | |
fmt.Printf("\nError: %v \n ", err.Error()) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment