Elasticsearch NEST Document count for default index - elasticsearch

I am using NEST for Elasticsearch 6 and would like to get the number of documents for the default index.
The documentation refers to the 1.1 version of the API which no longer seems to work.
I have created the connection settings using a default index:
var connectionSettings = new ConnectionSettings().DefaultIndex("test_docs");
When I try the code from the 1.1 api documentation:
var result = client.Count();
I get the following error:
The type arguments for method
'ElasticClient.Count(Func, ICountRequest>)'
cannot be inferred from the usage. Try specifying the type arguments
explicitly.
When I supply a type it is appended to the path. For example:
client.Count<TestDocument>();
Generates a URL of http://localhost:9200/test_docs/testdocument/_count when what I really need is http://localhost:9200/test_docs/_count

For those needing the new way of doing this (like myself). I would use the following to get a count from a specific index.
var countRequest = new CountRequest(Indices.Index("videos"));
long count = (await _ecClient.CountAsync(countRequest)).Count;

You can use
var countResponse = client.Count<TestDocument>(c => c.AllTypes());
which will call the API
GET http://localhost:9200/test_docs/_count

Related

Is it possible to use ElasticSearch.Net or Nest for dynamic response

Is there a client.Read(...) without generics? I have found none, neither in Nest nor ElasticSearch.Net.
Version 1.5 has an IDocument that might solve my problem but I cannot use that version with Elasticsearch5.5.
All examples, version 5 and 6, of ElasticSearch.Net and Nest require me to know the format of the response as generic at compile time. E.g. Read<Customer>(...)
My problem is that the we do not know the format of the database and we don't know the format of the output; but it should all be configurable.
You can use dynamic as the generic type if the response is truly dynamic.
In 5.x, this will be Json.NET's JObject type under the covers (so you could use JObject instead if you prefer).
In 6.x, dynamic will also work but the actual type will be an internal JObject type. If you would prefer to work with Json.NET's JObject type, you can hook up Json.NET as the serializer using the NEST.JsonNetSerializer nuget package, to use as the serializer for your documents and then use its JObject type as per 5.x.
(Feels strange to answer my own question but I want to show the resulting code for future reference.)
var settings = new ConnectionSettings(new Uri(#"http://localnhost:9200"))
.DefaultIndex("myindex");
var client = new ElasticClient(settings);
var res = client.Search<dynamic>(s => s
.AllTypes());
var rows = res.Documents;
Assert.IsTrue(rows.Count >= 1);
dynamic row = res.Documents.First();
Assert.AreEqual("50.7031526", row.POSITION.lat.ToString()); // It is case sensitive.
Assert.AreEqual(50.7031526, (double)row.POSITION.lat); // Convert to type explicitly.

Lowercase Document Type during Nest search of Elasticsearch cluster

I'm searching a v5.5 elasticsearch cluster that's hosted using AWS's managed solution. I'm using a client to send search requests to the cluster but it's not finding any hits. I switched on the cluster lever logging and can see that the problem is that the type being searched is in lowercase (when the document types in the index are uppercase) so it can't match on any documents.
I'm passing a search descriptor object into the Nest client:
GetSearchDescriptor(SearchDescriptor<T> descriptor)
{
descriptor.Index(index)
.Type(documentType)
.Query(q => (q
.Bool(bq => bq
...
}
client.Search<T>(s => GetSearchDescriptor(s))
Where documentType is of type T e.g. Invoice.
When I look at the CloudWatch logs I can see that the request that's hitting the cluster is lowercase invoice (instead of uppercase Invoice). This doesn't match the document type hence doesn't show any results. When I do a kibana search using the exact json in the logs I get the correct results when using uppercase Invoice, but no results when using lowercase invoice.
Any ideas what could be going on here?
NEST's default inference for a type name from a POCO of type T, is to lowercase the typeof(T).Name value. You can easily change this behaviour for T or for all POCOs on ConnectionSettings
For all POCOs
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultTypeNameInferrer(type => type.Name.ToUpperInvariant());
var client = new ElasticClient(settings);
For only T e.g. Invoice
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.InferMappingFor<Invoice>(m => m
.TypeName("INVOICE")
);
var client = new ElasticClient(settings);
With the latter, you can also use InferMappingFor<T> to specify a default index name for T, and tell the client which property should be used to infer the id for the document.

In Nest (Elasticsearch), how can I get the raw json mapping of an index?

I want to check the discrepancies between my current mapping (as in my C# code) and the mapping in the elasticsearch index.
With only:
var res = esClient.GetMapping<EsCompany>();
I get GetMappingResponse object in c#, I will have to compare field by field for equality. Even worse, each field has their own properties, I have to descend into those properties for further comparison.
In my application, I prefer obtaining the raw json of the mapping, and I can easily diff two json objects for equality.
I then tried this:
var res = esClient.Raw.IndicesGetMapping(myIndexName);
But when I read res.Response, I get an AmbiguousMatchException exception.
When you connect to Elasticsearch you can choose to expose the raw response like this:
var client = new ElasticClient(new ConnectionSettings().ExposeRawResponse());
Then you should be able to access the raw json via:
var json = res.ConnectionStatus.ResponseRaw;

FOSElasticaBundle order query

I am integrating FOSElasticaBundle in my Symfony 2.3 project and I need to sort the results by their price property.
Here is my code:
$finder = $this->container->get('fos_elastica.finder.website.product');
$fieldTerms = new \Elastica\Query\Terms();
$fieldTerms->setTerms('taxon_ids', $taxon_ids_array);
$boolQuery->addMust($fieldTerms);
$resultSet = $finder->find($boolQuery);
How I can do this?
Thanks
Try create a \Elastica\Query object which also contains the sorting information, then send this to the finder:
$finder = $this->container->get('fos_elastica.finder.website.product');
$fieldTerms = new \Elastica\Query\Terms();
$fieldTerms->setTerms('taxon_ids', $taxon_ids_array);
$boolQuery->addMust($fieldTerms);
$finalQuery = new \Elastica\Query($boolQuery);
$finalQuery->setSort(array('price' => array('order' => 'asc')));
$resultSet = $finder->find($finalQuery);
Have a look at the elasticsearch docs on the sort parameter to see how to use it properly.
NOTE: \Elastica\Query is quite different to \Elastica\Query\AbstractQuery, the first encapsulates everything you could send to the _search API endpoint (facets, sorting, explain, etc...) The AbstractQuery represents a base type for each of the individual query types (range, fuzzy, terms, etc...).

Return Count from Netflix oData Service When the LINQ Count() Method Doesn't Work

Is there a way to use a LINQ expression to request a Count query from the Netflix oData service in Silverlight 4?
The Netflix documentation shows that you can return counts by appending $count to a request for a collection, but a URL like this:
http://netflix.cloudapp.net/Catalog/Genres/$count
Is not generated from an expression like this:
var count = (from g in catalog.Genres select g).Count();
The above code returns an error saying that the Count method is not supported. Is there a way to do this in LINQ, or do I just need to make WebClient request to get the value?
Count and LongCount are not supported in Silverligth because they require a synchornous execution of the query. Since Silverlight requires all network operations to by asynchronous this is not possible.
You can either issue the HTTP query in question programatically not using DataServiceContext (or related classes), since the $count returns a text representation of the number, parsing the response is not that hard.
Or you can use a bit of a trick. You can use IncludeTotalCount() to add $inlinecount=allpages query option to the query which will include the count in the response. Then to not download all the entities from server, you can use Take(0) which will add $top=0 and thus return empty result set. But the inline count will still contain the right number.
You can access the inline count on the QueryOperationResponse.TotalCount property.
Something like this:
NetflixCatalog ctx = new NetflixCatalog(new Uri("http://netflix.cloudapp.net/Catalog"));
var q = (DataServiceQuery<Genre>)ctx.Genres.IncludeTotalCount().Take(0);
q.BeginExecute((ar) =>
{
QueryOperationResponse<Genre> r = (QueryOperationResponse<Genre>)q.EndExecute(ar);
r.TotalCount.ToString(); // Use the count in whatever way you need
}, null);
It works in LinqPad 4 using C# 4.0
var count = (from g in Genres select g).Count();
count.Dump();
Result: 518
In LinqPad 2 using C# 3.0 the error appears.

Resources