Document collections in Macrometa's GDN are schema-less, which makes them flexible. The tradeoff comes when trying to navigate complex documents to locate data. We can use the Search feature to more easily access data nested inside a document.
Note - Currently, using the GDN Console GUI a user can only create a Search(View) for top-level fields. You should be familiar with JSON and the GDN API before continuing.
We are going to create this Search View by making a call to the following GDN REST API endpoint,
https://api-gdn.paas.macrometa.io/_fabric/{your-fabric-name}/_api/search/view
First, we will create a JSON object with the following properties:
-
links (object):
-
[your-collection-name] (string):
-
analyzers (array of strings): The list of analyzers to be used for indexing of string values (default: ["identity"]).
-
fields (object):
-
field-name (object/string): This is a recursive structure for the specific attribute path, potentially containing any of the following attributes: analyzers, include all fields, trackListPositions, storeValuesAny attributes not specified are inherited from the parent.
-
-
includeAllFields (boolean): A flag that determines whether or not to index all fields on a particular level of depth (default: false).
-
trackListPositions (boolean): A flag that determines whether or not values in a list should be treated separately (default: false).
-
storeValues: How should the view track the attribute values, this setting allows for additional value retrieval optimizations, one of:
-
none: Do not store values by the view
-
id: Store only information about value presence, to allow the use of the EXISTS() function(default "none")
-
-
-
-
name (string): The name of the view.
-
primarySort (array of strings): The default sort for the view.
-
type: The type of the view. The value must be "search".
Example of JSON request body for nested Search:
{
"links": {
"testCollection": {
"analyzers": [ "text_en" ],
"fields": {
"parties": {
"fields": {
"name": {}
}
}
},
"includeAllFields": false,
"storeValues": "none",
"trackListPositions": false
}
},
"name": "testView",
"primarySort": [],
"type": "search"
}
Let's focus for a moment on the nested fields within this Search View. There are two options for accessing the nested data.
1. Use the top-level field and set the includeAllFields parameter to true. When the view is created all sub-fields of the top-level field will be included. As previously stated, the default value is false.
"includeAllFields": true
2. This option sets a path to the sub-field. The example below demonstrates how to set a path to the following nested field `topLevelField.subLevelField`.
"fields": {
"topLevelField": {
"fields": {
"subLevelField": {}
}
}
}
This second option is more optimized because it will only index on the specified fields, `subLevelField` in this case. The first option could be a good choice if you may add more sub-fields in the future.
The code snippet below shows a complete curl request to create the nested field search view.
curl -X 'POST' \
'https://api-gdn.paas.macrometa.io/_fabric/<your-fabric-name>/_api/search/view' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Authorization: bearer <JWT_TOKEN>' \
-d '{
"links": {
"testCollection": {
"analyzers": [ "text_en" ],
"fields": {
"parties": {
"fields": {
"name": {}
}
}
},
"includeAllFields": false,
"storeValues": "none",
"trackListPositions": false
}
},
"name": "testView",
"primarySort": [],
"type": "search"
}'
Conclusion
In summary, you can create a search view indexed on a nested object attribute using the GDN API. This can be configured with a simple boolean flag to include all subfields or with more granular control by creating a path from the top-level field to the desired subfield(s). Then you can use your preferred method to submit the request to the REST API endpoint. For more information about using Search with the Macrometa GDN follow the links below.
Comments
0 comments
Please sign in to leave a comment.