This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.go
60 lines (48 loc) · 1.49 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"sort"
"strings"
"github.com/derekparker/trie"
)
var locationTrie = trie.New()
var locationTypeWeighting = map[string]int{
"City": 11,
"Town": 10,
"Village": 9,
"Hamlet": 8,
"Other Settlement": 7,
"Suburban Area": 6,
"Named Road": 5,
"Numbered Road": 4,
"Section Of Named Road": 3,
"Section Of Numbered Road": 2,
"Postcode": 1,
}
func getSearchResults(term string) SearchResults {
searchResults := make(map[string]LocationRecords)
counter := 0
results := locationTrie.PrefixSearch(strings.ToLower(term))
for _, item := range results {
node, _ := locationTrie.Find(item)
meta := node.Meta()
recordList := meta.([]LocationRecord)
for _, locationRecord := range recordList {
typeName := locationRecord.GetTypeName()
locationRecord = MBRToLatLon(locationRecord)
searchResults[typeName] = append(searchResults[typeName], locationRecord)
counter++
if counter == MaxResults {
return sortSearchResults(searchResults)
}
}
}
return sortSearchResults(searchResults)
}
func sortSearchResults(searchResults map[string]LocationRecords) map[string]LocationRecords {
for key := range searchResults {
sort.Slice(searchResults[key][:], func(i, j int) bool {
return locationTypeWeighting[searchResults[key][i].LocalType] > locationTypeWeighting[searchResults[key][j].LocalType]
})
}
return searchResults
}