Skip to content

Commit

Permalink
fix search tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Sep 16, 2022
1 parent 208b77c commit 3427db3
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 152 deletions.
18 changes: 10 additions & 8 deletions services/search/pkg/engine/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func BuildBleveMapping() (mapping.IndexMapping, error) {
// Search executes a search request operation within the index.
// Returns a SearchIndexResponse object or an error.
func (b *Bleve) Search(_ context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) {
fmt.Println(b.buildQuery(sir.Query))
q := bleve.NewConjunctionQuery(
&query.QueryStringQuery{
Query: b.buildQuery(sir.Query),
Expand Down Expand Up @@ -322,30 +323,31 @@ func (b *Bleve) setDeleted(id string, deleted bool) error {
return nil
}

func (b *Bleve) buildQuery(si string) (so string) {
func (b *Bleve) buildQuery(si string) string {
var queries [][]string
var so []string
lexer := sq.NewLexer(strings.NewReader(si))
allowedFields := []string{"content", "title", "tags"}

for {
tok, lit := lexer.Scan()
if tok == sq.T_FIELD {
if tok == sq.TField {
for _, field := range allowedFields {
if strings.ToLower(field) == strings.ToLower(lit) {
if strings.EqualFold(field, lit) {
queries = append(queries, []string{cases.Title(language.Und, cases.NoLower).String(lit)})
}
}
}

if tok == sq.T_VALUE {
if tok == sq.TValue {
if len(queries) == 0 {
queries = append(queries, []string{"*"})
}

queries[len(queries)-1] = append(queries[len(queries)-1], lit)
}

if tok == sq.T_EOF {
if tok == sq.TEof {
break
}
}
Expand All @@ -362,15 +364,15 @@ func (b *Bleve) buildQuery(si string) (so string) {
}

for _, field := range fields {
ss := strings.ToLower(strings.Join(q[1:], " "))
ss := strings.ToLower(strings.Join(q[1:], `\ `))

if !strings.Contains(ss, "*") {
ss = "*" + ss + "*"
}

so += fmt.Sprintf("%s:%s ", field, ss)
so = append(so, fmt.Sprintf("%s:%s", field, ss))
}
}

return
return strings.Join(so, " ")
}
2 changes: 1 addition & 1 deletion services/search/pkg/engine/bleve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var _ = Describe("Bleve", func() {
r := createEntity(rid, content.Document{Name: "Foo oo.pdf"})
err := eng.Upsert(r.ID, r)
Expect(err).ToNot(HaveOccurred())
assertDocCount(rid, `Name:foo\ o*`, 1)
assertDocCount(rid, `Name:foo o*`, 1)
})

It("finds files by digits in the filename", func() {
Expand Down
14 changes: 7 additions & 7 deletions services/search/pkg/query/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ func (l *Lexer) Scan() (Token, string) {
r := l.read()

if r == eof {
return T_EOF, ""
return TEof, ""
}

if unicode.IsSpace(r) {
continue
}

if r == '"' {
return T_QUOTATION_MARK, ""
return TQuotationMark, ""
}

if r == '-' {
return T_NEGATION, ""
return TNegation, ""
}

if r != ':' {
l.unread()
return l.scanUnknown(T_VALUE)
return l.scanUnknown(TValue)
}

if r == ':' && (unicode.IsLetter(l.peek(1)) || unicode.IsNumber(l.peek(1))) {
return l.scanUnknown(T_SHORTCUT)
return l.scanUnknown(TShortcut)
}

return T_UNKNOWN, string(r)
return TUnknown, string(r)
}
}

Expand All @@ -69,7 +69,7 @@ func (l *Lexer) scanUnknown(t Token) (Token, string) {
}

if r == ':' {
return T_FIELD, buf.String()
return TField, buf.String()
}

buf.WriteRune(r)
Expand Down
Loading

0 comments on commit 3427db3

Please sign in to comment.