this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add sorting and pagination and 'and' to search (#177)

Not sure why ive been dragging my feet on this but this should make
search suck less

authored by

Whyrusleeping and committed by
GitHub
d5b32d01 7a54bcef

+43 -13
+29 -1
search/handlers.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "fmt" 6 + "strconv" 5 7 "strings" 6 8 7 9 api "github.com/bluesky-social/indigo/api" ··· 35 37 }) 36 38 } 37 39 38 - out, err := s.SearchPosts(ctx, q) 40 + offset := 0 41 + if q := strings.TrimSpace(e.QueryParam("offset")); q != "" { 42 + v, err := strconv.Atoi(q) 43 + if err != nil { 44 + return &echo.HTTPError{ 45 + Code: 400, 46 + Message: fmt.Sprintf("invalid value for 'offset': %s", err), 47 + } 48 + } 49 + 50 + offset = v 51 + } 52 + 53 + count := 30 54 + if q := strings.TrimSpace(e.QueryParam("count")); q != "" { 55 + v, err := strconv.Atoi(q) 56 + if err != nil { 57 + return &echo.HTTPError{ 58 + Code: 400, 59 + Message: fmt.Sprintf("invalid value for 'count': %s", err), 60 + } 61 + } 62 + 63 + count = v 64 + } 65 + 66 + out, err := s.SearchPosts(ctx, q, offset, count) 39 67 if err != nil { 40 68 return err 41 69 }
+11 -9
search/query.go
··· 44 44 Post any `json:"post"` 45 45 } 46 46 47 - func doSearchPosts(ctx context.Context, escli *es.Client, q string) (*EsSearchResponse, error) { 47 + func doSearchPosts(ctx context.Context, escli *es.Client, q string, offset int, size int) (*EsSearchResponse, error) { 48 48 query := map[string]interface{}{ 49 - /* 50 - "sort": map[string]any{ 51 - "createdAt": map[string]any{ 52 - "order": "desc", 53 - "format": "date_nanos", 54 - }, 49 + "sort": map[string]any{ 50 + "createdAt": map[string]any{ 51 + "order": "desc", 55 52 }, 56 - */ 53 + }, 57 54 "query": map[string]interface{}{ 58 55 "match": map[string]interface{}{ 59 - "text": q, 56 + "text": map[string]any{ 57 + "query": q, 58 + "operator": "and", 59 + }, 60 60 }, 61 61 }, 62 + "size": size, 63 + "from": offset, 62 64 } 63 65 64 66 return doSearch(ctx, escli, "posts", query)
+3 -3
search/server.go
··· 25 25 "github.com/ipfs/go-cid" 26 26 flatfs "github.com/ipfs/go-ds-flatfs" 27 27 blockstore "github.com/ipfs/go-ipfs-blockstore" 28 + logging "github.com/ipfs/go-log" 28 29 "github.com/labstack/echo/v4" 29 30 "github.com/labstack/echo/v4/middleware" 30 31 es "github.com/opensearch-project/opensearch-go/v2" 31 32 gorm "gorm.io/gorm" 32 - logging "github.com/ipfs/go-log" 33 33 ) 34 34 35 35 var log = logging.Logger("search") ··· 282 282 }) 283 283 } 284 284 285 - func (s *Server) SearchPosts(ctx context.Context, srch string) ([]PostSearchResult, error) { 286 - resp, err := doSearchPosts(ctx, s.escli, srch) 285 + func (s *Server) SearchPosts(ctx context.Context, srch string, offset, size int) ([]PostSearchResult, error) { 286 + resp, err := doSearchPosts(ctx, s.escli, srch, offset, size) 287 287 if err != nil { 288 288 return nil, err 289 289 }