Skip to content

Commit

Permalink
Merge pull request #2 from stevenferrer/feature/index-input-docs
Browse files Browse the repository at this point in the history
improved index JSON client API
  • Loading branch information
stevenferrer authored Jun 14, 2020
2 parents 24d6da6 + c46d99b commit b6d15ae
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 77 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/stevenferrer/solr-go
go 1.14

require (
github.com/davecgh/go-spew v1.1.0
github.com/dnaeon/go-vcr v1.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
Expand Down
20 changes: 10 additions & 10 deletions index/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

// Commander is a contract for an update command
type Commander interface {
// Command returns an update command string
// Command builds the command
Command() (string, error)
}

// AddCommand is an add command
type AddCommand struct {
// CommitWithin option to add the document within the
// specified number of milliseconds
// CommitWithin option to add the document within the specified
// number of milliseconds
CommitWithin int
// Overwrite indicates if the unique key constraints should be
// checked to overwrite previous versions of the same document
Expand Down Expand Up @@ -48,13 +48,13 @@ func (c AddCommand) Command() (string, error) {
return "\"add\"" + ":" + string(b), nil
}

// DelByQryCommand is a delete by query command
type DelByQryCommand struct {
// DeleteByQueryCommand is a delete-by-query command
type DeleteByQueryCommand struct {
Query string
}

// Command formats delete by query command
func (c DelByQryCommand) Command() (string, error) {
func (c DeleteByQueryCommand) Command() (string, error) {
cmd := map[string]interface{}{
"query": c.Query,
}
Expand All @@ -67,13 +67,13 @@ func (c DelByQryCommand) Command() (string, error) {
return "\"delete\"" + ":" + string(b), nil
}

// DelByIDsCommand is a delete by list of ids command
type DelByIDsCommand struct {
// DeleteByIDsCommand is a delete by list of ids command
type DeleteByIDsCommand struct {
IDs []string
}

// Command formats delete by ids command
func (c DelByIDsCommand) Command() (string, error) {
// Command builds the delete-by-ids command
func (c DeleteByIDsCommand) Command() (string, error) {
ids := []string{}
for _, id := range c.IDs {
ids = append(ids, fmt.Sprintf("%q", id))
Expand Down
4 changes: 2 additions & 2 deletions index/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestCommands(t *testing.T) {
})

t.Run("delete by query command", func(t *testing.T) {
delByQryCmds := []index.DelByQryCommand{
delByQryCmds := []index.DeleteByQueryCommand{
{
Query: "*:*",
},
Expand All @@ -72,7 +72,7 @@ func TestCommands(t *testing.T) {
})

t.Run("delete by ids command", func(t *testing.T) {
delByQryCmds := []index.DelByIDsCommand{
delByQryCmds := []index.DeleteByIDsCommand{
{
IDs: []string{"ID1", "ID2"},
},
Expand Down
30 changes: 30 additions & 0 deletions index/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package index

import (
"encoding/json"
)

// Docs contains the list of documents to be indexed
type Docs struct {
docs []interface{}
}

// NewDocs is a factory for *Docs
func NewDocs() *Docs {
return &Docs{docs: []interface{}{}}
}

// AddDoc adds a document to the list of documents
func (d *Docs) AddDoc(doc interface{}) {
d.docs = append(d.docs, doc)
}

// Count counts the number of documents
func (d *Docs) Count() int {
return len(d.docs)
}

// Marshal marshals the documents
func (d *Docs) Marshal() ([]byte, error) {
return json.Marshal(d.docs)
}
41 changes: 41 additions & 0 deletions index/docs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package index_test

import (
"testing"

"github.com/stevenferrer/solr-go/index"
"github.com/stretchr/testify/assert"
)

func TestDocs(t *testing.T) {
var names = []struct {
ID string `json:"id"`
Name string `json:"name"`
}{
{
ID: "1",
Name: "Milana Vino",
},
{
ID: "2",
Name: "Charly Jordan",
},
{
ID: "3",
Name: "Daisy Keech",
},
}

docs := index.NewDocs()
for _, model := range names {
docs.AddDoc(model)
}

assert.Equal(t, docs.Count(), 3)

b, err := docs.Marshal()
assert.NoError(t, err)

expected := `[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy Keech"}]`
assert.Equal(t, expected, string(b))
}
15 changes: 10 additions & 5 deletions index/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
collection := "gettingstarted"

// Indexing multiple documents
var docs = []struct {
var names = []struct {
ID string `json:"id"`
Name string `json:"name"`
}{
Expand All @@ -36,14 +36,19 @@ func main() {
},
}

docs := index.NewDocs()
for _, name := range names {
docs.AddDoc(name)
}

ctx := context.Background()
err := indexClient.AddDocs(ctx, collection, docs)
err := indexClient.AddDocuments(ctx, collection, docs)
checkErr(err)
err = indexClient.Commit(ctx, collection)
checkErr(err)

// Sending multiple update commands
err = indexClient.UpdateCommands(context.Background(), collection,
err = indexClient.SendCommands(context.Background(), collection,
index.AddCommand{
CommitWithin: 5000,
Overwrite: true,
Expand All @@ -65,10 +70,10 @@ func main() {
"name": "Charly Jordan",
},
},
index.DelByIDsCommand{
index.DeleteByIDsCommand{
IDs: []string{"2"},
},
index.DelByQryCommand{
index.DeleteByQueryCommand{
Query: "*:*",
},
)
Expand Down
23 changes: 9 additions & 14 deletions index/fixtures/add-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
version: 1
interactions:
- request:
body: '[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy
Keech"}]'
body: '[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy Keech"}]'
form: {}
headers:
Content-Type:
Expand All @@ -15,14 +14,12 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":137}}
"QTime":96}}
headers:
Content-Length:
- "57"
- "56"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
X-Content-Type-Options:
Expand All @@ -47,24 +44,22 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":1214}}
"QTime":691}}
headers:
Cache-Control:
- no-cache, no-store
Content-Length:
- "58"
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
Etag:
- '"172ad393043"'
- '"172b15912bf"'
Expires:
- Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified:
- Sat, 13 Jun 2020 10:28:25 GMT
- Sun, 14 Jun 2020 05:41:44 GMT
Pragma:
- no-cache
X-Content-Type-Options:
Expand Down
14 changes: 5 additions & 9 deletions index/fixtures/init-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":1337}}
"QTime":570}}
headers:
Content-Length:
- "58"
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
X-Content-Type-Options:
Expand All @@ -46,14 +44,12 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":683}}
"QTime":498}}
headers:
Content-Length:
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
X-Content-Type-Options:
Expand Down
19 changes: 7 additions & 12 deletions index/fixtures/update-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
version: 1
interactions:
- request:
body: '{"add":{"commitWithin":5000,"doc":{"id":"1","name":"Milana Vino"},"overwrite":true},"add":{"doc":{"id":"2","name":"Daisy
Keech"}},"add":{"doc":{"id":"3","name":"Charley Jordan"}},"delete":["2"],"delete":{"query":"*:*"}}'
body: '{"add":{"commitWithin":5000,"doc":{"id":"1","name":"Milana Vino"},"overwrite":true},"add":{"doc":{"id":"2","name":"Daisy Keech"}},"add":{"doc":{"id":"3","name":"Charley Jordan"}},"delete":["2"],"delete":{"query":"*:*"}}'
form: {}
headers:
Content-Type:
Expand All @@ -15,14 +14,12 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":200}}
"QTime":246}}
headers:
Content-Length:
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
X-Content-Type-Options:
Expand All @@ -47,24 +44,22 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":572}}
"QTime":159}}
headers:
Cache-Control:
- no-cache, no-store
Content-Length:
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
Etag:
- '"172ad393350"'
- '"172b159145e"'
Expires:
- Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified:
- Sat, 13 Jun 2020 10:28:26 GMT
- Sun, 14 Jun 2020 05:41:44 GMT
Pragma:
- no-cache
X-Content-Type-Options:
Expand Down
Loading

0 comments on commit b6d15ae

Please sign in to comment.