Beispiel #1
0
// The index API adds or updates a typed JSON document in a specific index, making it searchable.
// http://www.elasticsearch.org/guide/reference/api/index_.html
func Index(pretty bool, index string, _type string, id string, data interface{}) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s?%s", index, _type, id, api.Pretty(pretty))
	var method string
	if id == "" {
		method = "POST"
	} else {
		method = "PUT"
	}

	body, err := api.DoCommand(method, url, data)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	//fmt.Println(body)
	return retval, err
}
Beispiel #2
0
// IndexWithParameters takes all the potential parameters available
func IndexWithParameters(index string, _type string, id string, parentId string, version int, op_type string,
	routing string, timestamp string, ttl int, percolate string, timeout string, refresh bool,
	args map[string]interface{}, data interface{}) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url, err := GetIndexUrl(index, _type, id, parentId, version, op_type, routing, timestamp, ttl, percolate, timeout, refresh)
	if err != nil {
		return retval, err
	}
	var method string
	if len(id) == 0 {
		method = "POST"
	} else {
		method = "PUT"
	}
	if VerboseLogging {
		log.Printf("about to :%v %v %s", url, args, data)
	}
	body, err := api.DoCommand(method, url, args, data)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #3
0
// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
// The response includes a docs array with all the fetched documents, each element similar in structure to a document
// provided by the get API.
// see http://www.elasticsearch.org/guide/reference/api/multi-get.html
func MGet(pretty bool, index string, _type string, req string) (MGetResponseContainer, error) {
	var url string
	var retval MGetResponseContainer
	if len(index) <= 0 {
		url = fmt.Sprintf("/_mget?%s", api.Pretty(pretty))
	}
	if len(_type) > 0 && len(index) > 0 {
		url = fmt.Sprintf("/%s/%s/_mget?%s", index, _type, api.Pretty(pretty))
	} else if len(index) > 0 {
		url = fmt.Sprintf("/%s/_mget?%s", index, api.Pretty(pretty))
	}
	body, err := api.DoCommand("GET", url, req)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	fmt.Println(body)
	return retval, err
}
Beispiel #4
0
//str := `{"person" : {"properties" : {"file" : {"type" : "attachment","fields" : {"title" : { "store" : "yes" },"file" : { "term_vector":"with_positions_offsets", "store":"yes" }}}}}}`
func EsMap(applogo string, indexmate IndexMate) int {
	api.Domain = Esapi
	table := strings.ToLower(indexmate.IndexTable)
	url := strings.ToLower("/" + applogo + "/" + table + "/_mapping")
	fmt.Println(url)
	fileslist := strings.Split(strings.Replace(strings.ToLower(indexmate.IndexAttribute), " ", "", -1), ",")
	var files string
	for _, v := range fileslist {
		files = files + `"` + v + `" : { "store" : "yes" },`
	}
	str := `{
  "` + table + `" : {
    "properties" : {
      "file" : {
        "type" : "attachment",
        "fields" : {
          ` + files + `
           "file" : { "term_vector":"with_positions_offsets", "store":"yes" }
        }
      }
    }
  }
}`
	fmt.Println(str)
	_, err := api.DoCommand("POST", url, str)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}
	return 0
}
Beispiel #5
0
// Index adds or updates a typed JSON document in a specific index, making it searchable, creating an index
// if it did not exist.
// if id is omited, op_type 'create' will be pased and http method will default to "POST"
// id is optional
// http://www.elasticsearch.org/guide/reference/api/index_.html
func Index(index string, _type string, id string, args map[string]interface{}, data interface{}) (api.BaseResponse, error) {
	var retval api.BaseResponse
	var url string

	if len(id) > 0 {
		url = fmt.Sprintf("/%s/%s/%s", index, _type, id)
	} else {
		url = fmt.Sprintf("/%s/%s", index, _type)
	}

	var method string
	if len(id) == 0 {
		method = "POST"
	} else {
		method = "PUT"
	}

	body, err := api.DoCommand(method, url, args, data)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #6
0
// ClearCache allows to clear either all caches or specific cached associated with one ore more indices.
// see http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/
func ClearCache(clearId bool, clearBloom bool, indices ...string) (api.ExtendedStatus, error) {
	var retval api.ExtendedStatus
	var clearCacheUrl string
	if len(indices) > 0 {
		clearCacheUrl = fmt.Sprintf("/%s/_cache/clear", strings.Join(indices, ","))

	} else {
		clearCacheUrl = fmt.Sprintf("/_cache/clear")
	}
	var values url.Values = url.Values{}

	if clearId {
		values.Add("id", "true")
	}
	if clearBloom {
		values.Add("bloom", "true")
	}
	clearCacheUrl += "?" + values.Encode()
	body, err := api.DoCommand("POST", clearCacheUrl, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #7
0
// GetSource retrieves the document by id and converts it to provided interface
func GetSource(index string, _type string, id string, args map[string]interface{}, source interface{}) error {
	url := fmt.Sprintf("/%s/%s/%s/_source", index, _type, id)
	body, err := api.DoCommand("GET", url, args, nil)
	if err == nil {
		err = json.Unmarshal(body, &source)
	}
	return err
}
Beispiel #8
0
// This does the actual send of a buffer, which has already been formatted
// into bytes of ES formatted bulk data
func BulkSend(buf *bytes.Buffer) error {
	_, err := api.DoCommand("POST", "/_bulk", nil, buf)
	if err != nil {
		BulkErrorCt += 1
		return err
	}
	return nil
}
Beispiel #9
0
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html
func State(settingType string, key string, value int) error {
	url := "/_cluster/settings"
	m := map[string]map[string]int{settingType: map[string]int{key: value}}
	_, err := api.DoCommand("PUT", url, m)
	if err != nil {
		return err
	}
	return nil
}
Beispiel #10
0
func EsIndex(applogo string, number_of_shards, number_of_replicas string) int {
	api.Domain = Esapi
	url := "/" + applogo
	str := `{"settings" : { "index" : { "number_of_shards" :` + number_of_shards + ` , "number_of_replicas" : ` + number_of_replicas + ` }}}`
	_, err := api.DoCommand("POST", url, str)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}
	return 0
}
Beispiel #11
0
// NodesShutdown allows the caller to shutdown between one and all nodes in the cluster
// delay is a integer representing number of seconds
// passing "" or "_all" for the nodes parameter will shut down all nodes
// see http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown/
func NodesShutdown(delay int, nodes ...string) error {
	shutdownUrl := fmt.Sprintf("/_cluster/nodes/%s/_shutdown", strings.Join(nodes, ","))
	if delay > 0 {
		var values url.Values = url.Values{}
		values.Add("delay", strconv.Itoa(delay))
		shutdownUrl += "?" + values.Encode()
	}
	_, err := api.DoCommand("POST", shutdownUrl, nil)
	if err != nil {
		return err
	}
	return nil
}
// doSyncMapping synchronizes the configuration definition with the Elastic
// Search backend mappings.
func doSyncMapping(c *cli.Context) {
	config := ParseConfigOrDie(c.GlobalString("config"))

	notAnalyzedProtos := []mappingProto{}
	for _, notAnalyzedPattern := range config.NotAnalyzedPatterns {
		notAnalyzedProtos = append(notAnalyzedProtos, notAnalyzedStringProto(notAnalyzedPattern))
	}

	for _, r := range config.Repositories {
		template := makeTemplate(r.IndexPrefix(), notAnalyzedProtos)
		if _, err := api.DoCommand("PUT", "/_template/vossibility-"+r.GivenName, nil, template); err != nil {
			log.Fatal(err)
		}
	}
}
// IndicesExists checks for the existance of indices. uses http 404 if it does not exist, and 200 if it does
// see http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists/
func IndicesExists(indices ...string) (bool, error) {
	var url string
	if len(indices) > 0 {
		url = fmt.Sprintf("/%s", strings.Join(indices, ","))
	}
	_, err := api.DoCommand("HEAD", url, nil, nil)
	if err != nil {
		eserror := err.(api.ESError)
		if eserror.Code == 404 {
			return false, err
		} else {
			return eserror.Code == 200, err
		}
	}
	return true, nil
}
Beispiel #14
0
// State gets the comprehensive state information for the whole cluster
// see http://www.elasticsearch.org/guide/reference/api/admin-cluster-state/
func State(filter_nodes bool, filter_routing_table bool, filter_metadata bool, filter_blocks bool,
	filter_indices ...string) (ClusterStateResponse, error) {
	url := getStateUrl(false, false, false, false, filter_indices...)
	var retval ClusterStateResponse
	body, err := api.DoCommand("GET", url, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #15
0
// Count allows the caller to easily execute a query and get the number of matches for that query.
// It can be executed across one or more indices and across one or more types.
// The query can either be provided using a simple query string as a parameter,
// or using the Query DSL defined within the request body.
// http://www.elasticsearch.org/guide/reference/api/count.html
func Count(index string, _type string, args map[string]interface{}) (CountResponse, error) {
	var url string
	var retval CountResponse
	url = fmt.Sprintf("/%s/%s/_count", index, _type)
	body, err := api.DoCommand("GET", url, args, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
// MoreLikeThis allows the caller to get documents that are “like” a specified document.
// http://www.elasticsearch.org/guide/reference/api/more-like-this.html
func MoreLikeThis(index string, _type string, id string, args map[string]interface{}, query MoreLikeThisQuery) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s/_mlt", index, _type, id)
	body, err := api.DoCommand("GET", url, args, query)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #17
0
// Update updates a document based on a script provided. The operation gets the document
// (collocated with the shard) from the index, runs the script (with optional script language and parameters),
// and index back the result (also allows to delete, or ignore the operation). It uses versioning to make sure
// no updates have happened during the “get” and “reindex”. (available from 0.19 onwards).
// Note, this operation still means full reindex of the document, it just removes some network roundtrips
// and reduces chances of version conflicts between the get and the index. The _source field need to be enabled
// for this feature to work.
//
// http://www.elasticsearch.org/guide/reference/api/update.html
// TODO: finish this, it's fairly complex
func Update(pretty bool, index string, _type string, id string, data interface{}) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s/_update?%s", index, _type, id, api.Pretty(pretty))
	body, err := api.DoCommand("POST", url, data)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
func Percolate(index string, _type string, name string, args map[string]interface{}, doc string) (api.Match, error) {
	var url string
	var retval api.Match
	url = fmt.Sprintf("/%s/%s/_percolate", index, _type)
	body, err := api.DoCommand("GET", url, args, doc)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #19
0
// Delete API allows to delete a typed JSON document from a specific index based on its id.
// http://www.elasticsearch.org/guide/reference/api/delete.html
func Delete(index string, _type string, id string, args map[string]interface{}) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s", index, _type, id)
	body, err := api.DoCommand("DELETE", url, args, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
// RegisterPercolate allows the caller to register queries against an index, and then send percolate requests which include a doc, and
// getting back the queries that match on that doc out of the set of registered queries.
// Think of it as the reverse operation of indexing and then searching. Instead of sending docs, indexing them,
// and then running queries. One sends queries, registers them, and then sends docs and finds out which queries
// match that doc.
// see http://www.elasticsearch.org/guide/reference/api/percolate.html
func RegisterPercolate(index string, name string, args map[string]interface{}, query api.Query) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/_percolator/%s/%s", index, name)
	body, err := api.DoCommand("PUT", url, args, query)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #21
0
// The more like this (mlt) API allows to get documents that are “like” a specified document.
// http://www.elasticsearch.org/guide/reference/api/more-like-this.html
func MoreLikeThis(pretty bool, index string, _type string, id string, query MoreLikeThisQuery) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s/_mlt?%s", index, _type, id, api.Pretty(pretty))
	body, err := api.DoCommand("GET", url, query)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	fmt.Println(body)
	return retval, err
}
Beispiel #22
0
func Percolate(pretty bool, index string, _type string, name string, doc string) (api.Match, error) {
	var url string
	var retval api.Match
	url = fmt.Sprintf("/%s/%s/_percolate?%s", index, _type, api.Pretty(pretty))
	body, err := api.DoCommand("GET", url, doc)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	fmt.Println(body)
	return retval, err
}
Beispiel #23
0
// RegisterPercolate allows the caller to register queries against an index, and then send percolate requests which include a doc, and
// getting back the queries that match on that doc out of the set of registered queries.
// Think of it as the reverse operation of indexing and then searching. Instead of sending docs, indexing them,
// and then running queries. One sends queries, registers them, and then sends docs and finds out which queries
// match that doc.
// see http://www.elasticsearch.org/guide/reference/api/percolate.html
func RegisterPercolate(pretty bool, index string, name string, query api.Query) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/_percolator/%s/%s?%s", index, name, api.Pretty(pretty))
	body, err := api.DoCommand("PUT", url, query)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	fmt.Println(body)
	return retval, err
}
Beispiel #24
0
// Delete API allows to delete a typed JSON document from a specific index based on its id.
// http://www.elasticsearch.org/guide/reference/api/delete.html
// todo: add routing and versioning support
func Delete(pretty bool, index string, _type string, id string, version int, routing string) (api.BaseResponse, error) {
	var url string
	var retval api.BaseResponse
	url = fmt.Sprintf("/%s/%s/%s?%s", index, _type, id, api.Pretty(pretty))
	body, err := api.DoCommand("DELETE", url, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	//fmt.Println(body)
	return retval, err
}
Beispiel #25
0
// Count allows the caller to easily execute a query and get the number of matches for that query.
// It can be executed across one or more indices and across one or more types.
// The query can either be provided using a simple query string as a parameter,
// or using the Query DSL defined within the request body.
// http://www.elasticsearch.org/guide/reference/api/count.html
// TODO: take parameters.
// currently not working against 0.19.10
func Count(pretty bool, index string, _type string) (CountResponse, error) {
	var url string
	var retval CountResponse
	url = fmt.Sprintf("/%s/%s/_count?%s", index, _type, api.Pretty(pretty))
	body, err := api.DoCommand("GET", url, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	//fmt.Println(body)
	return retval, err
}
// State gets the comprehensive state information for the whole cluster
// see http://www.elasticsearch.org/guide/reference/api/admin-cluster-state/
func UpdateSetting(args map[string]interface{}, filter_indices ...string) (ClusterStateResponse, error) {
	var url string
	var retval ClusterStateResponse

	url = "/_cluster/state"

	body, err := api.DoCommand("GET", url, args, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
Beispiel #27
0
func Scroll(pretty bool, scroll_id string, scroll string) (SearchResult, error) {
	var url string
	var retval SearchResult

	url = fmt.Sprintf("/_search/scroll?%s%s", api.Pretty(pretty), api.Scroll(scroll))

	body, err := api.DoCommand("POST", url, scroll_id)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal([]byte(body), &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
// UpdateSettings allows to update cluster wide specific settings. Defaults to Transient setting
// Settings updated can either be persistent (applied cross restarts) or transient (will not survive a full cluster restart).
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html
func UpdateSettings(settingType string, key string, value int) (ClusterSettingsResponse, error) {
	var retval ClusterSettingsResponse
	if settingType != "transient" && settingType != "persistent" {
		return retval, fmt.Errorf("settingType must be one of transient or persistent, you passed %s", settingType)
	}
	var url string = "/_cluster/state"
	m := map[string]map[string]int{settingType: map[string]int{key: value}}
	body, err := api.DoCommand("PUT", url, nil, m)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}
// The inspecting the response
func ExampleBulkIndexer_responses() {
	indexer := core.NewBulkIndexer(10)
	// Create a custom Sender Func, to allow inspection of response/error
	indexer.BulkSender = func(buf *bytes.Buffer) error {
		// @buf is the buffer of docs about to be written
		respJson, err := api.DoCommand("POST", "/_bulk", nil, buf)
		if err != nil {
			// handle it better than this
			fmt.Println(string(respJson))
		}
		return err
	}
	done := make(chan bool)
	indexer.Run(done)

	for i := 0; i < 20; i++ {
		indexer.Index("twitter", "user", strconv.Itoa(i), "", nil, `{"name":"bob"}`, true)
	}
	done <- true // send shutdown signal
}
// AnalyzeIndices performs the analysis process on a text and return the tokens breakdown of the text.
// http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/
func OptimizeIndices(args map[string]interface{}, indices ...string) (OptimizeResponse, error) {
	var retval OptimizeResponse
	var optimizeUrl string = "/_optimize"
	if len(indices) > 0 {
		optimizeUrl = fmt.Sprintf("/%s/%s", strings.Join(indices, ","), optimizeUrl)
	}

	body, err := api.DoCommand("POST", optimizeUrl, args, nil)
	if err != nil {
		return retval, err
	}
	if err == nil {
		// marshall into json
		jsonErr := json.Unmarshal(body, &retval)
		if jsonErr != nil {
			return retval, jsonErr
		}
	}
	return retval, err
}