Have you used the Kibana Dev Console? This is a fantastic prototyping tool that allows you to build and test your Elasticsearch requests interactively. But what do you do after you have a working request in the Console?
In this article we'll take a look at the new code generation feature in the Kibana Dev Console, and how it can significantly reduce your development effort by generating ready to use code for you.
This feature is available in our Serverless platform and in Elastic Cloud and self-hosted releases 8.16 and up.
The Kibana Dev Console
This section provides a quick introduction to the Kibana Dev Console, in case you have never used it before. Skip to the next section if you are already familiar with it.
While you are in any part of the Search section in Kibana, you will notice a "Console" link at the bottom of your browser's page:
When you click this link, the Console expands to cover the page. Click it again to collapse it.
In the left-side panel of the Dev Console, you can enter Elasticsearch requests, with the help of an interactive editor that provides auto-completion and checks your syntax. Some example requests are already pre-populated so that you have something to start experimenting with.
When the cursor is on a request, a "play" button appears to its right. You can click this button to send the request to your Elasticsearch server.
After you execute a request, the response from the server appears in the panel on the right.
Code Export Feature
The Dev Console makes it easy to prototype your requests or queries until you get exactly what you want. But what happens next? If you need to convert the request to code so that you can incorporate it into your application, then you can save time using the new code export feature.
Next to the Play button you will find the three dot or "kebab" button, which opens a menu of options. The first option provides access to the code export feature. If you've never used this feature before, it will appear with a "Copy as curl" label.
If you select this option, your clipboard will be loaded with a curl command that is equivalent to the selected request.
Now, things get more interesting when you click the "Change" link, which allows you to switch to a different target language. In this initial release, the code export adds support for Python and JavaScript. More languages are expected to be added in future releases.
You can now select your desired language and click "Copy code" to put the exported code in your clipboard. You can also change the default language that is offered in the menu.
The exported code is a complete script in the selected language, using the official Elasticsearch client for that language. Here is an example of how the PUT /my-index
request shown above looks when exported to the Python language:
import os
from elasticsearch import Elasticsearch
client = Elasticsearch(
hosts=["<your-elasticsearch-endpoint-url-here"],
api_key=os.getenv("ELASTIC_API_KEY"),
)
resp = client.indices.create(
index="my-index",
)
print(resp)
To use the exported code follow these steps:
- Paste the code from the clipboard to a new file with the correct extension (
.py
for Python, or.js
for JavaScript). - In your terminal, add an environment variable called
ELASTIC_API_KEY
with a valid API Key for your Elasticsearch cluster. You can create an API key right in Kibana if you don't have one yet. - Execute the script with the
python
ornode
commands depending on your language, making sure the official Elasticsearch client is installed.
Now you are ready to adapt the exported code as needed to integrate it into your application!
Conclusion
In this article you have learned about the new Code Export feature in the Kibana Dev Console. We hope this feature will streamline your development process with Elasticsearch!
Ready to try this out on your own? Start a free trial.
Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!