Last active
November 23, 2016 16:11
-
-
Save hectorgool/67730c8a72f2d34b09e5a8888987ea0c to your computer and use it in GitHub Desktop.
How to
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
| [ | |
| { | |
| "cp": "03530", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Distrito Federal", | |
| "delegacion": "Benito Juarez", | |
| "location": { | |
| "lat": "19.3487", | |
| "lon": "-99.166" | |
| } | |
| }, | |
| { | |
| "cp": "81270", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Sinaloa", | |
| "delegacion": "Ahome", | |
| "location": { | |
| "lat": "25.1584", | |
| "lon": "-107.7063" | |
| } | |
| }, | |
| { | |
| "cp": "80140", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Sinaloa", | |
| "delegacion": "Culiacan", | |
| "location": { | |
| "lat": "25.0239", | |
| "lon": "-108.032" | |
| } | |
| } | |
| ] |
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() { | |
| 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://127.0.0.1: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 { | |
| for _, hit := range searchResult.Hits.Hits { | |
| var d Document | |
| err := json.Unmarshal(*hit.Source, &d) | |
| if err != nil { | |
| // Deserialization failed | |
| } | |
| 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"` | |
| } |
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
| curl -X DELETE 'http://localhost:9200/mex?pretty' | |
| curl -X PUT 'http://localhost:9200/mex?pretty' -d ' | |
| { | |
| "settings": { | |
| "number_of_shards": 1, | |
| "analysis": { | |
| "filter": { | |
| "autocomplete_filter": { | |
| "type": "edge_ngram", | |
| "min_gram": 1, | |
| "max_gram": 20 | |
| } | |
| }, | |
| "analyzer": { | |
| "autocomplete": { | |
| "type": "custom", | |
| "tokenizer": "standard", | |
| "filter": [ | |
| "lowercase", | |
| "autocomplete_filter" | |
| ] | |
| } | |
| } | |
| } | |
| }, | |
| "mappings": { | |
| "postal_code": { | |
| "properties": { | |
| "cp": { | |
| "type": "multi_field", | |
| "fields": { | |
| "cp": { | |
| "type" : "float", | |
| "store" : "yes", | |
| "precision_step" : "4" | |
| } | |
| } | |
| }, | |
| "colonia": { | |
| "type": "multi_field", | |
| "fields": { | |
| "colonia": { | |
| "type": "string", | |
| "index": "not_analyzed", | |
| "store": "yes" | |
| } | |
| } | |
| }, | |
| "ciudad": { | |
| "type": "multi_field", | |
| "fields": { | |
| "ciudad": { | |
| "type": "string", | |
| "index": "not_analyzed", | |
| "store": "yes" | |
| } | |
| } | |
| }, | |
| "delegacion": { | |
| "type": "multi_field", | |
| "fields": { | |
| "delegacion": { | |
| "type": "string", | |
| "index": "not_analyzed", | |
| "store": "yes" | |
| } | |
| } | |
| }, | |
| "location": { | |
| "type": "geo_point" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| ' | |
| curl -XGET 'http://localhost:9200/mex?pretty' | |
| curl -XGET 'http://localhost:9200/mex/postal_code/_search?pretty' | |
| curl -X PUT "http://localhost:9200/mex/postal_code/15962" -d " | |
| { | |
| \"cp\" : \"03530\", | |
| \"colonia\" : \"Villa de Cortes\", | |
| \"ciudad\" : \"Distrito Federal\", | |
| \"delegacion\" : \"Benito Juarez\", | |
| \"location\": { | |
| \"lat\": \"19.3487\", | |
| \"lon\": \"-99.166\" | |
| } | |
| }" | |
| curl -X PUT "http://localhost:9200/mex/postal_code/56142" -d " | |
| { | |
| \"cp\" : \"81270\", | |
| \"colonia\" : \"Villa de Cortes\", | |
| \"ciudad\" : \"Sinaloa\", | |
| \"delegacion\" : \"Ahome\", | |
| \"location\": { | |
| \"lat\": \"25.1584\", | |
| \"lon\": \"-107.7063\" | |
| } | |
| }" | |
| curl -X PUT "http://localhost:9200/mex/postal_code/56640" -d " | |
| { | |
| \"cp\" : \"80140\", | |
| \"colonia\" : \"Villa de Cortes\", | |
| \"ciudad\" : \"Sinaloa\", | |
| \"delegacion\" : \"Culiacan\", | |
| \"location\": { | |
| \"lat\": \"25.0239\", | |
| \"lon\": \"-108.032\" | |
| } | |
| }" |
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
| { | |
| "took": 30, | |
| "timed_out": false, | |
| "_shards": { | |
| "total": 1, | |
| "successful": 1, | |
| "failed": 0 | |
| }, | |
| "hits": { | |
| "total": 3, | |
| "max_score": null, | |
| "hits": [ | |
| { | |
| "_index": "mx", | |
| "_type": "postal_code", | |
| "_id": "15962", | |
| "_score": null, | |
| "_source": { | |
| "cp": "03530", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Distrito Federal", | |
| "delegacion": "Benito Juarez", | |
| "location": { | |
| "lat": "19.3487", | |
| "lon": "-99.166" | |
| } | |
| }, | |
| "sort": [ | |
| "Villa de Cortes" | |
| ] | |
| }, | |
| { | |
| "_index": "mx", | |
| "_type": "postal_code", | |
| "_id": "56142", | |
| "_score": null, | |
| "_source": { | |
| "cp": "81270", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Sinaloa", | |
| "delegacion": "Ahome", | |
| "location": { | |
| "lat": "25.1584", | |
| "lon": "-107.7063" | |
| } | |
| }, | |
| "sort": [ | |
| "Villa de Cortes" | |
| ] | |
| }, | |
| { | |
| "_index": "mx", | |
| "_type": "postal_code", | |
| "_id": "56640", | |
| "_score": null, | |
| "_source": { | |
| "cp": "80140", | |
| "colonia": "Villa de Cortes", | |
| "ciudad": "Sinaloa", | |
| "delegacion": "Culiacan", | |
| "location": { | |
| "lat": "25.0239", | |
| "lon": "-108.032" | |
| } | |
| }, | |
| "sort": [ | |
| "Villa de Cortes" | |
| ] | |
| } | |
| ] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment