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 ...)
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
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>.
There is just one thing, that I do not understang on this forum.
It is how can we correctly format code ...
If I copy paste your code, it is good, but when I try to do it myself that won't work
Really space
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.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.