Skip to content

Commit 0909616

Browse files
committed
[DOCS] Adds documentation for FaaS use cases
1 parent f167eea commit 0909616

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

docs/connecting.asciidoc

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This page contains the information you need to connect and use the Client with
88

99
* <<client-auth, Authentication options>>
1010
* <<client-usage, Using the client>>
11-
11+
* <<client-faas, Using the Client in a Function-as-a-Service Environment>>
1212

1313
[discrete]
1414
[[client-auth]]
@@ -144,3 +144,73 @@ client.indices.refresh(index: 'my-index')
144144
145145
client.search(index: 'my-index', body: { query: { match: { title: 'test' } } })
146146
------------------------------------
147+
148+
149+
[discrete]
150+
[[client-faas]]
151+
=== Using the Client in a Function-as-a-Service Environment
152+
153+
This section illustrates the best practices for leveraging the {es} client in a Function-as-a-Service (FaaS) environment. The most influential optimization is to initialize the client outside of the function, the global scope. This practice does not only improve performance but also enables background functionality as – for example – sniffing. The following examples provide a skeleton for the best practices.
154+
155+
[discrete]
156+
==== GCP Cloud Functions
157+
158+
[source,ruby]
159+
------------------------------------
160+
require 'functions_framework'
161+
require 'elasticsearch'
162+
163+
client = Elasticsearch::Client.new(
164+
cloud_id: "elasic-cloud-id",
165+
user: "elastic",
166+
password: "password",
167+
log: true
168+
)
169+
170+
FunctionsFramework.http "hello_world" do |request|
171+
client.search(
172+
index: 'stack-overflow',
173+
body: {
174+
query: {
175+
match: {
176+
title: {
177+
query: 'phone application'
178+
}
179+
}
180+
}
181+
}
182+
)
183+
end
184+
------------------------------------
185+
186+
[discrete]
187+
==== AWS Lambda
188+
189+
[source,ruby]
190+
------------------------------------
191+
require 'elasticsearch'
192+
193+
def client
194+
@client ||= Elasticsearch::Client.new(
195+
cloud_id: "elastic-cloud-id",
196+
user: "elastic",
197+
password: "password",
198+
log: true
199+
)
200+
end
201+
202+
def lambda_handler(event:, context:)
203+
client.search(
204+
index: 'stack-overflow',
205+
body: {
206+
query: {
207+
match: {
208+
title: {
209+
query: 'phone application'
210+
}
211+
}
212+
}
213+
}
214+
)
215+
end
216+
------------------------------------

0 commit comments

Comments
 (0)