Skip to content

Commit b6d15ae

Browse files
authored
Merge pull request #2 from stevenferrer/feature/index-input-docs
improved index JSON client API
2 parents 24d6da6 + c46d99b commit b6d15ae

File tree

11 files changed

+162
-77
lines changed

11 files changed

+162
-77
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/stevenferrer/solr-go
33
go 1.14
44

55
require (
6+
github.com/davecgh/go-spew v1.1.0
67
github.com/dnaeon/go-vcr v1.0.1
78
github.com/pkg/errors v0.9.1
89
github.com/stretchr/testify v1.6.1

index/commands.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010

1111
// Commander is a contract for an update command
1212
type Commander interface {
13-
// Command returns an update command string
13+
// Command builds the command
1414
Command() (string, error)
1515
}
1616

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

51-
// DelByQryCommand is a delete by query command
52-
type DelByQryCommand struct {
51+
// DeleteByQueryCommand is a delete-by-query command
52+
type DeleteByQueryCommand struct {
5353
Query string
5454
}
5555

5656
// Command formats delete by query command
57-
func (c DelByQryCommand) Command() (string, error) {
57+
func (c DeleteByQueryCommand) Command() (string, error) {
5858
cmd := map[string]interface{}{
5959
"query": c.Query,
6060
}
@@ -67,13 +67,13 @@ func (c DelByQryCommand) Command() (string, error) {
6767
return "\"delete\"" + ":" + string(b), nil
6868
}
6969

70-
// DelByIDsCommand is a delete by list of ids command
71-
type DelByIDsCommand struct {
70+
// DeleteByIDsCommand is a delete by list of ids command
71+
type DeleteByIDsCommand struct {
7272
IDs []string
7373
}
7474

75-
// Command formats delete by ids command
76-
func (c DelByIDsCommand) Command() (string, error) {
75+
// Command builds the delete-by-ids command
76+
func (c DeleteByIDsCommand) Command() (string, error) {
7777
ids := []string{}
7878
for _, id := range c.IDs {
7979
ids = append(ids, fmt.Sprintf("%q", id))

index/commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestCommands(t *testing.T) {
5050
})
5151

5252
t.Run("delete by query command", func(t *testing.T) {
53-
delByQryCmds := []index.DelByQryCommand{
53+
delByQryCmds := []index.DeleteByQueryCommand{
5454
{
5555
Query: "*:*",
5656
},
@@ -72,7 +72,7 @@ func TestCommands(t *testing.T) {
7272
})
7373

7474
t.Run("delete by ids command", func(t *testing.T) {
75-
delByQryCmds := []index.DelByIDsCommand{
75+
delByQryCmds := []index.DeleteByIDsCommand{
7676
{
7777
IDs: []string{"ID1", "ID2"},
7878
},

index/docs.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package index
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
// Docs contains the list of documents to be indexed
8+
type Docs struct {
9+
docs []interface{}
10+
}
11+
12+
// NewDocs is a factory for *Docs
13+
func NewDocs() *Docs {
14+
return &Docs{docs: []interface{}{}}
15+
}
16+
17+
// AddDoc adds a document to the list of documents
18+
func (d *Docs) AddDoc(doc interface{}) {
19+
d.docs = append(d.docs, doc)
20+
}
21+
22+
// Count counts the number of documents
23+
func (d *Docs) Count() int {
24+
return len(d.docs)
25+
}
26+
27+
// Marshal marshals the documents
28+
func (d *Docs) Marshal() ([]byte, error) {
29+
return json.Marshal(d.docs)
30+
}

index/docs_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package index_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stevenferrer/solr-go/index"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestDocs(t *testing.T) {
11+
var names = []struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
}{
15+
{
16+
ID: "1",
17+
Name: "Milana Vino",
18+
},
19+
{
20+
ID: "2",
21+
Name: "Charly Jordan",
22+
},
23+
{
24+
ID: "3",
25+
Name: "Daisy Keech",
26+
},
27+
}
28+
29+
docs := index.NewDocs()
30+
for _, model := range names {
31+
docs.AddDoc(model)
32+
}
33+
34+
assert.Equal(t, docs.Count(), 3)
35+
36+
b, err := docs.Marshal()
37+
assert.NoError(t, err)
38+
39+
expected := `[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy Keech"}]`
40+
assert.Equal(t, expected, string(b))
41+
}

index/examples/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818
collection := "gettingstarted"
1919

2020
// Indexing multiple documents
21-
var docs = []struct {
21+
var names = []struct {
2222
ID string `json:"id"`
2323
Name string `json:"name"`
2424
}{
@@ -36,14 +36,19 @@ func main() {
3636
},
3737
}
3838

39+
docs := index.NewDocs()
40+
for _, name := range names {
41+
docs.AddDoc(name)
42+
}
43+
3944
ctx := context.Background()
40-
err := indexClient.AddDocs(ctx, collection, docs)
45+
err := indexClient.AddDocuments(ctx, collection, docs)
4146
checkErr(err)
4247
err = indexClient.Commit(ctx, collection)
4348
checkErr(err)
4449

4550
// Sending multiple update commands
46-
err = indexClient.UpdateCommands(context.Background(), collection,
51+
err = indexClient.SendCommands(context.Background(), collection,
4752
index.AddCommand{
4853
CommitWithin: 5000,
4954
Overwrite: true,
@@ -65,10 +70,10 @@ func main() {
6570
"name": "Charly Jordan",
6671
},
6772
},
68-
index.DelByIDsCommand{
73+
index.DeleteByIDsCommand{
6974
IDs: []string{"2"},
7075
},
71-
index.DelByQryCommand{
76+
index.DeleteByQueryCommand{
7277
Query: "*:*",
7378
},
7479
)

index/fixtures/add-docs.yaml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
version: 1
33
interactions:
44
- request:
5-
body: '[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy
6-
Keech"}]'
5+
body: '[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy Keech"}]'
76
form: {}
87
headers:
98
Content-Type:
@@ -15,14 +14,12 @@ interactions:
1514
{
1615
"responseHeader":{
1716
"status":0,
18-
"QTime":137}}
17+
"QTime":96}}
1918
headers:
2019
Content-Length:
21-
- "57"
20+
- "56"
2221
Content-Security-Policy:
23-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
24-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
25-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
22+
- 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';
2623
Content-Type:
2724
- text/plain;charset=utf-8
2825
X-Content-Type-Options:
@@ -47,24 +44,22 @@ interactions:
4744
{
4845
"responseHeader":{
4946
"status":0,
50-
"QTime":1214}}
47+
"QTime":691}}
5148
headers:
5249
Cache-Control:
5350
- no-cache, no-store
5451
Content-Length:
55-
- "58"
52+
- "57"
5653
Content-Security-Policy:
57-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
58-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
59-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
54+
- 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';
6055
Content-Type:
6156
- text/plain;charset=utf-8
6257
Etag:
63-
- '"172ad393043"'
58+
- '"172b15912bf"'
6459
Expires:
6560
- Sat, 01 Jan 2000 01:00:00 GMT
6661
Last-Modified:
67-
- Sat, 13 Jun 2020 10:28:25 GMT
62+
- Sun, 14 Jun 2020 05:41:44 GMT
6863
Pragma:
6964
- no-cache
7065
X-Content-Type-Options:

index/fixtures/init-schema.yaml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ interactions:
1414
{
1515
"responseHeader":{
1616
"status":0,
17-
"QTime":1337}}
17+
"QTime":570}}
1818
headers:
1919
Content-Length:
20-
- "58"
20+
- "57"
2121
Content-Security-Policy:
22-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
23-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
24-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
22+
- 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';
2523
Content-Type:
2624
- text/plain;charset=utf-8
2725
X-Content-Type-Options:
@@ -46,14 +44,12 @@ interactions:
4644
{
4745
"responseHeader":{
4846
"status":0,
49-
"QTime":683}}
47+
"QTime":498}}
5048
headers:
5149
Content-Length:
5250
- "57"
5351
Content-Security-Policy:
54-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
55-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
56-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
52+
- 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';
5753
Content-Type:
5854
- text/plain;charset=utf-8
5955
X-Content-Type-Options:

index/fixtures/update-commands.yaml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
version: 1
33
interactions:
44
- request:
5-
body: '{"add":{"commitWithin":5000,"doc":{"id":"1","name":"Milana Vino"},"overwrite":true},"add":{"doc":{"id":"2","name":"Daisy
6-
Keech"}},"add":{"doc":{"id":"3","name":"Charley Jordan"}},"delete":["2"],"delete":{"query":"*:*"}}'
5+
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":"*:*"}}'
76
form: {}
87
headers:
98
Content-Type:
@@ -15,14 +14,12 @@ interactions:
1514
{
1615
"responseHeader":{
1716
"status":0,
18-
"QTime":200}}
17+
"QTime":246}}
1918
headers:
2019
Content-Length:
2120
- "57"
2221
Content-Security-Policy:
23-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
24-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
25-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
22+
- 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';
2623
Content-Type:
2724
- text/plain;charset=utf-8
2825
X-Content-Type-Options:
@@ -47,24 +44,22 @@ interactions:
4744
{
4845
"responseHeader":{
4946
"status":0,
50-
"QTime":572}}
47+
"QTime":159}}
5148
headers:
5249
Cache-Control:
5350
- no-cache, no-store
5451
Content-Length:
5552
- "57"
5653
Content-Security-Policy:
57-
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
58-
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
59-
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
54+
- 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';
6055
Content-Type:
6156
- text/plain;charset=utf-8
6257
Etag:
63-
- '"172ad393350"'
58+
- '"172b159145e"'
6459
Expires:
6560
- Sat, 01 Jan 2000 01:00:00 GMT
6661
Last-Modified:
67-
- Sat, 13 Jun 2020 10:28:26 GMT
62+
- Sun, 14 Jun 2020 05:41:44 GMT
6863
Pragma:
6964
- no-cache
7065
X-Content-Type-Options:

0 commit comments

Comments
 (0)