Unable to insert dynamic object to Elastic Search using NEST - elasticsearch

I am creating a dynamic object. I assign the values via IDictionary. Add the collections of the IDictionary to the object. Then I add the dynamic object to Elastic Search using NEST code. It throws me stackoverflow exception."An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
Here is what I have tried.
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node,defaultIndex: "test-index");
var client = new ElasticClient(settings);
try
{
dynamic x = new ExpandoObject();
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("NewProp", "test1");
dic.Add("NewProp3", "test1");
x = dic;
var index3 = client.Index(x);
}
catch (Exception ex)
{
string j = ex.StackTrace;
}
I need to create an index in ElasticSearch, using a dynamic object, because I will be having an excel work book consisting of over 300 worksheet, and each and every sheet will be named as type, and the contents inside the worksheet will be the _source.
In the above example 'x' the dynamic object created is the name of the worksheet, and the values added into the dictionary are the rows and columns of excel sheet.
Where am I going wrong.
Regards,
Hema

I belive you can skip ExpandoObject and just index Dictionary<string, object>().
var dictionary = new Dictionary<string, object>();
dictionary.Add("NewProp", "test1");
dictionary.Add("NewProp3", "test1");
client.Index(dictionary);

Related

Store a headerFilter with a persistent layout in Tabulator 3.5

I try to store my filter field into JSON object during the navigation process and get this Javascript issue in tabulator.js:
Options Error - Tabulator does not allow options to be set after initialization unless there is a function defined for that purpose
(java class)
JsonObject configuracion = new JsonObject();
configuracion.addProperty("height", "92%");
configuracion.addProperty("tooltips", true);
configuracion.addProperty("tooltipsHeader", true);
configuracion.addProperty("persistentLayout", true);
configuracion.addProperty("layout","fitDataFill");
configuracion.addProperty("persistenceMode", true);
configuracion.addProperty("persistenceID", myBandeja.getStrBandejaLogicaCaption().substring(0,4));
configuracion.addProperty("persistentFilter", true);
configuracion.addProperty("movableColumns", true);
configuracion.addProperty("selectable", true);
configuracion.addProperty("virtualDomBuffer",500);
JsonObject fila = new JsonObject();
JsonArray datasets = new JsonArray();
for (Entry<Integer, String> entry : myTitleRow.entrySet()) {
fila = new JsonObject();
fila.addProperty("title", entry.getValue());
fila.addProperty("field", entry.getValue());
fila.addProperty("sorter", "string");
fila.addProperty("headerFilter", "input");
fila.addProperty("headerFilterPlaceholder", "Filtrar...");
fila.addProperty("cellClick", "cellClickBandeja(e, cell);");
datasets.add(fila);
}
configuracion.add("columns", datasets);
Some property is sorting wrong?
Thanks in advance.
When you get that message it is either either one of two things.
You are trying to change an option after the Tabulator has been created using jQuery's option function which Tabulator does not allow.
Or because you are trying to re declare your table on an element that is already a Tabulator. If that is the case then you need to call the destroy function on the table before you create it again:
$("#example-table").tabulator("destroy");

Elastic search update fails after including null values

We recently upgraded to elastic search v5 and nest v5.6
We are trying to set a field to null, however, the default serialization settings are ignoring null values.
var pool = new SingleNodeConnectionPool(new Uri("http://local:9200"));
var connectionSettings =
new ConnectionSettings(pool)
.DisableDirectStreaming();
var elasticClient = new ElasticClient(connectionSettings);
var indexName = "myIndexName";
var typeName = "myTypeName";
var documentId = 2;
var pendingDescriptor = new BulkDescriptor();
pendingDescriptor.Index(indexName).Type(typeName);
var pendingUpdate = new Dictionary<string, object>
{
{ $"DocumentType_TAG_PENDING_Id", null }
};
var updateRequest = new UpdateRequest<dynamic, dynamic>(indexName, typeName, new Id(documentId));
updateRequest.Doc = pendingUpdate;
elasticClient.Update<dynamic>(updateRequest);
This results in the request:
{"update":{"_id":2,"_retry_on_conflict":3}}
{"doc":{}}
The field value isn't set to null
I tried to modify the serializer to include null values after reading here https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/modifying-default-serializer.html
var connectionSettings =
new ConnectionSettings(pool, connection, new SerializerFactory((settings, values) =>
{
settings.NullValueHandling = NullValueHandling.Include;
}));
Now my request becomes:
{"update":{"_index":null,"_type":null,"_id":2,"_version":null,"_version_type":null,"_routing":null,"_parent":null,"_timestamp":null,"_ttl":null,"_retry_on_conflict":3}}
{"doc":{"DocumentType_TAG_PENDING_Id":null},"upsert":null,"doc_as_upsert":null,"script":null,"scripted_upsert":null}
And I get the following error:
{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Current token (VALUE_NULL) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#181f5854; line: 1, column: 82]"}],"type":"json_parse_exception","reason":"Current token (VALUE_NULL) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput#181f5854; line: 1, column: 82]"},"status":500}
Please help
So far, we have two options:
Upgrade to v6, where they have separated document and request serializers.
So we can customize the way our documents are serialized without affecting the request/response headers. For more info see https://www.elastic.co/guide/en/elasticsearch/client/net-api/master/custom-serialization.html
Use the elastic search low-level client with post request avoid the nest serializer.
https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/elasticsearch-net.html
Our preferred way will be to go with the upgrade if everything works and revert to low-level client if any issues

saving & updating full json document with Spring data MongoTemplate

I'm using Spring data MongoTemplate to manage mongo operations. I'm trying to save & update json full documents (using String.class in java).
Example:
String content = "{MyId": "1","code":"UG","variables":[1,2,3,4,5]}";
String updatedContent = "{MyId": "1","code":"XX","variables":[6,7,8,9,10]}";
I know that I can update code & variables independently using:
Query query = new Query(where("MyId").is("1"));
Update update1 = new Update().set("code", "XX");
getMongoTemplate().upsert(query, update1, collectionId);
Update update2 = new Update().set("variables", "[6,7,8,9,10]");
getMongoTemplate().upsert(query, update2, collectionId);
But due to our application architecture, it could be more useful for us to directly replace the full object. As I know:
getMongoTemplate().save(content,collectionId)
getMongoTemplate().save(updatedContent,collectionId)
implements saveOrUpdate functionality, but this creates two objects, do not update anything.
I'm missing something? Any approach? Thanks
You can use Following Code :
Query query = new Query();
query.addCriteria(Criteria.where("MyId").is("1"));
Update update = new Update();
Iterator<String> iterator = json.keys();
while(iterator.hasNext()) {
String key = iterator.next();
if(!key.equals("MyId")) {
Object value = json.get(key);
update.set(key, value);
}
}
mongoTemplate.updateFirst(query, update, entityClass);
There may be some other way to get keyset from json, you can use according to your convenience.
You can use BasicDbObject to get keyset.
you can get BasicDbObject using mongoTemplate.getConverter().

