diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b5c28f8..022d1552 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [master](https://github.com/arangodb/go-driver/tree/master) (N/A) - Use Go 1.19.4 -- Add `IsExternalStorageError` to check for [external storage errors](https://www.arangodb.com/docs/stable/appendix-error-codes.html#external-arangodb-storage-errors). +- Add `IsExternalStorageError` to check for [external storage errors](https://www.arangodb.com/docs/stable/appendix-error-codes.html#external-arangodb-storage-errors) +- `nested` field in arangosearch type View ## [1.4.1](https://github.com/arangodb/go-driver/tree/v1.4.1) (2022-12-14) - Add support for `checksum` in Collections diff --git a/test/types.go b/test/types.go index 668b2dd9..95ccf08d 100644 --- a/test/types.go +++ b/test/types.go @@ -39,6 +39,16 @@ type UserDocWithKeyWithOmit struct { Age int `json:"age,omitempty"` } +type NestedFieldsDoc struct { + Name string `json:"name"` + Dimensions []Dimension `json:"dimensions,omitempty"` +} + +type Dimension struct { + Type string `json:"type"` + Value int `json:"value"` +} + type Account struct { ID string `json:"id"` User *UserDoc `json:"user"` diff --git a/test/view_test.go b/test/view_test.go index 55971046..bf1dbba8 100644 --- a/test/view_test.go +++ b/test/view_test.go @@ -512,6 +512,74 @@ func TestUseArangoSearchView(t *testing.T) { } } +// TestUseArangoSearchViewWithNested tries to create a view with nested fields and actually use it in an AQL query. +func TestUseArangoSearchViewWithNested(t *testing.T) { + ctx := context.Background() + // don't use disallowUnknownFields in this test - we have here custom structs defined + c := createClient(t, true, false) + skipBelowVersion(c, "3.10", t) + skipNoEnterprise(t) + db := ensureDatabase(nil, c, "view_nested_test", nil, t) + col := ensureCollection(ctx, db, "some_collection", nil, t) + + ensureArangoSearchView(ctx, db, "some_nested_view", &driver.ArangoSearchViewProperties{ + Links: driver.ArangoSearchLinks{ + "some_collection": driver.ArangoSearchElementProperties{ + Fields: driver.ArangoSearchFields{ + "dimensions": driver.ArangoSearchElementProperties{ + Nested: driver.ArangoSearchFields{ + "type": driver.ArangoSearchElementProperties{}, + "value": driver.ArangoSearchElementProperties{}, + }, + }, + }, + }, + }, + }, t) + + docs := []NestedFieldsDoc{ + { + Name: "John", + Dimensions: []Dimension{ + {"height", 10}, + {"weight", 80}, + }, + }, + { + Name: "Jakub", + Dimensions: []Dimension{ + {"height", 25}, + {"weight", 80}, + }, + }, + { + Name: "Marek", + Dimensions: []Dimension{ + {"height", 30}, + {"weight", 80}, + }, + }, + } + + _, errs, err := col.CreateDocuments(ctx, docs) + if err != nil { + t.Fatalf("Failed to create new documents: %s", describe(err)) + } else if err := errs.FirstNonNil(); err != nil { + t.Fatalf("Expected no errors, got first: %s", describe(err)) + } + + // now access it via AQL with waitForSync + { + query := "FOR doc IN some_nested_view SEARCH doc.dimensions[? FILTER CURRENT.type == \"height\" AND CURRENT.value > 20] OPTIONS {waitForSync:true} RETURN doc" + cur, err := db.Query(driver.WithQueryCount(ctx), query, nil) + if err != nil { + t.Fatalf("Failed to query data using arangodsearch: %s", describe(err)) + } else if cur.Count() != 2 || !cur.HasMore() { + t.Fatalf("Wrong number of return values: expected 1, found %d", cur.Count()) + } + } +} + // TestUseArangoSearchViewWithPipelineAnalyzer tries to create a view and analyzer and then actually use it in an AQL query. func TestUseArangoSearchViewWithPipelineAnalyzer(t *testing.T) { ctx := context.Background() diff --git a/view_arangosearch.go b/view_arangosearch.go index 3d241517..b8a5e915 100644 --- a/view_arangosearch.go +++ b/view_arangosearch.go @@ -492,6 +492,9 @@ type ArangoSearchElementProperties struct { // so that it remains basically available. inBackground is an option that can be set when adding links. // It does not get persisted as it is not a View property, but only a one-off option InBackground *bool `json:"inBackground,omitempty"` + // Nested contains the properties for nested fields (sub-objects) of the element + // Enterprise Edition only + Nested ArangoSearchFields `json:"nested,omitempty"` // Cache If you enable this option, then field normalization values are always cached in memory. // Introduced in v3.9.5, Enterprise Edition only Cache *bool `json:"cache,omitempty"`