Skip to content

Commit

Permalink
Add index_type JSON (#111)
Browse files Browse the repository at this point in the history
Co-authored-by: Guy Korland <[email protected]>
Co-authored-by: filipe oliveira <[email protected]>
  • Loading branch information
3 people authored Jun 9, 2021
1 parent 07c4e29 commit 8c86ddb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 33 deletions.
43 changes: 11 additions & 32 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,33 @@ jobs:
- run:
name: Copy RediSearch
command: |
docker run --rm --entrypoint cat redislabs/redisearch:edge /usr/lib/redis/modules/redisearch.so > redisearch.so
docker run --rm --entrypoint cat redislabs/redismod:edge /usr/lib/redis/modules/redisearch.so > redisearch.so
chmod 755 redisearch.so
docker run --rm --entrypoint cat redislabs/redismod:edge /usr/lib/redis/modules/rejson.so > rejson.so
chmod 755 rejson.so
- run:
name: Run RediSearch with tls support
command: |
docker run -d -v $(pwd)/redisearch.so:/data/redisearch.so \
-v $(pwd)/rejson.so:/data/rejson.so \
-v $(pwd)/redis/tests/tls/:/data \
-p 6379:6379 redis redis-server --tls-port 6379 --port 0 \
--tls-cert-file /data/redis.crt \
--tls-key-file /data/redis.key \
--tls-ca-cert-file /data/ca.crt \
--tls-auth-clients no --loadmodule /data/redisearch.so
--tls-auth-clients no --loadmodule /data/redisearch.so --loadmodule /data/rejson.so
- run:
name: Run Examples
command: |
make examples TLS_CERT=redis/tests/tls/redis.crt \
TLS_KEY=redis/tests/tls/redis.key \
TLS_CACERT=redis/tests/tls/ca.crt
build: # test with redisearch:edge
build: # test with redismod:latest
docker:
- image: circleci/golang:1.12
- image: redislabs/redisearch:edge
- image: redislabs/redismod:latest

working_directory: /go/src/github.com/RediSearch/redisearch-go
steps:
Expand All @@ -58,30 +62,10 @@ jobs:
- run: make coverage
- run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}

build-latest:
docker:
- image: circleci/golang:1.12
- image: redislabs/redisearch:latest

working_directory: /go/src/github.com/RediSearch/redisearch-go
steps:
- checkout
- run: make test

build-v16:
docker:
- image: circleci/golang:1.12
- image: redislabs/redisearch:1.6.13

working_directory: /go/src/github.com/RediSearch/redisearch-go
steps:
- checkout
- run: make test

build-v14:
docker:
- image: circleci/golang:1.12
- image: redislabs/redisearch:1.4.28
- image: redislabs/redisearch:1.6.15

working_directory: /go/src/github.com/RediSearch/redisearch-go
steps:
Expand All @@ -91,7 +75,7 @@ jobs:
build_nightly: # test nightly with redisearch:edge
docker:
- image: circleci/golang:1.12
- image: redislabs/redisearch:edge
- image: redislabs/redismod:edge

working_directory: /go/src/github.com/RediSearch/redisearch-go
steps:
Expand All @@ -102,11 +86,9 @@ workflows:
version: 2
commit:
jobs:
- build
- build-tls
- build-latest
- build
- build-v16
- build-v14
nightly:
triggers:
- schedule:
Expand All @@ -118,6 +100,3 @@ workflows:
jobs:
- build_nightly
- build-tls
- build-latest
- build-v16
- build-v14
37 changes: 37 additions & 0 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,43 @@ func TestClient_GetRediSearchVersion(t *testing.T) {
assert.Nil(t, err)
}

func TestClient_CreateIndexWithIndexDefinitionJSON(t *testing.T) {
c := createClient("index-definition-test")
version, err := c.getRediSearchVersion()
assert.Nil(t, err)
if version <= 20200 {
// JSON IndexDefinition is available for RediSearch 2.2+
return
}
// Create a schema
sc := NewSchema(DefaultOptions).
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true})).
AddField(NewTextFieldOptions("description", TextFieldOptions{Weight: 5.0, Sortable: true})).
AddField(NewNumericField("price"))

type args struct {
schema *Schema
definition *IndexDefinition
}
tests := []struct {
name string
args args
wantErr bool
}{
{"default+index_on", args{sc, NewIndexDefinition().SetIndexOn(JSON)}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c.Drop()
if err := c.CreateIndexWithIndexDefinition(tt.args.schema, tt.args.definition); (err != nil) != tt.wantErr {
t.Errorf("CreateIndexWithIndexDefinition() error = %v, wantErr %v", err, tt.wantErr)
}
})
teardown(c)

}
}

func TestClient_CreateIndexWithIndexDefinition(t *testing.T) {
i := createClient("index-definition-test")
version, err := i.getRediSearchVersion()
Expand Down
21 changes: 20 additions & 1 deletion redisearch/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import (
"github.com/gomodule/redigo/redis"
)

// IndexType - Enum of existing index types
type IndexType int

const (
HASH IndexType = iota
JSON
)

func (it IndexType) String() string {
return [...]string{"HASH", "JSON"}[it]
}

// IndexInfo - Structure showing information about an existing index
type IndexInfo struct {
Schema Schema
Expand Down Expand Up @@ -42,7 +54,14 @@ type IndexDefinition struct {
// This is only valid for >= RediSearch 2.0
func NewIndexDefinition() *IndexDefinition {
prefixArray := make([]string, 0)
return &IndexDefinition{"HASH", false, prefixArray, "", "", "", -1, "", ""}
return &IndexDefinition{HASH.String(), false, prefixArray, "", "", "", -1, "", ""}
}

// This is only valid for >= RediSearch 2.2
func (defintion *IndexDefinition) SetIndexOn(value IndexType) (outDef *IndexDefinition) {
outDef = defintion
outDef.IndexOn = value.String()
return
}

// This is only valid for >= RediSearch 2.0
Expand Down
1 change: 1 addition & 0 deletions redisearch/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestIndexDefinition_Serialize(t *testing.T) {
want redis.Args
}{
{"default", fields{NewIndexDefinition()}, args{redis.Args{}}, redis.Args{"ON", "HASH"}},
{"default", fields{NewIndexDefinition().SetIndexOn(JSON)}, args{redis.Args{}}, redis.Args{"ON", "JSON"}},
{"default+async", fields{NewIndexDefinition().SetAsync(true)}, args{redis.Args{}}, redis.Args{"ON", "HASH", "ASYNC"}},
{"default+score", fields{NewIndexDefinition().SetScore(0.75)}, args{redis.Args{}}, redis.Args{"ON", "HASH", "SCORE", 0.75}},
{"default+score_field", fields{NewIndexDefinition().SetScoreField("myscore")}, args{redis.Args{}}, redis.Args{"ON", "HASH", "SCORE_FIELD", "myscore"}},
Expand Down

0 comments on commit 8c86ddb

Please sign in to comment.