Create a custum Sharepoint list and add items problems

Im new to SharePoint programming, or programming at all for that case. My problem is that I get a error-message when I try to go to my sharepoint site after I debugged this oode (it worked fine in visual studio). I don't know whats wrong? The error message i get is:
"Server Error in '/' Application.
0x80004005Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb."
I've read that you got to allow safe updates? but is that the case? I think maybe it is my code that is not right... Please help :)
// choose your site
SPSite site = new SPSite("http://....");
SPWeb web = SPContext.Current.Web;
//Add a list to the choosen site
SPListCollection lists = web.Lists;
// create new Generic list called "OrdersList"
lists.Add("OrdersTest", "All of my testorders will be here", SPListTemplateType.GenericList);
//Add the new list to the website
SPList newList = web.Lists["OrdersTest"];
// create Number type new column called "OrderID"
newList.Fields.Add("OrderID", SPFieldType.Number, true);
// create Number type new column called "OrderNumber"
newList.Fields.Add("OrderNumber", SPFieldType.Number, true);
// create Text type new column called "OrderProducts"
newList.Fields.Add("OrderProducts", SPFieldType.Text, true);
newList.Update();
//Create a object to that list
SPListItem newOrder = newList.AddItem();
//Add a new item
newOrder["OrderID"] = 1;
newOrder["OrderNumber"] = 1;
newOrder["OrderProducts"] = "Icecream";
newOrder.Update();
// create new Generic list called "ProductTest"
lists.Add("ProductTest", "All of my testproducts will be here", SPListTemplateType.GenericList);
//Add the new list to the website
SPList newList2 = web.Lists["ProductTest"];
// create Number type new column called "ProductID"
newList2.Fields.Add("ProductID", SPFieldType.Number, true);
// create Text type new column called "ProductName"
newList2.Fields.Add("ProductName", SPFieldType.Text, true);
// create Number type new column called "OrderID"
newList2.Fields.Add("ProductPrice", SPFieldType.Number, true);
//Create a object to that list
SPListItem nyProduct = newList2.AddItem();
//Add a new item
nyProduct["ProductID"] = 1;
nyProduct["ProductName"] = "Icecream";
nyProduct["ProductPrice"] = 13;
nyProduct.Update();
You need to set AllowUnsafeUpdates to true for updating data in get requests.
using(SPSite site = new SPSite("http://...."))
using (SPWeb web = site.OpenWeb())
{
bool b = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
try
{
//your code
//create and update list
}
catch (Exception ex)
{
//handles errors
}
finally
{
web.AllowUnsafeUpdates = b;
}
}

Retrieving related entities using RetrieveMultipleRequest

I have an entity called Invoice and an entity called InvoiceItem.
There is a one to many relationship called new_invoice_invoiceitem.
There is a LookupAttribute in InvoiceItem called new_parent_invoice_invoiceitem.
I am trying to retrieve the InvoiceItems that are related to the Invoice with a particular ID using the following code:
QueryExpression query = new QueryExpression();
query.EntityName = "new_invoiceitem";
query.ColumnSet = new AllColumns();
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "new_parent_invoice_invoiceitem";
condition.Values = new object [] { new Guid("fe1009cc-e034-49d5-bc59-ab4c3091a6f9") };
condition.Operator = ConditionOperator.Equal;
FilterExpression filter = new FilterExpression();
filter.AddCondition(condition);
query.Criteria = filter;
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
BusinessEntityCollection bec = response.BusinessEntityCollection;
The code runs without errors but the BusinessEntityCollection is always empty even though there are records in Dynamics.
Any idea what I'm doing wrong?
Thanks,
David
Try setting request.ReturnDynamicEntities = true

Resources