Select Git revision
convert_image.py
-
Frederik Menke authoredFrederik Menke authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
ElasticsearchSearchClient.cs 14.07 KiB
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SemanticSearchImplementation
{
/// <summary>
/// Implements necessary functions to use Elasticsearch as a search engine.
/// </summary>
/// <inheritdoc cref="ISearchClient"/>
public class ElasticsearchSearchClient : ISearchClient
{
// API
private const string ALIASES = "_aliases";
private const string SEARCH = "_search";
private const string MAPPING = "_mapping";
private const string BULK = "_bulk";
private static readonly HttpClient client = new HttpClient();
private readonly string baseUrl;
private string _index;
public ElasticsearchSearchClient(string server = "localhost", string port = "9200")
{
baseUrl = $"http://{server}:{port}/";
client.DefaultRequestHeaders.Add("Accept", "application/json");
}
public void ChangeIndex(string index)
{
_index = index;
}
public async Task<IDictionary<string, string>> GetMappingAsync()
{
var response = await client.GetAsync(baseUrl + _index + "/" + MAPPING);
JObject jObject = JObject.Parse(await response.Content.ReadAsStringAsync());
var fieldMappings = (JObject)jObject[_index]["mappings"]["properties"];
IDictionary<string, string> mapping = new Dictionary<string, string>();
foreach (var prop in fieldMappings)
{
mapping.Add(prop.Key, (string) prop.Value["type"]);
}
return mapping;
}
/// <summary>
/// Queries the document ID for a metadata graph.
/// </summary>
/// <param name="graphName">ID of the metadata graph.</param>
/// <returns>ID of the Elasticsearch document.</returns>
private async Task<string> GetIdFromGraphNameAsync(string graphName)
{
var content = new JObject() {
new JProperty("query", new JObject
{
new JProperty("term", new JObject
{
new JProperty(ElasticsearchIndexMapper.LABEL_GRAPHNAME, new JObject
{
new JProperty("value", graphName)
})
})