Created
November 22, 2016 22:58
-
-
Save hectorgool/58879409ea85da994bfa204bf65e924d 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
| /* | |
| https://github.com/olivere/elastic/issues/286 | |
| https://libraries.io/go/github.com%2Fmagicshui%2Felastic | |
| https://github.com/olivere/elastic/issues/130 | |
| https://olivere.github.io/elastic/ | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| j "github.com/ricardolonga/jsongo" | |
| "gopkg.in/olivere/elastic.v3" | |
| "encoding/json" | |
| ) | |
| func main() { | |
| 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()) | |
| elasticHost := "http://172.17.0.4:9200" //docker ip | |
| client, err := elastic.NewClient(elastic.SetURL(elasticHost)) | |
| if err != nil { | |
| panic(err) | |
| } | |
| searchResult, err := client.Search(). | |
| Index("mx"). | |
| Type("postal_code"). | |
| Source(searchJson). | |
| Pretty(true). | |
| Do() | |
| if err != nil { | |
| panic(err) | |
| } | |
| //fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis) | |
| //fmt.Printf("%d results\n", searchResult.TotalHits) | |
| if searchResult.Hits.TotalHits > 0 { | |
| //fmt.Printf("Found a total of %d documents\n", searchResult.Hits.TotalHits) | |
| // Iterate through results | |
| for _, hit := range searchResult.Hits.Hits { | |
| // hit.Index contains the name of the index | |
| // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}). | |
| var d Document | |
| err := json.Unmarshal(*hit.Source, &d) | |
| if err != nil { | |
| // Deserialization failed | |
| } | |
| // Work with document | |
| fmt.Printf("Document by %s: %s\n", d.Colonia, d.Ciudad) | |
| } | |
| } else { | |
| fmt.Print("Found no documents\n") | |
| } | |
| } | |
| 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"` | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment