Skip to content

Commit

Permalink
Java & Go snippets (#1080)
Browse files Browse the repository at this point in the history
* go docs set up

* configurable weaviate host for java

* configurable weaviate host for go

* java docs for manage-data.create

* go docs for manage-data.create

* change go indent style to tabs

* Batch import items

* Manage-data -> Retrieve objects

* How-to: Manage-data -> Delete objects

* How-to: Manage data -> Update objects

* How-to: Manage-data -> Multi-tenancy operations

* How-to: Manage-data -> Cross-references

* How-to: Manage data -> Read all objects

* Updates while reviewing Java & Go code examples

- Bump Weaviate versions
- Fix TS CRUD tests (pointing to wrong clusters)
- Consolidate pytest ini

* Minor fixes; update test port

- Update anonymous Weaviate instance port to 8080 (from 8090)
- Update TS tests accordingly

* update spacing

---------

Co-authored-by: Marcin Antas <[email protected]>
Co-authored-by: JP Hwang <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2023
1 parent 4d1d58f commit 986f33d
Show file tree
Hide file tree
Showing 49 changed files with 19,805 additions and 21,214 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ insert_final_newline = true
quote_type = single

[*.{js,py}]
indent_style = space
indent_size = 4

[*.{go}]
indent_style = tab
indent_size = 2

[*.{md,txt}]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.venv
nohup.out
tests/temp.*
_includes/code/howto/java/target
_includes/code/howto/go/vendor
**/.ipynb
**/.ipynb_checkpoints/*

Expand Down
28 changes: 28 additions & 0 deletions _includes/code/howto/go/docs/helper/envhelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package helper

import "os"

const (
envScheme = "WEAVIATE_SCHEME"
envHost = "WEAVIATE_HOST"
envPort = "WEAVIATE_PORT"
)

func EnvScheme(defaultScheme string) string {
return Env(envScheme, defaultScheme)
}

func EnvHost(defaulHost string) string {
return Env(envHost, defaulHost)
}

func EnvPort(defaultPort string) string {
return Env(envPort, defaultPort)
}

func Env(envName string, def string) string {
if env := os.Getenv(envName); env != "" {
return env
}
return def
}
125 changes: 125 additions & 0 deletions _includes/code/howto/go/docs/manage-data.classes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// How-to: Manage-Data -> Classes
package docs

import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/vectorindex/hnsw"
"weaviate.io/docs/docs/helper"
)

func Test_ManageDataClasses(t *testing.T) {
ctx := context.Background()
scheme := helper.EnvScheme("http")
host := helper.EnvHost("localhost")
port := helper.EnvPort("8080")

config := weaviate.Config{Scheme: scheme, Host: host + ":" + port}
client, err := weaviate.NewClient(config)
require.NoError(t, err)

err = client.Schema().AllDeleter().Do(ctx)
require.NoError(t, err)

// START CreateClass // START ReadOneClass // START UpdateClass
className := "Article"

// END CreateClass // END ReadOneClass // END UpdateClass

t.Run("create class", func(t *testing.T) {
// START CreateClass
emptyClass := &models.Class{
Class: className,
}

// Add the class to the schema
err := client.Schema().ClassCreator().
WithClass(emptyClass).
Do(ctx)

// END CreateClass

require.NoError(t, err)
})

t.Run("read one class", func(t *testing.T) {
// START ReadOneClass
class, err := client.Schema().ClassGetter().
WithClassName(className).
Do(ctx)

// END ReadOneClass

require.NoError(t, err)

// START ReadOneClass
b, err := json.MarshalIndent(class, "", " ")
// END ReadOneClass
require.NoError(t, err)
// START ReadOneClass
fmt.Println(string(b))
// END ReadOneClass
})

t.Run("read all classes", func(t *testing.T) {
// START ReadAllClasses
schema, err := client.Schema().Getter().
Do(ctx)

// END ReadAllClasses

require.NoError(t, err)

// START ReadAllClasses
b, err := json.MarshalIndent(schema, "", " ")
// END ReadAllClasses
require.NoError(t, err)
// START ReadAllClasses
fmt.Println(string(b))
// END ReadAllClasses
})

t.Run("update class", func(t *testing.T) {
errDel := client.Schema().ClassDeleter().WithClassName(className).Do(ctx)
require.NoError(t, errDel)

// START UpdateClassTODO
// Define class
originalClass := &models.Class{
Class: className,
VectorIndexConfig: map[string]interface{}{
"distance": hnsw.DistanceCosine, // Note the distance metric
},
}

// Add the class to the schema
err := client.Schema().ClassCreator().
WithClass(originalClass).
Do(ctx)

// END UpdateClassTODO

require.NoError(t, err)

// START UpdateClassTODO
// Define updated class
updatedClass := &models.Class{
Class: className,
VectorIndexConfig: map[string]interface{}{
"distance": hnsw.DistanceDot, // Note the distance metric
},
}

// Update the class definition
_ = updatedClass
// TODO Not yet available in GO

// END UpdateClassTODO
})
}
170 changes: 170 additions & 0 deletions _includes/code/howto/go/docs/manage-data.create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// How-to: Manage-data -> Create objects
package docs

import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"weaviate.io/docs/docs/helper"
)

func Test_ManageDataCreate(t *testing.T) {
ctx := context.Background()
scheme := helper.EnvScheme("http")
host := helper.EnvHost("localhost")
port := helper.EnvPort("8080")
openaiApiKey := helper.Env("OPENAI_APIKEY", "_dummy_")

config := weaviate.Config{Scheme: scheme, Host: host + ":" + port, Headers: map[string]string{
"X-Openai-Api-Key": openaiApiKey,
}}
client, err := weaviate.NewClient(config)
require.NoError(t, err)

err = client.Schema().AllDeleter().Do(ctx)
require.NoError(t, err)

className := "JeopardyQuestion"

t.Run("create class", func(t *testing.T) {
jeopardyClass := &models.Class{
Class: className,
Description: "A Jeopardy! question",
Vectorizer: "text2vec-openai",
Properties: []*models.Property{
{
Name: "question",
DataType: schema.DataTypeText.PropString(),
},
{
Name: "answer",
DataType: schema.DataTypeText.PropString(),
},
},
}

err := client.Schema().ClassCreator().
WithClass(jeopardyClass).
Do(ctx)

require.NoError(t, err)
})

t.Run("create object", func(t *testing.T) {
// CreateObject START
w, err := client.Data().Creator().
WithClassName("JeopardyQuestion").
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
// "answer": "Weaviate", // schema properties can be omitted
"somePropNotInTheSchema": 123, // will be automatically added as a number property
}).
Do(ctx)

// CreateObject END

require.NoError(t, err)

// CreateObject START
// the returned value is a wrapped object
b, err := json.MarshalIndent(w.Object, "", " ")
// CreateObject END
require.NoError(t, err)
// CreateObject START
fmt.Println(string(b))
// CreateObject END
})

t.Run("create object with id and vector", func(t *testing.T) {
// CreateObjectWithIdAndVector START
vector := make([]float32, 1536)
for i := 0; i < len(vector); i++ {
vector[i] = 0.12345
}

w, err := client.Data().Creator().
WithClassName("JeopardyQuestion").
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
}).
// highlight-start
WithID("12345678-e64f-5d94-90db-c8cfa3fc1234").
WithVector(vector).
// highlight-end
Do(ctx)

// CreateObjectWithIdAndVector END

require.NoError(t, err)

// CreateObjectWithIdAndVector START
// the returned value is a wrapped object
b, err := json.MarshalIndent(w.Object, "", " ")
// CreateObjectWithIdAndVector END
require.NoError(t, err)
// CreateObjectWithIdAndVector START
fmt.Println(string(b))
// CreateObjectWithIdAndVector END
})

t.Run("create object with deterministic id", func(t *testing.T) {
// CreateObjectWithDeterministicIdTODO START
properties := map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
}

// highlight-start
// String id = "gen_uuid5(properties)"; // TODO implement
id := "17f91943-1a5e-46d3-9323-a1cdb735591c"
// highlight-end

w, err := client.Data().Creator().
WithClassName("JeopardyQuestion").
WithProperties(properties).
WithID(id).
Do(ctx)

// CreateObjectWithDeterministicIdTODO END

require.NoError(t, err)

// CreateObjectWithDeterministicIdTODO START
// the returned value is a wrapped object
b, err := json.MarshalIndent(w.Object, "", " ")
// CreateObjectWithDeterministicIdTODO END
require.NoError(t, err)
// CreateObjectWithDeterministicIdTODO START
fmt.Println(string(b))
// CreateObjectWithDeterministicIdTODO END
})

t.Run("validate object", func(t *testing.T) {
// ValidateObject START
err := client.Data().Validator().
WithClassName("JeopardyQuestion").
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
"thisPropShouldNotEndUpInTheSchema": -1,
}).
WithID("12345678-1234-1234-1234-123456789012").
Do(ctx)

// ValidateObject END

require.Error(t, err)

// ValidateObject START
assert.ErrorContains(t, err, "invalid object: no such prop with name 'thisPropShouldNotEndUpInTheSchema' found")
// ValidateObject END
})
}
Loading

0 comments on commit 986f33d

Please sign in to comment.