Skip to content
Merged
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
15 changes: 14 additions & 1 deletion redisearch/query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package redisearch

import (
"github.com/gomodule/redigo/redis"
"math"

"github.com/gomodule/redigo/redis"
)

// Flag is a type for query flags
Expand Down Expand Up @@ -82,6 +83,7 @@ type Query struct {

Filters []Filter
InKeys []string
InFields []string
ReturnFields []string
Language string
Expander string
Expand Down Expand Up @@ -152,6 +154,11 @@ func (q Query) serialize() redis.Args {
args = args.AddFlat(q.InKeys)
}

if q.InFields != nil {
args = args.Add("INFIELDS", len(q.InFields))
args = args.AddFlat(q.InFields)
}

if q.ReturnFields != nil {
args = args.Add("RETURN", len(q.ReturnFields))
args = args.AddFlat(q.ReturnFields)
Expand Down Expand Up @@ -267,6 +274,12 @@ func (q *Query) SetInKeys(keys ...string) *Query {
return q
}

// SetInFields sets the INFIELDS argument of the query - filter the results to ones appearing only in specific fields of the document
func (q *Query) SetInFields(fields ...string) *Query {
q.InFields = fields
return q
}

// SetSortBy sets the sorting key for the query
func (q *Query) SetSortBy(field string, ascending bool) *Query {
q.SortBy = &SortingKey{Field: field, Ascending: ascending}
Expand Down
3 changes: 3 additions & 0 deletions redisearch/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestQuery_serialize(t *testing.T) {
Raw string
Flags Flag
InKeys []string
InFields []string
ReturnFields []string
Language string
Expander string
Expand All @@ -89,6 +90,7 @@ func TestQuery_serialize(t *testing.T) {
{"QueryWithPayloads", fields{Raw: raw, Flags: QueryWithPayloads}, redis.Args{raw, "LIMIT", 0, 0, "WITHPAYLOADS"}},
{"QueryWithScores", fields{Raw: raw, Flags: QueryWithScores}, redis.Args{raw, "LIMIT", 0, 0, "WITHSCORES"}},
{"InKeys", fields{Raw: raw, InKeys: []string{"test_key"}}, redis.Args{raw, "LIMIT", 0, 0, "INKEYS", 1, "test_key"}},
{"InFields", fields{Raw: raw, InFields: []string{"test_key"}}, redis.Args{raw, "LIMIT", 0, 0, "INFIELDS", 1, "test_key"}},
{"ReturnFields", fields{Raw: raw, ReturnFields: []string{"test_field"}}, redis.Args{raw, "LIMIT", 0, 0, "RETURN", 1, "test_field"}},
{"Language", fields{Raw: raw, Language: "chinese"}, redis.Args{raw, "LIMIT", 0, 0, "LANGUAGE", "chinese"}},
{"Expander", fields{Raw: raw, Expander: "test_expander"}, redis.Args{raw, "LIMIT", 0, 0, "EXPANDER", "test_expander"}},
Expand All @@ -113,6 +115,7 @@ func TestQuery_serialize(t *testing.T) {
Raw: tt.fields.Raw,
Flags: tt.fields.Flags,
InKeys: tt.fields.InKeys,
InFields: tt.fields.InFields,
ReturnFields: tt.fields.ReturnFields,
Language: tt.fields.Language,
Expander: tt.fields.Expander,
Expand Down