Skip to content

Commit

Permalink
Merge pull request #313 from hearchco/as/fix/bangs
Browse files Browse the repository at this point in the history
fix!: removed bangs, since they can be handled by frontend
  • Loading branch information
aleksasiriski authored May 28, 2024
2 parents 146b103 + 28c5c16 commit ced3fab
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 155 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
run:
air --pretty
air -- --pretty
run-cli:
go run ./src --cli --pretty

debug:
air -- -v --pretty
air -- --pretty -v
debug-cli:
go run ./srv -v --cli --pretty
go run ./srv --cli --pretty -v

trace:
air -- -vv --pretty
air -- --pretty -vv
trace-cli:
go run ./src -vv --cli --pretty
go run ./src --cli --pretty -vv

install:
go get ./...
Expand Down
11 changes: 8 additions & 3 deletions src/cli/climode.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ func Run(flags Flags, db cache.DB, conf config.Config) {
Bool("visit", flags.Visit).
Msg("Started hearching")

categoryName, err := category.FromString(flags.Category)
if err != nil {
log.Fatal().Err(err).Msg("Invalid category")
}

options := engines.Options{
Pages: engines.Pages{
Start: flags.StartPage,
Max: flags.MaxPages,
},
VisitPages: flags.Visit,
Category: category.FromString[flags.Category],
Category: categoryName,
UserAgent: flags.UserAgent,
Locale: flags.Locale,
SafeSearch: flags.SafeSearch,
Expand All @@ -76,7 +81,7 @@ func Run(flags Flags, db cache.DB, conf config.Config) {

start := time.Now()

results, foundInDB := search.Search(flags.Query, options, db, conf.Settings, conf.Categories, conf.Server.Proxy.Salt)
results, foundInDB := search.Search(flags.Query, options, db, conf.Categories[options.Category], conf.Settings, conf.Server.Proxy.Salt)

if !flags.Silent {
printResults(results)
Expand All @@ -87,5 +92,5 @@ func Run(flags Flags, db cache.DB, conf config.Config) {
Dur("duration", time.Since(start)).
Msg("Found results")

search.CacheAndUpdateResults(flags.Query, options, db, conf.Server.Cache.TTL, conf.Settings, conf.Categories, results, foundInDB, conf.Server.Proxy.Salt)
search.CacheAndUpdateResults(flags.Query, options, db, conf.Server.Cache.TTL, conf.Categories[options.Category], conf.Settings, results, foundInDB, conf.Server.Proxy.Salt)
}
2 changes: 1 addition & 1 deletion src/cli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Setup() Flags {
// ^FATAL
}

if category.SafeFromString(cli.Category) == category.UNDEFINED {
if _, err := category.FromString(cli.Category); err != nil {
log.Fatal().Msg("cli.Setup(): invalid category flag")
// ^FATAL
}
Expand Down
27 changes: 13 additions & 14 deletions src/router/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Search(w http.ResponseWriter, r *http.Request, db cache.DB, ttlConf config.

params := r.Form

query := getParamOrDefault(params, "q")
query := strings.TrimSpace(getParamOrDefault(params, "q"))
pagesStartS := getParamOrDefault(params, "start", "1")
pagesMaxS := getParamOrDefault(params, "pages", "1")
visitPagesS := getParamOrDefault(params, "deep", "false")
Expand All @@ -42,10 +42,12 @@ func Search(w http.ResponseWriter, r *http.Request, db cache.DB, ttlConf config.
safeSearchS := getParamOrDefault(params, "safesearch", "false")
mobileS := getParamOrDefault(params, "mobile", "false")

queryWithoutSpaces := strings.TrimSpace(query)
if queryWithoutSpaces == "" || "!"+string(category.FromQuery(query)) == queryWithoutSpaces {
// return "[]" JSON when the query is empty or contains only category name
return writeResponseJSON(w, http.StatusOK, []struct{}{})
if query == "" {
// user error
return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{
Message: "query cannot be empty or whitespace",
Value: "empty query",
})
}

pagesMax, err := strconv.Atoi(pagesMaxS)
Expand Down Expand Up @@ -106,12 +108,12 @@ func Search(w http.ResponseWriter, r *http.Request, db cache.DB, ttlConf config.
})
}

categoryName := category.SafeFromString(categoryS)
if categoryName == category.UNDEFINED {
categoryName, err := category.FromString(categoryS)
if err != nil {
// user error
return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{
Message: "invalid category value",
Value: fmt.Sprintf("%v", category.UNDEFINED),
Value: fmt.Sprintf("%v", categoryName),
})
}

Expand Down Expand Up @@ -147,13 +149,10 @@ func Search(w http.ResponseWriter, r *http.Request, db cache.DB, ttlConf config.
}

// search for results in db and web, afterwards return JSON
results, foundInDB := search.Search(query, options, db, settings, categories, salt)

// get category from query or options
cat := category.FromQueryWithFallback(query, options.Category)
results, foundInDB := search.Search(query, options, db, categories[options.Category], settings, salt)

// send response as soon as possible
if cat == category.IMAGES {
if categoryName == category.IMAGES {
resultsOutput := result.ConvertToImageOutput(results)
err = writeResponseJSON(w, http.StatusOK, resultsOutput)
} else {
Expand All @@ -162,7 +161,7 @@ func Search(w http.ResponseWriter, r *http.Request, db cache.DB, ttlConf config.
}

// don't return immediately, we want to cache results and update them if necessary
search.CacheAndUpdateResults(query, options, db, ttlConf, settings, categories, results, foundInDB, salt)
search.CacheAndUpdateResults(query, options, db, ttlConf, categories[options.Category], settings, results, foundInDB, salt)

// if writing response failed, return the error
return err
Expand Down
75 changes: 0 additions & 75 deletions src/search/bang.go

This file was deleted.

5 changes: 2 additions & 3 deletions src/search/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"github.com/hearchco/hearchco/src/anonymize"
"github.com/hearchco/hearchco/src/cache"
"github.com/hearchco/hearchco/src/config"
"github.com/hearchco/hearchco/src/search/category"
"github.com/hearchco/hearchco/src/search/engines"
"github.com/hearchco/hearchco/src/search/result"
"github.com/rs/zerolog/log"
)

func CacheAndUpdateResults(
query string, options engines.Options, db cache.DB,
ttlConf config.TTL, settings map[engines.Name]config.Settings, categories map[category.Name]config.Category,
ttlConf config.TTL, categoryConf config.Category, settings map[engines.Name]config.Settings,
results []result.Result, foundInDB bool,
salt string,
) {
Expand Down Expand Up @@ -46,7 +45,7 @@ func CacheAndUpdateResults(
Str("queryAnon", anonymize.String(query)).
Str("queryHash", anonymize.HashToSHA256B64(query)).
Msg("Updating results...")
newResults := PerformSearch(query, options, settings, categories, salt)
newResults := PerformSearch(query, options, categoryConf, settings, salt)
uerr := db.Set(query, newResults, ttlConf.Time)
if uerr != nil {
// Error in updating cache is not returned, just logged
Expand Down
42 changes: 11 additions & 31 deletions src/search/category/category.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package category

import (
"strings"
)
import "fmt"

var FromString = map[string]Name{
var catMap = map[string]Name{
"general": GENERAL,
"images": IMAGES,
"science": SCIENCE,
Expand All @@ -13,36 +11,18 @@ var FromString = map[string]Name{
"slow": THOROUGH,
}

// returns category
func FromQuery(query string) Name {
if query == "" || query[0] != '!' {
return ""
}
cat := strings.SplitN(query, " ", 2)[0][1:]
if val, ok := FromString[cat]; ok {
return val
}
return ""
}

func SafeFromString(cat string) Name {
// converts a string to a category name if it exists
// if the string is empty, then GENERAL is returned
// otherwise returns UNDEFINED
func FromString(cat string) (Name, error) {
if cat == "" {
return ""
return GENERAL, nil
}
ret, ok := FromString[cat]

catName, ok := catMap[cat]
if !ok {
return UNDEFINED
return UNDEFINED, fmt.Errorf("category %q is not defined", cat)
}
return ret
}

func FromQueryWithFallback(query string, fallback Name) Name {
cat := FromQuery(query)
if cat != "" {
return cat
} else if fallback != "" {
return fallback
} else {
return GENERAL
}
return catName, nil
}
6 changes: 3 additions & 3 deletions src/search/engines/_engines_test/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func CheckTestCases(tchar []TestCaseHasAnyResults, tccr []TestCaseContainsResult

// TestCaseHasAnyResults
for _, tc := range tchar {
if results := search.PerformSearch(tc.Query, tc.Options, conf.Settings, conf.Categories, ""); len(results) == 0 {
if results := search.PerformSearch(tc.Query, tc.Options, conf.Categories[tc.Options.Category], conf.Settings, ""); len(results) == 0 {
defer t.Errorf("Got no results for %q", tc.Query)
}
}

// TestCaseContainsResults
for _, tc := range tccr {
results := search.PerformSearch(tc.Query, tc.Options, conf.Settings, conf.Categories, "")
results := search.PerformSearch(tc.Query, tc.Options, conf.Categories[tc.Options.Category], conf.Settings, "")
if len(results) == 0 {
defer t.Errorf("Got no results for %q", tc.Query)
} else {
Expand All @@ -43,7 +43,7 @@ func CheckTestCases(tchar []TestCaseHasAnyResults, tccr []TestCaseContainsResult

// TestCaseRankedResults
for _, tc := range tcrr {
results := search.PerformSearch(tc.Query, tc.Options, conf.Settings, conf.Categories, "")
results := search.PerformSearch(tc.Query, tc.Options, conf.Categories[tc.Options.Category], conf.Settings, "")
if len(results) == 0 {
defer t.Errorf("Got no results for %q", tc.Query)
} else if len(results) < len(tc.ResultURL) {
Expand Down
26 changes: 9 additions & 17 deletions src/search/perform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,47 @@ import (
"github.com/hearchco/hearchco/src/anonymize"
"github.com/hearchco/hearchco/src/config"
"github.com/hearchco/hearchco/src/search/bucket"
"github.com/hearchco/hearchco/src/search/category"
"github.com/hearchco/hearchco/src/search/engines"
"github.com/hearchco/hearchco/src/search/rank"
"github.com/hearchco/hearchco/src/search/result"
"github.com/rs/zerolog/log"
)

func PerformSearch(query string, options engines.Options, settings map[engines.Name]config.Settings, categories map[category.Name]config.Category, salt string) []result.Result {
func PerformSearch(query string, options engines.Options, categoryConf config.Category, settings map[engines.Name]config.Settings, salt string) []result.Result {
// check for empty query
if query == "" {
log.Trace().Msg("Empty search query.")
return []result.Result{}
}

// start searching
searchTimer := time.Now()

query, cat, timings, enginesToRun := procBang(query, options.Category, settings, categories)
// set the new category only within the scope of this function
options.Category = cat
query = url.QueryEscape(query)

// check again after the bang is taken out
if query == "" {
log.Trace().Msg("Empty search query (with bang present).")
return []result.Result{}
}

log.Debug().
Str("queryAnon", anonymize.String(query)).
Str("queryHash", anonymize.HashToSHA256B64(query)).
Msg("Searching")
Msg("Searching...")

// getting results from engines
resTimer := time.Now()
log.Debug().Msg("Waiting for results from engines...")

resultMap := runEngines(enginesToRun, query, options, settings, timings, salt)
resultMap := runEngines(categoryConf.Engines, url.QueryEscape(query), options, settings, categoryConf.Timings, salt)

log.Debug().
Dur("duration", time.Since(resTimer)).
Msg("Got results")

// ranking results
rankTimer := time.Now()
log.Debug().Msg("Ranking...")

results := rank.Rank(resultMap, categories[options.Category].Ranking)
results := rank.Rank(resultMap, categoryConf.Ranking)

log.Debug().
Dur("duration", time.Since(rankTimer)).
Msg("Finished ranking")

// finish searching
log.Debug().
Dur("duration", time.Since(searchTimer)).
Msg("Found results")
Expand Down
Loading

0 comments on commit ced3fab

Please sign in to comment.