Programming/How To Post data to Elasticsearch: Difference between revisions
From Wiki Aghanim
Jump to navigationJump to search
| (7 intermediate revisions by the same user not shown) | |||
| Line 17: | Line 17: | ||
# Click '''Create API key''' | # Click '''Create API key''' | ||
# '''Copy the key immediately''' — it is only shown once | # '''Copy the key immediately''' — it is only shown once | ||
== Your Project == | |||
In <code>appsettings.json</code> add the following. | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"Elasticsearch": { | |||
"Url": "https://10.18.20.1:9200", | |||
"IndexName": "my-index", | |||
"ApiKey": "U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ==" | |||
} | |||
} | |||
</syntaxhighlight> | |||
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. ''my-index/_doc'' is just the indices that I made. | |||
<syntaxhighlight lang="csharp"> | |||
public Task<bool> PostDocument(MessageModel message) | |||
{ | |||
var apiKey = _configuration["Elasticsearch:ApiKey"]; | |||
var url = _configuration["Elasticsearch:Url"]; | |||
var json = JsonSerializer.Serialize(message); | |||
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); | |||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ApiKey", apiKey); | |||
var body = new StringContent(json, Encoding.UTF8, "application/json"); | |||
var result = _httpClient.PostAsync($"{url}/my-index/_doc/", body).GetAwaiter().GetResult(); | |||
if (result.IsSuccessStatusCode) | |||
{ | |||
Console.WriteLine(result.ToString()); | |||
return Task.FromResult(true); | |||
} | |||
return Task.FromResult(false); | |||
} | |||
} | |||
</syntaxhighlight> | |||
Now you can expose this service via a controller. | |||
<syntaxhighlight lang="csharp"> | |||
[HttpPost] | |||
public Task<ActionResult> PostData([FromBody] MessageModel messageModel) | |||
{ | |||
var elastic = new ElasticsearchService(_httpClient, _configuration); | |||
var result = elastic.PostDocument(messageModel); | |||
if (result.IsCompleted) | |||
{ | |||
return OkResult | |||
} | |||
return OkResult(result); | |||
} | |||
</syntaxhighlight> | |||
[[Category:.NET]] | |||
[[Category:CSharp]] | |||
[[Category:Programming]] | |||
Latest revision as of 13:40, 3 March 2026
Elasticsearch API Key
In order to POST or GET data from Elasticsearch, you need an API key.
You must have one of the following cluster privileges:
manage_securitymanage_api_keymanage_own_api_key
- Open Kibana
- In the left sidebar, scroll down and click Stack Management
- Under Security, click API Keys
- Click Create API key
- Fill in the following fields:
- Name — give it a meaningful name
- Expiration — optional, leave blank for no expiration
- Privileges — set index/cluster permissions (e.g. index
*with privilegewriteto post data)
- Click Create API key
- Copy the key immediately — it is only shown once
Your Project
In appsettings.json add the following.
{
"Elasticsearch": {
"Url": "https://10.18.20.1:9200",
"IndexName": "my-index",
"ApiKey": "U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ=="
}
}
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. my-index/_doc is just the indices that I made.
public Task<bool> PostDocument(MessageModel message)
{
var apiKey = _configuration["Elasticsearch:ApiKey"];
var url = _configuration["Elasticsearch:Url"];
var json = JsonSerializer.Serialize(message);
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ApiKey", apiKey);
var body = new StringContent(json, Encoding.UTF8, "application/json");
var result = _httpClient.PostAsync($"{url}/my-index/_doc/", body).GetAwaiter().GetResult();
if (result.IsSuccessStatusCode)
{
Console.WriteLine(result.ToString());
return Task.FromResult(true);
}
return Task.FromResult(false);
}
}
Now you can expose this service via a controller.
[HttpPost]
public Task<ActionResult> PostData([FromBody] MessageModel messageModel)
{
var elastic = new ElasticsearchService(_httpClient, _configuration);
var result = elastic.PostDocument(messageModel);
if (result.IsCompleted)
{
return OkResult
}
return OkResult(result);
}