diff --git a/.circleci/config.yml b/.circleci/config.yml index edae4f3..878c9b2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,16 +3,16 @@ # Check https://circleci.com/docs/2.0/language-go/ for more details version: 2 jobs: - build: # test with redisearch:latest + build: # test with redisearch:edge docker: - image: circleci/golang:1.9 - - image: redislabs/redisearch:latest + - image: redislabs/redisearch:edge working_directory: /go/src/github.com/RediSearch/redisearch-go steps: - checkout - - run: go get -v -t -d ./... - - run: go test -v ./... -race -coverprofile=coverage.txt -covermode=atomic + - run: make get + - run: make coverage - run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN} build_nightly: # test nightly with redisearch:edge diff --git a/.gitignore b/.gitignore index 499f048..b8bc9d2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ !.gitignore !.circleci/config.yml !/tests/*.bz2 +!Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6727596 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +# Go parameters +GOCMD=go +GOBUILD=$(GOCMD) build +GOINSTALL=$(GOCMD) install +GOCLEAN=$(GOCMD) clean +GOTEST=$(GOCMD) test +GOGET=$(GOCMD) get +GOMOD=$(GOCMD) mod + +.PHONY: all test coverage +all: test coverage + +get: + $(GOGET) -t -v ./... + +examples: get + $(GOBUILD) ./examples/quickstart/. + ./quickstart > /dev/null + +test: get examples + $(GOTEST) -race -covermode=atomic ./... + +coverage: get test + $(GOTEST) -race -coverprofile=coverage.txt -covermode=atomic ./redisearch + diff --git a/examples/quickstart/main.go b/examples/quickstart/main.go new file mode 100644 index 0000000..2569179 --- /dev/null +++ b/examples/quickstart/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "github.com/RediSearch/redisearch-go/redisearch" + "log" + "time" +) + +/** + * This demo should be updated in RediSearch.io if changed + * Update at: https://github.com/RediSearch/RediSearch/blob/master/docs/go_client.md + */ +func main() { + // Create a client. By default a client is schemaless + // unless a schema is provided when creating the index + c := redisearch.NewClient("localhost:6379", "myIndex") + + // Create a schema + sc := redisearch.NewSchema(redisearch.DefaultOptions). + AddField(redisearch.NewTextField("body")). + AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})). + AddField(redisearch.NewNumericField("date")) + + // Drop an existing index. If the index does not exist an error is returned + c.Drop() + + // Create the index with the given schema + if err := c.CreateIndex(sc); err != nil { + log.Fatal(err) + } + + // Create a document with an id and given score + doc := redisearch.NewDocument("doc1", 1.0) + doc.Set("title", "Hello world"). + Set("body", "foo bar"). + Set("date", time.Now().Unix()) + + // Index the document. The API accepts multiple documents at a time + if err := c.IndexOptions(redisearch.DefaultIndexingOptions, doc); err != nil { + log.Fatal(err) + } + + // Searching with limit and sorting + docs, total, err := c.Search(redisearch.NewQuery("hello world"). + Limit(0, 2). + SetReturnFields("title")) + + fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err) + // Output: doc1 Hello world 1 +}