Skip to content

Commit

Permalink
Search by prefix. Fix escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored and m-kus committed Dec 3, 2020
1 parent 190fd21 commit a734eab
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions internal/elastic/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

var ptrRegEx = regexp.MustCompile(`^ptr:\d+$`)
var sanitizeRegEx = regexp.MustCompile(`[\+\-\=\&\|\>\<\!\(\)\{\}\[\]\^\"\~\*\?\:\\\/]`)
var sanitizeRegEx = regexp.MustCompile(`[\:]`)

type searchContext struct {
Text string
Expand Down Expand Up @@ -265,33 +265,27 @@ func parseSearchGroupingResponse(response searchByTextResponse, offset int64) ([
func prepare(search string, filters map[string]interface{}, fields []string) (searchContext, error) {
ctx := newSearchContext()

needEscape := true
if ptrRegEx.MatchString(search) {
ctx.Text = strings.TrimPrefix(search, "ptr:")
ctx.Indices = []string{DocBigMapDiff}
ctx.Fields = []string{"ptr"}
needEscape = false
} else {
ctx.Text = sanitizeRegEx.ReplaceAllString(search, "\\${1}")

internalFields, usingIndices, highlights, err := getFields(ctx.Text, filters, fields)
if err != nil {
return ctx, err
}
ctx.Indices = usingIndices
ctx.Highlights = highlights
ctx.Fields = internalFields
}

if needEscape {
ctx.Text = fmt.Sprintf("*%s*", ctx.Text)
ctx.Text = fmt.Sprintf("%s*", search)
}

filterString, err := prepareSearchFilters(filters)
if err != nil {
return ctx, err
}
if filterString != "" {
ctx.Text = sanitizeRegEx.ReplaceAllString(ctx.Text, "\\${0}")
ctx.Text = fmt.Sprintf("%s AND %s", filterString, ctx.Text)
}
return ctx, nil
Expand Down

0 comments on commit a734eab

Please sign in to comment.