Skip to content

Commit 5a55b22

Browse files
authored
GT-317 Nested field in arangosearch type View (#464)
1 parent 18378be commit 5a55b22

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
44
- Use Go 1.19.4
5-
- Add `IsExternalStorageError` to check for [external storage errors](https://www.arangodb.com/docs/stable/appendix-error-codes.html#external-arangodb-storage-errors).
5+
- Add `IsExternalStorageError` to check for [external storage errors](https://www.arangodb.com/docs/stable/appendix-error-codes.html#external-arangodb-storage-errors)
6+
- `nested` field in arangosearch type View
67

78
## [1.4.1](https://github.com/arangodb/go-driver/tree/v1.4.1) (2022-12-14)
89
- Add support for `checksum` in Collections

test/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ type UserDocWithKeyWithOmit struct {
3939
Age int `json:"age,omitempty"`
4040
}
4141

42+
type NestedFieldsDoc struct {
43+
Name string `json:"name"`
44+
Dimensions []Dimension `json:"dimensions,omitempty"`
45+
}
46+
47+
type Dimension struct {
48+
Type string `json:"type"`
49+
Value int `json:"value"`
50+
}
51+
4252
type Account struct {
4353
ID string `json:"id"`
4454
User *UserDoc `json:"user"`

test/view_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,74 @@ func TestUseArangoSearchView(t *testing.T) {
512512
}
513513
}
514514

515+
// TestUseArangoSearchViewWithNested tries to create a view with nested fields and actually use it in an AQL query.
516+
func TestUseArangoSearchViewWithNested(t *testing.T) {
517+
ctx := context.Background()
518+
// don't use disallowUnknownFields in this test - we have here custom structs defined
519+
c := createClient(t, true, false)
520+
skipBelowVersion(c, "3.10", t)
521+
skipNoEnterprise(t)
522+
db := ensureDatabase(nil, c, "view_nested_test", nil, t)
523+
col := ensureCollection(ctx, db, "some_collection", nil, t)
524+
525+
ensureArangoSearchView(ctx, db, "some_nested_view", &driver.ArangoSearchViewProperties{
526+
Links: driver.ArangoSearchLinks{
527+
"some_collection": driver.ArangoSearchElementProperties{
528+
Fields: driver.ArangoSearchFields{
529+
"dimensions": driver.ArangoSearchElementProperties{
530+
Nested: driver.ArangoSearchFields{
531+
"type": driver.ArangoSearchElementProperties{},
532+
"value": driver.ArangoSearchElementProperties{},
533+
},
534+
},
535+
},
536+
},
537+
},
538+
}, t)
539+
540+
docs := []NestedFieldsDoc{
541+
{
542+
Name: "John",
543+
Dimensions: []Dimension{
544+
{"height", 10},
545+
{"weight", 80},
546+
},
547+
},
548+
{
549+
Name: "Jakub",
550+
Dimensions: []Dimension{
551+
{"height", 25},
552+
{"weight", 80},
553+
},
554+
},
555+
{
556+
Name: "Marek",
557+
Dimensions: []Dimension{
558+
{"height", 30},
559+
{"weight", 80},
560+
},
561+
},
562+
}
563+
564+
_, errs, err := col.CreateDocuments(ctx, docs)
565+
if err != nil {
566+
t.Fatalf("Failed to create new documents: %s", describe(err))
567+
} else if err := errs.FirstNonNil(); err != nil {
568+
t.Fatalf("Expected no errors, got first: %s", describe(err))
569+
}
570+
571+
// now access it via AQL with waitForSync
572+
{
573+
query := "FOR doc IN some_nested_view SEARCH doc.dimensions[? FILTER CURRENT.type == \"height\" AND CURRENT.value > 20] OPTIONS {waitForSync:true} RETURN doc"
574+
cur, err := db.Query(driver.WithQueryCount(ctx), query, nil)
575+
if err != nil {
576+
t.Fatalf("Failed to query data using arangodsearch: %s", describe(err))
577+
} else if cur.Count() != 2 || !cur.HasMore() {
578+
t.Fatalf("Wrong number of return values: expected 1, found %d", cur.Count())
579+
}
580+
}
581+
}
582+
515583
// TestUseArangoSearchViewWithPipelineAnalyzer tries to create a view and analyzer and then actually use it in an AQL query.
516584
func TestUseArangoSearchViewWithPipelineAnalyzer(t *testing.T) {
517585
ctx := context.Background()

view_arangosearch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ type ArangoSearchElementProperties struct {
492492
// so that it remains basically available. inBackground is an option that can be set when adding links.
493493
// It does not get persisted as it is not a View property, but only a one-off option
494494
InBackground *bool `json:"inBackground,omitempty"`
495+
// Nested contains the properties for nested fields (sub-objects) of the element
496+
// Enterprise Edition only
497+
Nested ArangoSearchFields `json:"nested,omitempty"`
495498
// Cache If you enable this option, then field normalization values are always cached in memory.
496499
// Introduced in v3.9.5, Enterprise Edition only
497500
Cache *bool `json:"cache,omitempty"`

0 commit comments

Comments
 (0)