Skip to content

Commit

Permalink
(feat) add search by html body
Browse files Browse the repository at this point in the history
fixes #236
  • Loading branch information
leonjza committed Oct 2, 2024
1 parent f5713f3 commit 97f1a78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions web/api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type searchResult struct {
MatchedFields []string `json:"matched_fields"`
}

// searchOperators are the operators we support. everything else is
// "free text"
var searchOperators = []string{"title", "body", "tech", "header", "p"}

// SearchHandler handles search
//
// @Summary Search for results
// @Description Searches for results based on free form text, or operators.
// @Tags Results
// @Accept json
// @Produce json
// @Param query body searchRequest true "The search term to search for. Supports search operators: `title:`, `tech:`, `header:`"
// @Param query body searchRequest true "The search term to search for. Supports search operators: `title:`, `tech:`, `header:`, `body:`, `p:`"
// @Success 200 {object} searchResult
// @Router /search [post]
func (h *ApiHandler) SearchHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -81,6 +85,16 @@ func (h *ApiHandler) SearchHandler(w http.ResponseWriter, r *http.Request) {
}

searchResults = appendResults(searchResults, resultIDs, techResults, key)

case "body":
var bodyResults []models.Result
if err := h.DB.Model(&models.Result{}).
Where("LOWER(html) LIKE ?", lowerValue).Find(&bodyResults).Error; err != nil {
log.Error("failed to get html results", "err", err)
return
}
searchResults = appendResults(searchResults, resultIDs, bodyResults, key)

case "header":
var headerResults []models.Result
if err := h.DB.Model(&models.Result{}).
Expand Down Expand Up @@ -147,7 +161,7 @@ func (h *ApiHandler) SearchHandler(w http.ResponseWriter, r *http.Request) {
// and captures any remaining free-form text.
func parseSearchQuery(query string) (map[string]string, string) {
// Operators that we know of and that will be parsed
operators := []string{"title", "tech", "header", "p"}

result := make(map[string]string)

var freeText string
Expand All @@ -162,7 +176,7 @@ func parseSearchQuery(query string) (map[string]string, string) {
// Check if the part contains an operator (e.g., title: or tech:)
if index := strings.Index(part, ":"); index != -1 {
operator := part[:index]
if slices.Contains(operators, operator) {
if slices.Contains(searchOperators, operator) {
// If we are processing an operator, finalize the previous key-value pair
if currentKey != "" {
result[currentKey] = strings.Join(currentValue, " ")
Expand Down
1 change: 1 addition & 0 deletions web/ui/src/components/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const navs = [

const searchOperators = [
{ key: 'title', description: 'search by title' },
{ key: 'body', description: 'search by html body' },
{ key: 'tech', description: 'search by technology' },
{ key: 'header', description: 'search by header' },
{ key: 'p', description: 'search by perception hash' },
Expand Down

0 comments on commit 97f1a78

Please sign in to comment.