Skip to content

Commit fb86e42

Browse files
abbyhu2000sipopo
authored andcommitted
README.md for saving index pattern relationship (#2276)
Add readme for saved object and index pattern relationship Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com> Signed-off-by: Sergey V. Osipov <sipopo@yandex.ru>
1 parent c46bd14 commit fb86e42

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Saved object
2+
3+
The saved object plugin provides all the core services and functionalities of saved objects. It is utilized by many core plugins such as [`visualization`](../visualizations/), [`dashboard`](../dashboard/) and [`wizard`](../wizard/), as well as external plugins. Saved object is the primary way to store app and plugin data in a standardized form in OpenSearch Dashboards. They allow plugin developers to manage creating, saving, editing and retrieving data for the application. They can also make reference to other saved objects and have useful features out of the box, such as migrations and strict typings. The saved objects can be managed by the Saved Object Management UI.
4+
5+
## Save relationships to index pattern
6+
7+
Saved objects that have relationships to index patterns are saved using the [`kibanaSavedObjectMeta`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts#L59) attribute and the [`references`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts#L60) array structure. Functions from the data plugin are used by the saved object plugin to manage this index pattern relationship.
8+
9+
A standard saved object and its index pattern relationship:
10+
11+
```ts
12+
13+
"kibanaSavedObjectMeta" : {
14+
"searchSourceJSON" : """{"filter":[],"query":{"query":"","language":"kuery"},"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}"""
15+
}
16+
},
17+
"type" : "visualization",
18+
"references" : [
19+
{
20+
"name" : "kibanaSavedObjectMeta.searchSourceJSON.index",
21+
"type" : "index-pattern",
22+
"id" : "90943e30-9a47-11e8-b64d-95841ca0b247"
23+
}
24+
],
25+
26+
```
27+
28+
### Saving a saved object
29+
30+
When saving a saved object and its relationship to the index pattern:
31+
32+
1. A saved object will be built using [`buildSavedObject`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts#L46) function. Services such as hydrating index pattern, initializing and serializing the saved object are set, and configs such as saved object id, migration version are defined.
33+
2. The saved object will then be serialized by three steps:
34+
35+
a. By using [`extractReferences`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/data/common/search/search_source/extract_references.ts#L35) function from the data plugin, the index pattern information will be extracted using the index pattern id within the `kibanaSavedObjectMeta`, and the id will be replaced by a reference name, such as `indexRefName`. A corresponding index pattern object will then be created to include more detailed information of the index pattern: name (`kibanaSavedObjectMeta.searchSourceJSON.index`), type, and id.
36+
37+
```ts
38+
let searchSourceFields = { ...state };
39+
const references = [];
40+
41+
if (searchSourceFields.index) {
42+
const indexId = searchSourceFields.index.id || searchSourceFields.index;
43+
const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index';
44+
references.push({
45+
name: refName,
46+
type: 'index-pattern',
47+
id: indexId
48+
});
49+
searchSourceFields = { ...searchSourceFields,
50+
indexRefName: refName,
51+
index: undefined
52+
};
53+
}
54+
```
55+
56+
b. The `indexRefName` along with other information will be stringified and saved into `kibanaSavedObjectMeta.searchSourceJSON`.
57+
58+
c. Saved object client will create the reference array attribute, and the index pattern object will be pushed into the reference array.
59+
60+
61+
### Loading an existing or creating a new saved object
62+
63+
1. When loading an existing object or creating a new saved object, [`initializeSavedObject`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/saved_objects/public/saved_object/helpers/initialize_saved_object.ts#L38) function will be called.
64+
2. The saved object will be deserialized in the [`applyOpenSearchResp`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/saved_objects/public/saved_object/helpers/apply_opensearch_resp.ts#L50) function.
65+
66+
a. Using [`injectReferences`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/data/common/search/search_source/inject_references.ts#L34) function from the data plugin, the index pattern reference name within the `kibanaSavedObject` will be substituted by the index pattern id and the corresponding index pattern reference object will be deleted if filters are applied.
67+
68+
```ts
69+
searchSourceReturnFields.index = reference.id;
70+
delete searchSourceReturnFields.indexRefName;
71+
```
72+
73+
### Others
74+
75+
If a saved object type wishes to have additional custom functionalities when extracting/injecting references, or after OpenSearch's response, it can define functions in the class constructor when extending the `SavedObjectClass`. For example, visualization plugin's [`SavedVis`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/4a06f5a6fe404a65b11775d292afaff4b8677c33/src/plugins/visualizations/public/saved_visualizations/_saved_vis.ts#L91) class has additional `extractReferences`, `injectReferences` and `afterOpenSearchResp` functions defined in [`_saved_vis.ts`](../visualizations/public/saved_visualizations/_saved_vis.ts).
76+
77+
```ts
78+
class SavedVis extends SavedObjectClass {
79+
constructor(opts: Record<string, unknown> | string = {}) {
80+
super({
81+
... ...
82+
extractReferences,
83+
injectReferences,
84+
... ...
85+
afterOpenSearchResp: async (savedObject: SavedObject) => {
86+
const savedVis = (savedObject as any) as ISavedVis;
87+
... ...
88+
89+
return (savedVis as any) as SavedObject;
90+
},
91+
```
92+
93+
## Migration
94+
95+
When a saved object is created using a previous version, the migration will trigger if there is a new way of saving the saved object and the migration functions alter the structure of the old saved object to follow the new structure. Migrations can be defined in the specific saved object type in the plugin's server folder. For example,
96+
97+
```ts
98+
export const visualizationSavedObjectType: SavedObjectsType = {
99+
name: 'visualization',
100+
management: {},
101+
mappings: {},
102+
migrations: visualizationSavedObjectTypeMigrations,
103+
};
104+
```
105+
106+
```ts
107+
const visualizationSavedObjectTypeMigrations = {
108+
'1.0.0': flow(migrateIndexPattern),
109+
```
110+
111+
The migraton version will be saved as a `migrationVersion` attribute in the saved object, not to be confused with the other `verson` attribute.
112+
113+
```ts
114+
"migrationVersion" : {
115+
"visualization" : "1.0.0"
116+
},
117+
```
118+
119+
For a more detailed explanation on the migration, refer to [`saved objects management`](src/core/server/saved_objects/migrations/README.md).

0 commit comments

Comments
 (0)