diff --git a/x-pack/examples/embedded_lens_example/public/app.tsx b/x-pack/examples/embedded_lens_example/public/app.tsx
index 3ef7721c58c02..fead11db04ed4 100644
--- a/x-pack/examples/embedded_lens_example/public/app.tsx
+++ b/x-pack/examples/embedded_lens_example/public/app.tsx
@@ -126,6 +126,9 @@ export const App = (props: {
from: 'now-5d',
to: 'now',
});
+ const [searchSession, setSearchSession] = useState(() =>
+ props.plugins.data.search.session.start()
+ );
const LensComponent = props.plugins.lens.EmbeddableComponent;
const LensSaveModalComponent = props.plugins.lens.SaveModalComponent;
@@ -239,6 +242,18 @@ export const App = (props: {
Change time range
+
+ {
+ setSearchSession(props.plugins.data.search.session.start());
+ }}
+ >
+ Refresh
+
+
{
setIsLoading(val);
}}
diff --git a/x-pack/examples/embedded_lens_example/public/mount.tsx b/x-pack/examples/embedded_lens_example/public/mount.tsx
index 49775bca09b72..1c4608f17b52d 100644
--- a/x-pack/examples/embedded_lens_example/public/mount.tsx
+++ b/x-pack/examples/embedded_lens_example/public/mount.tsx
@@ -40,5 +40,8 @@ export const mount =
);
render(reactElement, element);
- return () => unmountComponentAtNode(element);
+ return () => {
+ unmountComponentAtNode(element);
+ plugins.data.search.session.clear();
+ };
};
diff --git a/x-pack/plugins/lens/readme.md b/x-pack/plugins/lens/readme.md
index 340931efdcd52..f851486e1581c 100644
--- a/x-pack/plugins/lens/readme.md
+++ b/x-pack/plugins/lens/readme.md
@@ -109,6 +109,14 @@ if (!dataView) {
}
const dataViewIdForLens = dataView.id;
```
+## Refreshing a Lens embeddable
+
+The Lens embeddable is handling data fetching internally, this means as soon as the props change, it will trigger a new request if necessary. However, in some situations it's necessary to trigger a refresh even if the configuration of the chart doesn't change at all. Refreshing is managed using search sessions is Lens. To trigger a refresh without changing the actual configuration of a Lens embeddable, follow these steps:
+* Pull in the contract of the `data` plugin. It contains the session service at `plugins.data.search.session`.
+* When loading the app containing a Lens embeddable, start a new session using `session.start`. It returns the current session id - keep it in the state of our app (e.g. a `useState` hook or your redux store)
+* Pass the current session id to the Lens embeddable component via the `searchSessionId` property
+* When refreshing, simply call `session.start` again and update your state - Lens will discard the existing cache and re-fetch even if the query doesn't change at all
+* When unmounting your app, call `session.clear` to end the current session
# Lens Development