Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var waitGroup sync.WaitGroup
- // CitySearchByName returns autocomplete suggestions for cities
- func CitySearchByName(es *elasticsearch.Client, name string, location locationModel.Location, unit string) ([]*City, error) {
- q, err := GenerateAutocompleteQuery(name, location, unit)
- if err != nil {
- return nil, err
- }
- elasticsearchResult, err := esService.Search(es, q)
- if err != nil {
- return nil, err
- }
- resultLength := len(elasticsearchResult.Hits)
- c := make(chan *City, resultLength)
- waitGroup.Add(resultLength)
- go mapElasticsearchResultToCities(c, elasticsearchResult, unit)
- cities := make([]*City, 0)
- for elem := range c {
- cities = append(cities, elem)
- }
- waitGroup.Wait()
- return cities, nil
- }
- func mapElasticsearchResultToCities(c chan *City, elasticsearchResult *esService.ElasticsearchResult, unit string) {
- for _, hit := range elasticsearchResult.Hits {
- source := hit.Source
- fields := hit.Fields
- highlight := hit.Highlight
- city, err := mapElasticsearchHitToCity(source, fields, highlight, unit)
- if err != nil {
- // Error handling...
- }
- c <- city
- waitGroup.Done()
- }
- close(c)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement