Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Created March 31, 2016 21:53
Show Gist options
  • Save hectorgool/c9e18d7d6324a9ed1a2df92ddcc95c08 to your computer and use it in GitHub Desktop.
Save hectorgool/c9e18d7d6324a9ed1a2df92ddcc95c08 to your computer and use it in GitHub Desktop.
Elastigo Example
/*
curl -X PUT "http://localhost:9200/mx2/postal_code/1" -d "
{
\"cp\" : \"20000\",
\"colonia\" : \"Zona Centro\",
\"ciudad\" : \"Aguascalientes\",
\"delegacion\" : \"Aguascalientes\",
\"location\": {
\"lat\": \"22.0074\",
\"lon\": \"-102.2837\"
}
}"
curl -X PUT "http://localhost:9200/mx2/postal_code/2" -d "
{
\"cp\" : \"20008\",
\"colonia\" : \"Delegacion de La Secretaria de Comercio y Fomento Industrial\",
\"ciudad\" : \"Aguascalientes\",
\"delegacion\" : \"Aguascalientes\",
\"location\": {
\"lat\": \"22.0074\",
\"lon\": \"-102.2837\"
}
}"
*/
package main
import (
"encoding/json"
"flag"
"log"
elastigo "github.com/mattbaird/elastigo/lib"
)
var (
eshost *string = flag.String("host", "localhost", "Elasticsearch Server Host Address")
)
func main() {
flag.Parse()
log.SetFlags(log.Ltime | log.Lshortfile)
c := elastigo.NewConn()
c.Domain = *eshost
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
searchJson := `{
"size": 10,
"query": {
"match": {
"all": {
"query": "aguascalientes",
"operator": "and"
}
}
},
"sort": [{
"colonia": {
"order": "asc",
"mode": "avg"
}
}]
}`
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
searchresponse, err := c.Search("mx2", "postal_code", nil, searchJson)
if err != nil {
log.Println("error during search:" + err.Error())
log.Fatal(err)
}
// try marshalling to ElasticSearchResponse type
var t ElasticSearchResponse
bytes, err := searchresponse.Hits.Hits[0].Source.MarshalJSON()
if err != nil {
log.Fatalf("err calling marshalJson:%v", err)
}
json.Unmarshal(bytes, &t)
log.Printf("Search Found: %s", t)
c.Flush()
}
func (t *ElasticSearchResponse) String() string {
b, _ := json.Marshal(t)
return string(b)
}
// used in test suite, chosen to be similar to the documentation
type ElasticSearchResponse struct {
Cp string `json:"cp"`
Colonia string `json:"colonia"`
Ciudad string `json:"ciudad"`
Delegacion string `json:"delegacion"`
Location Location `json:"location"`
}
type Location struct {
Lat string `json:"lat"`
Lon string `json:"lon"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment