Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch NEST Executing raw Query DSL

I'm trying to create the simplest proxy possible in an API to execute searches on ElasticSearch nodes. The only reason for the proxy to be there is to "hide" the credentials and abstract ES from the API endpoint.

Using Nest.ElasticClient, is there a way to execute a raw string query? Example query that is valid in vanilla ES:

{
    "query": {
        "fuzzy": { "title": "potato" }
    }
}

In my API, I tried Deserializing the raw string into a SearchRequest, but it fails. I'm assuming it cannot deserialize the field:

var req = m_ElasticClient.Serializer.Deserialize<SearchRequest>(p_RequestBody);
var res = m_ElasticClient.Search<T>(req);
return m_ElasticClient.Serializer.SerializeToString(res);

System.InvalidCastException: Invalid cast from 'System.String' to 'Newtonsoft.Json.Linq.JObject'.

Is there a way to just forward the raw string query to ES and return the string response? I tried using the LowLevel.Search method without luck.

like image 786
MicG Avatar asked Dec 24 '22 17:12

MicG


1 Answers

NEST does not support deserializing the short form of "field_name" : "your_value" of the Elasticsearch Query DSL, but it does support the long form "field_name" : { "value" : "your_value" }, so the following works

var client = new ElasticClient();

var json = @"{
    ""query"": {
        ""fuzzy"": { 
            ""title"": {
                ""value"": ""potato""
            }
        }
    }
}";

SearchRequest searchRequest;
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
    searchRequest = client.Serializer.Deserialize<SearchRequest>(stream);
}

As Rob has answered, NEST also supports supplying a raw json string as a query

like image 118
Russ Cam Avatar answered Dec 31 '22 04:12

Russ Cam