2013-06-15 14:18:48 +08:00
|
|
|
// Copyright 2013 Belogik. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package goes
|
|
|
|
|
2016-10-14 08:37:40 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2017-03-03 08:58:52 +08:00
|
|
|
"net/url"
|
2016-10-14 08:37:40 +08:00
|
|
|
)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Client represents a connection to elasticsearch
|
|
|
|
type Client struct {
|
2013-06-15 14:18:48 +08:00
|
|
|
// The host to connect to
|
2017-03-03 08:58:52 +08:00
|
|
|
Host *url.URL
|
2014-05-30 05:01:39 +08:00
|
|
|
|
|
|
|
// Client is the http client used to make requests, allowing settings things
|
|
|
|
// such as timeouts etc
|
|
|
|
Client *http.Client
|
2017-02-03 01:54:24 +08:00
|
|
|
|
|
|
|
// Detected version of ES
|
|
|
|
version string
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Response holds an elasticsearch response
|
2013-06-15 14:18:48 +08:00
|
|
|
type Response struct {
|
|
|
|
Acknowledged bool
|
|
|
|
Error string
|
2016-10-14 08:37:40 +08:00
|
|
|
RawError json.RawMessage `json:"error"`
|
2014-10-13 15:11:12 +08:00
|
|
|
Errors bool
|
2013-06-15 14:18:48 +08:00
|
|
|
Status uint64
|
|
|
|
Took uint64
|
|
|
|
TimedOut bool `json:"timed_out"`
|
|
|
|
Shards Shard `json:"_shards"`
|
|
|
|
Hits Hits
|
|
|
|
Index string `json:"_index"`
|
2016-09-28 03:19:07 +08:00
|
|
|
ID string `json:"_id"`
|
2013-06-15 14:18:48 +08:00
|
|
|
Type string `json:"_type"`
|
|
|
|
Version int `json:"_version"`
|
|
|
|
Found bool
|
2015-01-27 09:43:50 +08:00
|
|
|
Count int
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
// Used by the _stats API
|
|
|
|
All All `json:"_all"`
|
|
|
|
|
|
|
|
// Used by the _bulk API
|
|
|
|
Items []map[string]Item `json:"items,omitempty"`
|
|
|
|
|
|
|
|
// Used by the GET API
|
|
|
|
Source map[string]interface{} `json:"_source"`
|
|
|
|
Fields map[string]interface{} `json:"fields"`
|
2013-06-23 14:59:23 +08:00
|
|
|
|
|
|
|
// Used by the _status API
|
|
|
|
Indices map[string]IndexStatus
|
2014-01-10 21:12:33 +08:00
|
|
|
|
|
|
|
// Scroll id for iteration
|
2016-09-28 03:19:07 +08:00
|
|
|
ScrollID string `json:"_scroll_id"`
|
2014-02-27 20:33:51 +08:00
|
|
|
|
|
|
|
Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
|
2014-11-13 16:21:27 +08:00
|
|
|
|
|
|
|
Raw map[string]interface{}
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Aggregation holds the aggregation portion of an ES response
|
2014-02-27 20:33:51 +08:00
|
|
|
type Aggregation map[string]interface{}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Bucket represents a bucket for aggregation
|
2014-02-27 20:33:51 +08:00
|
|
|
type Bucket map[string]interface{}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Document holds a document to send to elasticsearch
|
2013-06-15 14:18:48 +08:00
|
|
|
type Document struct {
|
|
|
|
// XXX : interface as we can support nil values
|
|
|
|
Index interface{}
|
|
|
|
Type string
|
2016-09-28 03:19:07 +08:00
|
|
|
ID interface{}
|
2013-06-15 14:18:48 +08:00
|
|
|
BulkCommand string
|
2014-08-18 04:24:21 +08:00
|
|
|
Fields interface{}
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Item holds an item from the "items" field in a _bulk response
|
2013-06-15 14:18:48 +08:00
|
|
|
type Item struct {
|
|
|
|
Type string `json:"_type"`
|
2016-09-28 03:19:07 +08:00
|
|
|
ID string `json:"_id"`
|
2013-06-15 14:18:48 +08:00
|
|
|
Index string `json:"_index"`
|
|
|
|
Version int `json:"_version"`
|
2014-10-13 15:11:12 +08:00
|
|
|
Error string `json:"error"`
|
|
|
|
Status uint64 `json:"status"`
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// All represents the "_all" field when calling the _stats API
|
2013-06-15 14:18:48 +08:00
|
|
|
// This is minimal but this is what I only need
|
|
|
|
type All struct {
|
|
|
|
Indices map[string]StatIndex `json:"indices"`
|
|
|
|
Primaries map[string]StatPrimary `json:"primaries"`
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
// StatIndex contains stats for a specific index
|
2013-06-15 14:18:48 +08:00
|
|
|
type StatIndex struct {
|
|
|
|
Primaries map[string]StatPrimary `json:"primaries"`
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
// StatPrimary contains stats for a primary index
|
2013-06-15 14:18:48 +08:00
|
|
|
type StatPrimary struct {
|
|
|
|
// primary/docs:
|
|
|
|
Count int
|
|
|
|
Deleted int
|
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Shard holds the "shard" struct as returned by elasticsearch
|
2013-06-15 14:18:48 +08:00
|
|
|
type Shard struct {
|
|
|
|
Total uint64
|
|
|
|
Successful uint64
|
|
|
|
Failed uint64
|
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Hit holds a hit returned by a search
|
2013-06-15 14:18:48 +08:00
|
|
|
type Hit struct {
|
2015-09-28 19:15:18 +08:00
|
|
|
Index string `json:"_index"`
|
|
|
|
Type string `json:"_type"`
|
2016-09-28 03:19:07 +08:00
|
|
|
ID string `json:"_id"`
|
2015-09-28 19:15:18 +08:00
|
|
|
Score float64 `json:"_score"`
|
|
|
|
Source map[string]interface{} `json:"_source"`
|
|
|
|
Highlight map[string]interface{} `json:"highlight"`
|
|
|
|
Fields map[string]interface{} `json:"fields"`
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// Hits holds the hits structure as returned by elasticsearch
|
2013-06-15 14:18:48 +08:00
|
|
|
type Hits struct {
|
|
|
|
Total uint64
|
|
|
|
// max_score may contain the "null" value
|
|
|
|
MaxScore interface{} `json:"max_score"`
|
|
|
|
Hits []Hit
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
// SearchError holds errors returned from an ES search
|
2013-06-15 14:18:48 +08:00
|
|
|
type SearchError struct {
|
|
|
|
Msg string
|
|
|
|
StatusCode uint64
|
|
|
|
}
|
2013-06-23 14:59:23 +08:00
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
// IndexStatus holds the status for a given index for the _status command
|
2013-06-23 14:59:23 +08:00
|
|
|
type IndexStatus struct {
|
|
|
|
// XXX : problem, int will be marshaled to a float64 which seems logical
|
|
|
|
// XXX : is it better to use strings even for int values or to keep
|
|
|
|
// XXX : interfaces and deal with float64 ?
|
|
|
|
Index map[string]interface{}
|
|
|
|
|
|
|
|
Translog map[string]uint64
|
|
|
|
Docs map[string]uint64
|
|
|
|
Merges map[string]interface{}
|
|
|
|
Refresh map[string]interface{}
|
|
|
|
Flush map[string]interface{}
|
|
|
|
|
|
|
|
// TODO: add shards support later, we do not need it for the moment
|
|
|
|
}
|