Last active
November 25, 2016 14:35
-
-
Save hectorgool/e805c9225bf8c3fa30643da6cc871b53 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
| package main | |
| import ( | |
| "fmt" | |
| j "github.com/ricardolonga/jsongo" | |
| "gopkg.in/olivere/elastic.v3" | |
| ) | |
| 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")))) | |
| elasticHost := "http://172.17.0.2:9200" | |
| 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) | |
| } | |
| if searchResult.Hits.TotalHits > 0 { | |
| jsonArray := j.Array() | |
| for _, hit := range searchResult.Hits.Hits { | |
| jsonArray.Put(hit.Source) | |
| } | |
| fmt.Print(jsonArray.Indent()) | |
| } else { | |
| fmt.Print("Found no documents\n") | |
| } | |
| } |
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
| package main | |
| import ( | |
| "fmt" | |
| j "github.com/ricardolonga/jsongo" | |
| "gopkg.in/olivere/elastic.v3" | |
| "encoding/json" | |
| ) | |
| func main() { | |
| //fmt.Print("ok\n") | |
| term := "villa de cortes" | |
| rawJsonDocuments := searchTerm(term) | |
| fmt.Printf("%v", rawJsonDocuments ) | |
| } | |
| func searchTerm(term string) string { | |
| result := "" | |
| 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")))) | |
| elasticHost := "http://172.17.0.2:9200" | |
| 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) | |
| } | |
| if searchResult.Hits.TotalHits > 0 { | |
| //fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits) | |
| // Iterate through results | |
| var documents []Document | |
| 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 | |
| } | |
| documents = append(documents, d) | |
| // Work with tweet | |
| //fmt.Printf("Document %s\n", d.Ciudad) | |
| } | |
| rawJsonDocuments, err := json.Marshal(documents) | |
| if err != nil { | |
| // Deserialization failed | |
| } | |
| //fmt.Printf("%v", string(rawJsonDocuments) ) | |
| result = string(rawJsonDocuments) | |
| } | |
| return result | |
| } | |
| 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