'Query Elastic App Search with flutter using public api key

I am developing a Flutter mobile and web app to query data from Elasticsearch.

The Elastic App Search provides me a public search key which I would like my Flutter app to use to get data from my index. All the examples e.g. https://pub.dev/packages/elastic_client do authenticate with basic auth and user/pass.

Are there any approaches or existing libraries which I may use with Flutter?

Currently I am stuck at the very beginning.

I successfully connected the "Search with Elastic App Search" AddOn in Firebase to update my Elastic documents when a document in my Cloud Firestore changes.

In my Flutter app I would like to only query the Elasic index. There is no need for insert, update nor delete.

Before, I used Algolia https://pub.dev/packages/algolia where I could set up e.g.

  static const Algolia algolia = Algolia.init(
    applicationId: '<my-application-id>',
    apiKey: '<my-api-key>',
  );

Now I am looking for something similar to connect to my Elastic App Search.

Do I have a general misunderstanding or did I miss anything in my plan?



Solution 1:[1]

There's a way to utilize public key with elastic_client, simply place api key as the authorization parameter:

Client.HttpTransport(url: 'https://ES_ENDPOINT:9243/', authorization: "ApiKey <BASE64_ENCODED_VALUE>");

The trick is to add "ApiKey" in front of the hashed/encoded value. Encoded value contains <id>:<api_key>, values generated via POST /_security/api_key (Create ES API key).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir