Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from PumpkinSeed/feature/embedded-fields-RW
Browse files Browse the repository at this point in the history
Feature/embedded fields rw
  • Loading branch information
PumpkinSeed committed Sep 26, 2019
2 parents 4980fe4 + e57f464 commit 95d74df
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 187 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
vendor
go.sum
go.sum
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
PKG_TEST=testing go test ./...
32 changes: 1 addition & 31 deletions fts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const (
var (
ErrEmptyField = errors.New("field must be filled")
ErrEmptyIndex = errors.New("index must be filled")

placeholderBucket *gocb.Bucket
placeholderCluster *gocb.Cluster
)

type SearchQuery struct {
Expand Down Expand Up @@ -76,32 +73,7 @@ type RangeQuery struct {
Offset int `json:"-"`
}

func placeholderInit() {
if placeholderBucket == nil {
var err error
placeholderCluster, err = gocb.Connect("couchbase://localhost")
if err != nil {
panic(err)
}

err = placeholderCluster.Authenticate(gocb.PasswordAuthenticator{
Username: "Administrator",
Password: "password",
})
if err != nil {
panic(err)
}

placeholderBucket, err = placeholderCluster.OpenBucket("company", "")
if err != nil {
panic(err)
}
}
}

func (h *Handler) SimpleSearch(index string, q *SearchQuery) ([]gocb.SearchResultHit, error) {
placeholderInit()

if err := q.Setup(); err != nil {
return nil, err
}
Expand All @@ -116,8 +88,6 @@ func (h *Handler) SimpleSearch(index string, q *SearchQuery) ([]gocb.SearchResul
}

func (h *Handler) SimpleSearchWithFacets(index string, q *SearchQuery, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
placeholderInit()

if err := q.Setup(); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -191,7 +161,7 @@ func (h *Handler) RangeSearchWithFacets(index string, q *RangeQuery, facets []Fa
}

func (h *Handler) doSearch(query *gocb.SearchQuery) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
res, err := placeholderBucket.ExecuteSearchQuery(query)
res, err := h.state.bucket.ExecuteSearchQuery(query)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions fts_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (h *Handler) CreateFullTextSearchIndex(def *IndexDefinition) error {
if err != nil {
return err
}
req, _ := http.NewRequest("PUT", h.fullTestSearchURL(def.Name), bytes.NewBuffer(body))
req, _ := http.NewRequest("PUT", h.fullTextSearchURL(def.Name), bytes.NewBuffer(body))
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
Expand All @@ -181,7 +181,7 @@ func (h *Handler) CreateFullTextSearchIndex(def *IndexDefinition) error {
}

func (h *Handler) DeleteFullTextSearchIndex(indexName string) error {
req, _ := http.NewRequest("DELETE", h.fullTestSearchURL(indexName), nil)
req, _ := http.NewRequest("DELETE", h.fullTextSearchURL(indexName), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")

Expand Down Expand Up @@ -209,7 +209,7 @@ func (h *Handler) DeleteFullTextSearchIndex(indexName string) error {
}

func (h *Handler) InspectFullTextSearchIndex(indexName string) (bool, *IndexDefinition, error) {
req, _ := http.NewRequest("GET", h.fullTestSearchURL(""), nil)
req, _ := http.NewRequest("GET", h.fullTextSearchURL(""), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")

Expand All @@ -234,7 +234,7 @@ func (h *Handler) InspectFullTextSearchIndex(indexName string) (bool, *IndexDefi
return false, nil, nil
}

func (h *Handler) fullTestSearchURL(indexName string) string {
func (h *Handler) fullTextSearchURL(indexName string) string {
if indexName == "" {
return fmt.Sprintf("%s%s", h.httpAddress, ftsEndpoint)
}
Expand Down
7 changes: 3 additions & 4 deletions fts_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import "testing"
func TestCreateFullTextSearchIndex(t *testing.T) {
indexName := "order_fts_idx"

h, _ := New(&Configuration{})
if ok, _, _ := h.InspectFullTextSearchIndex(indexName); ok {
err := h.DeleteFullTextSearchIndex(indexName)
if ok, _, _ := th.InspectFullTextSearchIndex(indexName); ok {
err := th.DeleteFullTextSearchIndex(indexName)
if err != nil {
t.Fatal(err)
}
Expand All @@ -22,7 +21,7 @@ func TestCreateFullTextSearchIndex(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = h.CreateFullTextSearchIndex(def)
err = th.CreateFullTextSearchIndex(def)
if err != nil {
t.Fatal(err)
}
Expand Down
24 changes: 6 additions & 18 deletions fts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import (
"fmt"
"testing"
"time"

"github.com/brianvoe/gofakeit"
)

func init() {
gofakeit.Seed(time.Now().UnixNano())
}

func TestSearchQuery(t *testing.T) {
sq := SearchQuery{
Query: "card",
Expand Down Expand Up @@ -80,20 +74,17 @@ func TestRangeQuery(t *testing.T) {
}

func TestSimpleSearchMatch(t *testing.T) {
placeholderInit()

for i := 0; i < 10; i++ {
order := newTestStruct1()
_, err := placeholderBucket.Insert("order::"+order.Token, order, 0)
order := generate()
_, err := th.state.bucket.Insert("order::"+order.Token, order, 0)
if err != nil {
t.Fatal(err)
}
}

handler, _ := New(&Configuration{})
searchMatch := "Talia"
mes := time.Now()
_, err := handler.SimpleSearch("order_fts_idx", &SearchQuery{
_, err := th.SimpleSearch("order_fts_idx", &SearchQuery{
Query: searchMatch,
//Field: "CardHolderName",
})
Expand All @@ -104,20 +95,17 @@ func TestSimpleSearchMatch(t *testing.T) {
}

func TestSimpleSearchMatchWithFacet(t *testing.T) {
placeholderInit()

for i := 0; i < 10; i++ {
order := newTestStruct1()
_, err := placeholderBucket.Insert("order::"+order.Token, order, 0)
order := generate()
_, err := th.state.bucket.Insert("order::"+order.Token, order, 0)
if err != nil {
t.Fatal(err)
}
}

handler, _ := New(&Configuration{})
searchMatch := "Talia"
mes := time.Now()
_, _, err := handler.SimpleSearchWithFacets(
_, _, err := th.SimpleSearchWithFacets(
"order_fts_idx",
&SearchQuery{
Query: searchMatch,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/PumpkinSeed/odatas

go 1.13
go 1.12

require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func New(c *Configuration) (*Handler, error) {
httpAddress: "http://localhost:8091",
username: c.Username,
password: c.Password,
state: s,
state: s,
}, nil
}

Expand Down
45 changes: 9 additions & 36 deletions indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,14 @@ import (
"github.com/couchbase/gocb"
)

const (
bucketName = "company"
)

var h *Handler

func init() {
h, _ = New(&Configuration{
Username: "Administrator",
Password: "password",
BucketName: bucketName,
BucketPassword: "",
})

start := time.Now()
if err := h.GetManager().Flush(); err != nil {
fmt.Printf("Turn on flush in bucket: %+v\n", err)
}
fmt.Printf("Bucket flushed: %v\n", time.Since(start))

for j := 0; j < 10000; j++ {
instance := newTestStruct1()
_, _ = h.state.bucket.Insert(instance.Token, instance, 0)
}
fmt.Printf("Connection setup, data seeded %v\n", time.Since(start))
}

func TestIndexCreate(t *testing.T) {
instance := testStructEmbedded{}
instance := webshop{}

if err := h.Index(instance); err != nil {
if err := th.Index(instance); err != nil {
t.Fatal(err)
}

indexes, err := h.GetManager().GetIndexes()
indexes, err := th.GetManager().GetIndexes()
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,7 +30,7 @@ func TestIndexCreate(t *testing.T) {
}

func TestSearchWithIndex(t *testing.T) {
if err := h.Index(testStruct1{}); err != nil {
if err := th.Index(webshop{}); err != nil {
t.Fatal(err)
}

Expand All @@ -69,7 +42,7 @@ func TestSearchWithIndex(t *testing.T) {
}

func TestSearchWithoutIndex(t *testing.T) {
if err := h.Index(testStruct1{}); err != nil {
if err := th.Index(webshop{}); err != nil {
t.Fatal(err)
}

Expand All @@ -81,7 +54,7 @@ func TestSearchWithoutIndex(t *testing.T) {
}

func BenchmarkWithIndex(b *testing.B) {
if err := h.Index(testStruct1{}); err != nil {
if err := th.Index(webshop{}); err != nil {
b.Fatal(err)
}

Expand All @@ -97,7 +70,7 @@ func BenchmarkWithIndex(b *testing.B) {
}

func BenchmarkWithoutIndex(b *testing.B) {
if err := h.Index(testStruct1{}); err != nil {
if err := th.Index(webshop{}); err != nil {
b.Fatal(err)
}

Expand All @@ -114,7 +87,7 @@ func BenchmarkWithoutIndex(b *testing.B) {

func searchIndexedProperty(t *testing.T) (time.Time, gocb.QueryResults, error) {
start := time.Now()
resp, err := h.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(email, $1)"), []interface{}{"a"})
resp, err := th.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(email, $1)"), []interface{}{"a"})
if err != nil {
return start, nil, err
}
Expand All @@ -124,7 +97,7 @@ func searchIndexedProperty(t *testing.T) (time.Time, gocb.QueryResults, error) {

func searchNotIndexedProperty(t *testing.T) (time.Time, gocb.QueryResults, error) {
start := time.Now()
resp, err := h.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(billing_address_address_2, $1)"), []interface{}{"a"})
resp, err := th.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(billing_address_address_2, $1)"), []interface{}{"a"})
if err != nil {
return start, nil, err
}
Expand Down
Loading

0 comments on commit 95d74df

Please sign in to comment.