Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions search/query/query_string.y
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ tSTRING {
str := $1
logDebugGrammar("STRING - %s", str)
var q FieldableQuery
if strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
if len(str) > 1 && strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
q = NewRegexpQuery(str[1:len(str)-1])
} else if strings.ContainsAny(str, "*?"){
q = NewWildcardQuery(str)
Expand Down Expand Up @@ -153,7 +153,7 @@ fieldName tCOLON tSTRING {
str := $3
logDebugGrammar("FIELD - %s STRING - %s", field, str)
var q FieldableQuery
if strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
if len(str) > 1 && strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
q = NewRegexpQuery(str[1:len(str)-1])
} else if strings.ContainsAny(str, "*?"){
q = NewWildcardQuery(str)
Expand Down
4 changes: 2 additions & 2 deletions search/query/query_string.y.go

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

24 changes: 24 additions & 0 deletions search/query/query_string_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,30 @@ func TestQuerySyntaxParserValid(t *testing.T) {
},
nil),
},
{
input: `\/`,
mapping: mapping.NewIndexMapping(),
result: NewBooleanQueryForQueryString(
nil,
[]Query{
NewMatchQuery("/"),
},
nil),
},
{
input: `path:\/`,
mapping: mapping.NewIndexMapping(),
result: NewBooleanQueryForQueryString(
nil,
[]Query{
func() Query {
q := NewMatchQuery("/")
q.SetField("path")
return q
}(),
},
nil),
},
{
input: `mart*`,
mapping: mapping.NewIndexMapping(),
Expand Down
36 changes: 36 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,42 @@ func TestQueryStringEmptyConjunctionSearcher(t *testing.T) {
_, _ = index.Search(searchReq)
}

func TestQueryStringSearchEscapedSlashPath(t *testing.T) {
mapping := NewIndexMapping()
pathFieldMapping := NewTextFieldMapping()
pathFieldMapping.Analyzer = keyword.Name
mapping.DefaultMapping.AddFieldMappingsAt("path", pathFieldMapping)

index, err := NewMemOnly(mapping)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = index.Close()
}()

if err := index.Index("root", map[string]interface{}{"path": "/"}); err != nil {
t.Fatal(err)
}
if err := index.Index("tmp", map[string]interface{}{"path": "/tmp"}); err != nil {
t.Fatal(err)
}

query := NewQueryStringQuery(`path:\/`)
searchReq := NewSearchRequest(query)

res, err := index.Search(searchReq)
if err != nil {
t.Fatal(err)
}
if res.Total != 1 {
t.Fatalf("expected 1 hit for escaped slash path, got %d", res.Total)
}
if res.Hits[0].ID != "root" {
t.Fatalf("expected root hit, got %s", res.Hits[0].ID)
}
}

func TestDisjunctionQueryIncorrectMin(t *testing.T) {
tmpIndexPath := createTmpIndexPath(t)
defer cleanupTmpIndexPath(t, tmpIndexPath)
Expand Down
Loading