Finalize search endpoint

pull/24/head
Aloïs Micard 4 years ago
parent cacf4f1236
commit 8d9d9524a7
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -115,15 +115,8 @@ func searchResources(es *elastic.Client) echo.HandlerFunc {
return c.NoContent(http.StatusUnprocessableEntity)
}
// Build the search query
var query elastic.Query
if url := string(b); url != "" {
query = elastic.NewMatchQuery("url", url)
} else {
query = elastic.NewMatchAllQuery()
}
// Perform the search request.
query := buildSearchQuery(string(b), c.QueryParam("keyword"))
res, err := es.Search().
Index(resourcesIndex).
Query(query).
@ -186,6 +179,27 @@ func addResource(es *elastic.Client) echo.HandlerFunc {
}
}
func buildSearchQuery(url, keyword string) elastic.Query {
var queries []elastic.Query
if url != "" {
queries = append(queries, elastic.NewTermQuery("url", url))
}
if keyword != "" {
queries = append(queries, elastic.NewTermQuery("body", keyword))
}
// Handle specific case
if len(queries) == 0 {
return elastic.NewMatchAllQuery()
}
if len(queries) == 1 {
return queries[0]
}
// otherwise AND combine them
return elastic.NewBoolQuery().Must(queries...)
}
func scheduleURL(nc *nats.Conn) echo.HandlerFunc {
return func(c echo.Context) error {
var url string

Loading…
Cancel
Save