c# - Index a dynamic object using NEST -
I am creating an API application that essentially allows a user to create a document, which he Although they want to be stored in Flexible, basically, I am providing users with a simple interface to use our elastic search examples. I am trying to keep the implementation as simple as possible. This is what I am doing so far.
Object for expected entity:
Public class DocumentModel {public string index {get; Set; } Public string type {get; Set; } Public string id {get; Set; } [ElasticProperty (Type = FieldTip.Nosted)] Public Dynamic Document {get; Set; Simple implementation: [http post] [root ("")] public IHttpActionResult post (document model document) {Uri node location = New Uri ("http: // localhost: 9200"); IConnectionPool connectionpool = new sniffing connection bridge (new list & lt; URI & gt; {node location}); Connection settings settings = new connection settings (connectionpool); Elastic client esClient = new elastic client (settings); IIndexResponse Results = esClient.Index (document, i => i.index). Type (document.Type) .id (document.Id)); Return is OK (results. ISID); }
This works fine, but it also includes the index, type and ID in the source. What I really want to do is provide information of only those three pieces when the index is in place, but in fact only the index document. Document, which is a dynamic type. But, it seems that the NEST disagrees, because it compiles an error and time in the IDE:
"An anonymous function or method group can be dynamically connected to the component value of the bound Operation "
" A lambda expression can not be used as a representative for a dynamically transmitted operation, without any representative or expression tree. Casting the car. "
How can I make my index just document.Document
? Is there a better way of handling JSON documents coming from an unknown structure than a dynamic type?
A few ways to do this.
The dynamics will not try to index the type of document, but you can index it as an object through the object of the index.
dynamic dynamic doc = new {/ * fill the document format here} Elastic client esClient = new elastic client (esSettings); IndexRequest & LT; Object & gt; Request = new index request & lt; Object & gt; (Dynamic dock) {index = "some index", type = "some type", id = "something"}; esClient.Index & lt; item & gt; (request);
< P> or if dealing with documents in bulk list> dynamic> gtk = new list> dynamic> gt; (); // populate document BulkDescriptor descriptor = new BulkDescriptor (); Vorach (Miscellaneous documents in the document) {Descriptor.index
One of the nest (or more precisely, the LolacicSearch.net) is a method method associated with the Locastics class, Which can do raw JSON by using Raw.Index (), we work like this:
String DocumentJason = Jason Convert. Serialize Object (document.); ElasticsearchResponse & Lt; string & gt; results = esClient.Raw.Index (document.Index, document.Type, document.Id, documentJson);
There is a type descriptor type for Tikri which you would expect to react (string means that you will have a serialized Jason reaction, which you can deerize and do anything else). This allows us to solve the problem of the whole object type and the document indexes the NEES exactly as expected in Alsteschurch.
Comments
Post a Comment