index_exists.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "fmt"
  7. "github.com/olivere/elastic/uritemplates"
  8. )
  9. type IndexExistsService struct {
  10. client *Client
  11. index string
  12. }
  13. func NewIndexExistsService(client *Client) *IndexExistsService {
  14. builder := &IndexExistsService{
  15. client: client,
  16. }
  17. return builder
  18. }
  19. func (b *IndexExistsService) Index(index string) *IndexExistsService {
  20. b.index = index
  21. return b
  22. }
  23. func (b *IndexExistsService) Do() (bool, error) {
  24. // Build url
  25. path, err := uritemplates.Expand("/{index}", map[string]string{
  26. "index": b.index,
  27. })
  28. if err != nil {
  29. return false, err
  30. }
  31. // Get response
  32. res, err := b.client.PerformRequest("HEAD", path, nil, nil)
  33. if err != nil {
  34. return false, err
  35. }
  36. if res.StatusCode == 200 {
  37. return true, nil
  38. } else if res.StatusCode == 404 {
  39. return false, nil
  40. }
  41. return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode)
  42. }