Created
November 22, 2016 19:01
-
-
Save hectorgool/b09bdab637d96ef0d02c1ccf24a617f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| http://json2struct.mervine.net/ | |
| https://mholt.github.io/json-to-go/ | |
| http://stackoverflow.com/questions/26511417/unmarshal-json-data-in-a-specific-struct-in-golang | |
| { | |
| "cp": "81270", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Sinaloa", | |
| "delegacion": "Ahome", | |
| "location": { | |
| "lat": "25.1584", | |
| "lon": "-107.7063" | |
| } | |
| } | |
| */ | |
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| elastigo "github.com/mattbaird/elastigo/lib" | |
| j "github.com/ricardolonga/jsongo" | |
| "log" | |
| "os" | |
| "encoding/json" | |
| ) | |
| var ( | |
| host *string = flag.String("host", "172.17.0.4", "Elasticsearch Host") | |
| ) | |
| func main() { | |
| c := elastigo.NewConn() | |
| log.SetFlags(log.LstdFlags) | |
| flag.Parse() | |
| // Trace all requests | |
| c.RequestTracer = func(method, url, body string) { | |
| log.Printf("Requesting %s %s", method, url) | |
| log.Printf("Request body: %s", body) | |
| } | |
| fmt.Println("host = ", *host) | |
| // Set the Elasticsearch Host to Connect to | |
| c.Domain = *host | |
| // Search Using Raw json String | |
| term := "villa de cortes" | |
| searchJson := j.Object(). | |
| Put("size", 10). | |
| Put("query", j.Object(). | |
| Put("match", j.Object(). | |
| Put("_all", j.Object(). | |
| Put("query", term). | |
| Put("operator", "and")))). | |
| Put("sort", j.Array(). | |
| Put(j.Object(). | |
| Put("colonia", j.Object(). | |
| Put("order", "asc"). | |
| Put("mode", "avg")))) | |
| fmt.Println(searchJson.Indent()) | |
| searchresponse, err := c.Search("mx", "postal_code", nil, searchJson) | |
| var d Document | |
| bytes, err := searchresponse.Hits.Hits[0].Source.MarshalJSON() | |
| if err != nil { | |
| log.Fatalf("err calling marshalJson:%v", err) | |
| } | |
| log.Printf("%s", bytes) | |
| json.Unmarshal(bytes, &d) | |
| log.Printf("Search Found: %s", d) | |
| /* | |
| if len(out.Hits.Hits) >= 1 { | |
| fmt.Println("%v", out.Hits.Hits[0].Source) | |
| } | |
| exitIfErr(err) | |
| fmt.Println("%n", len(out.Hits.Hits)) | |
| */ | |
| } | |
| func exitIfErr(err error) { | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) | |
| os.Exit(1) | |
| } | |
| } | |
| type Document struct { | |
| Ciudad string `json:"ciudad"` | |
| Colonia string `json:"colonia"` | |
| Cp string `json:"cp"` | |
| Delegacion string `json:"delegacion"` | |
| Location struct { | |
| Lat string `json:"lat"` | |
| Lon string `json:"lon"` | |
| } `json:"location"` | |
| } | |
| func (d *Document) String() string { | |
| b, _ := json.Marshal(d) | |
| return string(b) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment