Guest User

Untitled

a guest
May 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "context"
  5. "log"
  6.  
  7. "github.com/olivere/elastic"
  8. )
  9.  
  10. const (
  11. indexName = "films"
  12. mapping = `
  13. {
  14. "settings":{
  15. "number_of_shards":1,
  16. "number_of_replicas":0
  17. },
  18. "mappings":{
  19. "_doc":{
  20. "properties":{
  21. "title":{
  22. "type":"keyword"
  23. },
  24. "genre":{
  25. "type":"keyword"
  26. },
  27. "year":{
  28. "type":"long"
  29. },
  30. "director":{
  31. "type":"keyword"
  32. }
  33. }
  34. }
  35. }
  36. }
  37. `
  38. )
  39.  
  40. func main() {
  41. log.Print("Connecting...")
  42. opts := []elastic.ClientOptionFunc{
  43. // Uncomment next line to show response from Elasticsearch
  44. //elastic.SetTraceLog(log.New(os.Stdout, "", 0)),
  45. }
  46. client, err := elastic.NewClient(opts...)
  47. if err != nil {
  48. log.Printf("unable to connect: %v", err)
  49. }
  50.  
  51. // Check if index exists
  52. log.Print("Check if index exists...")
  53. ctx := context.Background()
  54. exists, err := client.IndexExists(indexName).Do(ctx)
  55. if err != nil {
  56. log.Fatalf("unable to check if index exists: %v", err)
  57. }
  58. if exists {
  59. log.Print("Removing existing index...")
  60. _, err = client.DeleteIndex(indexName).Do(ctx)
  61. if err != nil {
  62. log.Fatalf("unable to delete index: %v", err)
  63. }
  64. }
  65. // Create index with mapping
  66. log.Print("Create index with mapping...")
  67. _, err = client.CreateIndex(indexName).Body(mapping).Do(ctx)
  68. if err != nil {
  69. log.Fatalf("unable to create index: %v", err)
  70. }
  71. }
Add Comment
Please, Sign In to add comment