Browse Source

v2: Added fuzzy search to stops API

master
Patrick Gaskin 1 year ago
parent
commit
30f25ab944
Signed by: geek1011 GPG Key ID: A2FD79F68A2AB707
3 changed files with 44 additions and 0 deletions
  1. +2
    -0
      go.mod
  2. +4
    -0
      go.sum
  3. +38
    -0
      v2/api.go

+ 2
- 0
go.mod View File

@ -6,6 +6,8 @@ require (
github.com/dinedal/textql v0.0.0-20170718222433-01160a69ae3b
github.com/jmoiron/sqlx v1.2.0
github.com/julienschmidt/httprouter v1.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/sahilm/fuzzy v0.1.0
github.com/spf13/pflag v1.0.3
google.golang.org/appengine v1.5.0 // indirect
)

+ 4
- 0
go.sum View File

@ -7,10 +7,14 @@ github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

+ 38
- 0
v2/api.go View File

@ -17,10 +17,12 @@ import (
"log"
"net/http"
"strconv"
"strings"
"time"
"git.geek1011.net/geek1011/drtapi/v2/drt"
"github.com/julienschmidt/httprouter"
"github.com/sahilm/fuzzy"
)
// Mount initializes and mounts the v1 api.
@ -57,6 +59,42 @@ func Mount(r *httprouter.Router) {
apiResponse{"error", fmt.Sprintf("could not get stops: %v", err)}.WriteResp(w, 500)
return
}
if q := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("q"))); q != "" {
ssStrings := make([]string, len(ss))
ssMap := map[string]drt.Stop{}
for i, stop := range ss {
ssStrings[i] = fmt.Sprintf("%d|%s", stop.ID, stop.Name)
ssMap[ssStrings[i]] = stop
}
var ssFound []drt.Stop
ssUsed := map[string]bool{}
for _, v := range ssStrings {
if idString := strings.Split(v, "|")[0]; idString == q {
ssUsed[v] = true
ssFound = append(ssFound, ssMap[v])
}
}
for _, v := range ssStrings {
if idString := strings.Split(v, "|")[0]; strings.HasPrefix(idString, q) {
ssUsed[v] = true
ssFound = append(ssFound, ssMap[v])
}
}
for i, match := range fuzzy.Find(q, ssStrings) {
if v := match.Str; !ssUsed[v] {
ssUsed[v] = true
ssFound = append(ssFound, ssMap[v])
}
if i >= 25 {
break
}
}
ss = ssFound
}
apiResponse{"success", ss}.WriteResp(w, 200)
})))

Loading…
Cancel
Save