| 
									
										
										
										
											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_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2014-05-30 05:01:39 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2014-05-30 05:01:39 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/belogik/goes" | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	ES_HOST = "localhost" | 
					
						
							|  |  |  | 	ES_PORT = "9200" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getConnection() (conn *goes.Connection) { | 
					
						
							|  |  |  | 	h := os.Getenv("TEST_ELASTICSEARCH_HOST") | 
					
						
							|  |  |  | 	if h == "" { | 
					
						
							|  |  |  | 		h = ES_HOST | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p := os.Getenv("TEST_ELASTICSEARCH_PORT") | 
					
						
							|  |  |  | 	if p == "" { | 
					
						
							|  |  |  | 		p = ES_PORT | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	conn = goes.NewConnection(h, p) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | func ExampleConnection_CreateIndex() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mapping := map[string]interface{}{ | 
					
						
							|  |  |  | 		"settings": map[string]interface{}{ | 
					
						
							|  |  |  | 			"index.number_of_shards":   1, | 
					
						
							|  |  |  | 			"index.number_of_replicas": 0, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		"mappings": map[string]interface{}{ | 
					
						
							|  |  |  | 			"_default_": map[string]interface{}{ | 
					
						
							|  |  |  | 				"_source": map[string]interface{}{ | 
					
						
							|  |  |  | 					"enabled": true, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				"_all": map[string]interface{}{ | 
					
						
							|  |  |  | 					"enabled": false, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp, err := conn.CreateIndex("test", mapping) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnection_DeleteIndex() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 	resp, err := conn.DeleteIndex("yourinde") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnection_RefreshIndex() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 	resp, err := conn.RefreshIndex("yourindex") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnection_Search() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var query = map[string]interface{}{ | 
					
						
							|  |  |  | 		"query": map[string]interface{}{ | 
					
						
							|  |  |  | 			"bool": map[string]interface{}{ | 
					
						
							|  |  |  | 				"must": map[string]interface{}{ | 
					
						
							|  |  |  | 					"match_all": map[string]interface{}{}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		"from":   0, | 
					
						
							|  |  |  | 		"size":   100, | 
					
						
							|  |  |  | 		"fields": []string{"onefield"}, | 
					
						
							|  |  |  | 		"filter": map[string]interface{}{ | 
					
						
							|  |  |  | 			"range": map[string]interface{}{ | 
					
						
							|  |  |  | 				"somefield": map[string]interface{}{ | 
					
						
							|  |  |  | 					"from":          "some date", | 
					
						
							|  |  |  | 					"to":            "some date", | 
					
						
							|  |  |  | 					"include_lower": false, | 
					
						
							|  |  |  | 					"include_upper": false, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-30 05:01:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-12 06:24:23 +08:00
										 |  |  | 	extraArgs := make(url.Values, 1) | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-12 06:24:23 +08:00
										 |  |  | 	searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs) | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", searchResults) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnection_Index() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	d := goes.Document{ | 
					
						
							|  |  |  | 		Index: "twitter", | 
					
						
							|  |  |  | 		Type:  "tweet", | 
					
						
							|  |  |  | 		Fields: map[string]interface{}{ | 
					
						
							|  |  |  | 			"user":    "foo", | 
					
						
							|  |  |  | 			"message": "bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	extraArgs := make(url.Values, 1) | 
					
						
							|  |  |  | 	extraArgs.Set("ttl", "86400000") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	response, err := conn.Index(d, extraArgs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", response) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnection_Delete() { | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//[create index, index document ...]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d := goes.Document{ | 
					
						
							|  |  |  | 		Index: "twitter", | 
					
						
							|  |  |  | 		Type:  "tweet", | 
					
						
							| 
									
										
										
										
											2014-01-12 00:57:32 +08:00
										 |  |  | 		Id:    "1", | 
					
						
							| 
									
										
										
										
											2013-06-15 14:18:48 +08:00
										 |  |  | 		Fields: map[string]interface{}{ | 
					
						
							|  |  |  | 			"user": "foo", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	response, err := conn.Delete(d, url.Values{}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", response) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-30 05:01:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func ExampleConnectionOverrideHttpClient() { | 
					
						
							|  |  |  | 	tr := &http.Transport{ | 
					
						
							|  |  |  | 		ResponseHeaderTimeout: 1 * time.Second, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cl := &http.Client{ | 
					
						
							|  |  |  | 		Transport: tr, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-18 04:26:08 +08:00
										 |  |  | 	conn := getConnection() | 
					
						
							|  |  |  | 	conn.WithClient(cl) | 
					
						
							| 
									
										
										
										
											2014-05-30 05:01:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%v\n", conn.Client) | 
					
						
							|  |  |  | } |