How force index level for raw query


I currently have a elasticsearch index like :
logs-{ automate timestamp message }

I have a query like
{ ""aggs"": { ""langs"": { ""terms"": {""field"": ""automate""}} } }

That works fine is the requester or in python

But I can't find a way to do the same thing in .net

I try
var searchResults = client.Search < string > (s => s .Profile() .From(0) .Index("logs-") .Size(100) .Query(q => q .Raw(@"{ ""aggs"": { ""langs"": { ""terms"": {""field"": ""automate""} } } }") )
But have this error:
Invalid NEST response built from a unsuccessful low level call on POST:/logs-/string/_search} Nest.ISearchResponse<string> {Nest.SearchResponse<string>

How force POST:/logs-/_search and not POST:/logs-/string/_search ?

(Try also like the documentation:
.Aggregations(a => a.Terms("automate", st => st .Field(o => o.Content) .Size(10) .ExecutionHint(TermsAggregationExecutionHint.Ordinals))
But o.Content do not compile ...) :2.X
Nest 2.X
Elasticsearch 2.3.3


A few observations

  1. You can't use .Query(q => q.Raw("")) to send an aggregation string as the json string here is inserted inside the "query": { } object on the request. There is an open issue for supporting raw aggregations, but it's tricky :smile:
  2. You can omit the type in the URI by specifying .AllTypes() on the SearchDescriptor<T> (the s in your example).

You may prefer to use Elasticsearch.Net to send this request, as you can send a raw string as you have; using the Elasticsearch.Net client exposed through the .LowLevel property of the NEST client, you can have the best of both worlds; use NEST for high level calls, and drop down to Elasticsearch.Net when you want to, still taking advantage of strong types on the response. Here's an example

var client = new ElasticClient();

var searchResults = client.LowLevel.Search<string>("log-", @"{
    ""aggs"": {
        ""langs"": {
            ""terms"": {""field"": ""automate""}

You can also use an anonymous type to represent the query. It's a little bit easier than escaping quotes in a json string

var client = new ElasticClient();

var searchResults = client.LowLevel.Search<string>("log-", 
new {
    aggs = new {
        langs = new {
            terms = new { field = "automate" }

The string generic type in Search<T> is the type of the response. You can use the NEST SearchResponse<T> and expose documents in a strongly typed fashion e.g. using SearchResponse<JObject>.

1 Like

Really thank you for you time

The solution with client.LowLevel works fine for me :slight_smile:
Just for my knowledge, I see that you add the index, should I also add document type?

(The second code did not compile on new { for me, but the first works :wink: )


There is just one thing, that I do not understang on this forum.
It is how can we correctly format code ... :sweat:
If I copy paste your code, it is good, but when I try to do it myself that won't work
Really space :disappointed_relieved:

Add the type if you want to search for a specific document type within an index/indices, otherwise leave it out.

Once you have pasted your code, highlight it and press the </> button in the editor. Alternatively, you can enclose a block of code inside ``` and ```, optionally adding the language name after the opening ``` e.g.

Code here

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.