Skip to content

Commit

Permalink
integrate couchbase FTS
Browse files Browse the repository at this point in the history
  • Loading branch information
JLL32 committed Jan 21, 2025
1 parent cb4e94f commit c78e1ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/saferwall/saferwall-api

go 1.21
go 1.23.0

require (
github.com/MicahParks/recaptcha v0.0.5
Expand Down Expand Up @@ -61,6 +61,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/saferwall/advanced-search v0.0.0-20250120184926-f1a096cecd50 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"time"

gocb "github.com/couchbase/gocb/v2"
"github.com/couchbase/gocb/v2/search"
"github.com/saferwall/advanced-search/gen"
"github.com/saferwall/advanced-search/lexer"
"github.com/saferwall/advanced-search/parser"
"github.com/saferwall/advanced-search/token"
)

const (
Expand Down Expand Up @@ -248,11 +251,25 @@ func shortID(length int) string {
return string(b)
}

func (db *DB) Search(ctx context.Context, val *interface{}, totalHits *uint64) error {
func generate(input string) map[string]interface{} {
l := lexer.New(input)
var tokens []*token.Token
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
tokCopy := tok
tokens = append(tokens, &tokCopy)
}

p := parser.New(tokens)
expr, _ := p.Parse()
result, _ := gen.GenerateCouchbaseFTS(expr)
return result
}

func (db *DB) Search(ctx context.Context, stringQuery string, val *interface{}, totalHits *uint64) error {

queryOne := search.NewMatchQuery("spyware").Field("multiav.last_scan.avast.output")
queryTwo := search.NewTermQuery("exe").Field("file_extension")
query := search.NewConjunctionQuery(queryOne, queryTwo)
// queryOne := search.NewMatchQuery("spyware").Field("multiav.last_scan.avast.output")
// queryTwo := search.NewTermQuery("exe").Field("file_extension")
query := generate(stringQuery)

// sfw._default.sfw_fts
result, err := db.Cluster.SearchQuery(
Expand Down
2 changes: 1 addition & 1 deletion internal/file/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (r repository) MetaUI(ctx context.Context, id string) (
func (r repository) Search(ctx context.Context, input FileSearchRequest) (FileSearchResponse, error) {

resp := FileSearchResponse{}
err := r.db.Search(ctx, &resp.Results, &resp.TotalHits)
err := r.db.Search(ctx, input.Query, &resp.Results, &resp.TotalHits)
if err != nil {
return resp, err
}
Expand Down

0 comments on commit c78e1ef

Please sign in to comment.