Is it mandatory to create POCO objects for all Indexes I intend to search with ElasticSearch?
Is it mandatory to create POCO objects for all Indexes I intend to search with ElasticSearch?
I'm starting with ElasticSearch.NET (trying Nest first).
A very basic question: all the search API methods I see (search, get, etc) require specifying a .NET type.
Isn't there a way to specify an index name so the API infers the response type automatically ? In other words, is it mandatory to create POCO objects for all Indexes we intend to search ? (I understand from the documentation that ElasticSearch can infer a document type from an index by using the structure of the first document...)
I have some experience with Nest, from what I remember (I am not next to my computer now) you have to declare a poco class in order to know fields you will get in return from your request. In other words, so the anonymous methods (which Nest uses) would know which fields to invoke when they are creating the Json. When you declare a request it works with a generic type -Although you can use a general one, but I think you can't really determine like this what fields you will get back - if it not so clear, write me here and I will explain better later on(:
– Green
Jul 1 at 15:04
@Green: Thanks for taking the time to drop a line in all my elastic search posts :)
– Veverke
Jul 2 at 7:54
1 Answer
1
Isn't there a way to specify an index name so the API infers the response type automatically ?
Not currently. We've previously discussed doing something like this based on index patterns, which would be useful to support covariant responses across multiple indices when types are completely removed in the future.
In other words, is it mandatory to create POCO objects for all Indexes we intend to search ?
No it's not mandatory. You can specify any type you desire for TDocument in IElasticClient.Search<TDocument> and the type will be used to
TDocument
IElasticClient.Search<TDocument>
_source
Thanks for helping. What I did not get from your reply is you say it is not mandatory but I still have to define TDocuments in order to perform the search. Why is this not making mandatory to create POCO objects ? Will not this TDocument be my POCO object to map the document type ?
– Veverke
Jul 2 at 7:53
It's a generic method, so a type still needs to be provided for
TDocument, but you don't need to create a specific POCO for this (although I would recommend doing so to work with NEST). You could use e.g. object or dynamic, or if hooking up JsonNetSerializer from NEST.JsonNetSerializer package, JObject.– Russ Cam
Jul 2 at 9:20
TDocument
object
dynamic
JsonNetSerializer
NEST.JsonNetSerializer
JObject
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I believe the answer is no with Nest's high level wrapper. Seems this was its whole purpose: to provide a strongly typed wrapper to interact with the database. Using the low level api, though, one does not need to provide a POCO .net target type.
– Veverke
Jul 1 at 11:00