So my goal is to use ElasticSearch, ES, as a log. To be more specific, i want to upload basically just a timestamp from when my application last ran. The uploading works fine but i cannot figure out how to fetch the data from the index. I've tried both using the Query and Aggregetion but in neither of cases have I managed to get some data. I get a response that says :
Valid NEST response built from a low level call on POST: /lastrun/lastrun/_search.
I have also tried searching for solutions but cannot manage to find anything that works for me. Can anyone help me fetch the data?
The index name is 'lastrun' and the class I upload to the index is called LastRun.
The Logger class
public static Boolean WriteLastRun()
{
var response = Elastic.Index(new LastRun { Date = DateTime.Now });
return response.IsValid ? true : false;
}
public static DateTime ReadLastRun()
{
var SearchResponse = Elastic.Search<LastRun>(s => s
.Query(q => q.MatchAll())
.Index("lastrun"));
Console.WriteLine(SearchResponse.Documents);
return new DateTime();
}
The LastRun class I upload to ES.
public class LastRun
{
public DateTime Date { get; set; }
}
Thanks!
EDIT
Settings for the Elastic:
var settings = new ConnectionSettings(new Uri("http://localhost:9200/")).DefaultIndex('lastrun');
ElasticClient Elastic = new ElasticClient(settings);
EDIT 2
I can verify that the same index is being uploaded to and searched by this code and by checking the same index in kibana.
var resolver = new IndexNameResolver(settings);
var index = resolver.Resolve<LastRun>();
Console.WriteLine(index); //prints 'lastrun'
Turns out that there wasn't a problem from the beginning. The Search method worked fine and I had the wrong ideá of accessing the doc in the respons.
This is what is did, which was wrong:
Console.WriteLine(SearchResponse.Documents);
And here the right way to do it:
foreach (var item in SearchResponse.Documents)
{
Console.WriteLine(item.Date)
}
Related
I have a problem with downloading CosmosDb data, even when doing it like in the tutorial.
So in the beginning, my CosmosDb looks like this:
I tried to simply add a new class:
public class CaseModel
{
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("vin")]
public string Vin { get; set; }
}
and then just do like it is mentioned in the documentation
using (FeedIterator<Case> iterator = collection.GetItemLinqQueryable<Case>(true).ToFeedIterator())
{
while (iterator.HasMoreResults)
{
foreach (var item in await iterator.ReadNextAsync())
{
var x = item;
}
}
}
This way, the code iterates over many elements (like it is working),
but the properties are always null - as if the mapping would not work:
Then I tried something like this
using (FeedIterator<Case> feedIterator = collection.GetItemQueryIterator<Case>(
"select * from cases",
null,
new QueryRequestOptions() { PartitionKey = new PartitionKey("shardedKey") }))
{
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
var x = item;
}
}
}
But this query returns no results.
I have no idea what is wrong.
Lately I was working with CosmosDb on Azure some year ago, and was doing some similar things.
The only thing that I think is strange, that the elements are marked as 'documents'
In the end, my code which should work looks like this
var dbClient = new CosmosClient(info.ConnectionString);
var db = dbClient.GetDatabase(info.DatabaseName);
var collection = db.GetContainer(info.Collection);
using (FeedIterator<CaseModel> iterator = collection.GetItemLinqQueryable<CaseModel>(true)
.ToFeedIterator())
{
while (iterator.HasMoreResults)
{
foreach (var item in await iterator.ReadNextAsync())
{
var x = item;
}
}
}
In the debug windows, I see that 3 steps at the beginning (like connect with connection string, then get database then get-container) work.
You are mixing APIs. The SDK you are referencing to (Microsoft.Azure.Cosmos) is the SQL API SDK: https://learn.microsoft.com/azure/cosmos-db/sql/sql-api-sdk-dotnet-standard
The screenshot in your question is from a Mongo API account.
Either you use a SQL API account with that SDK or you use the C# Mongo driver to interact with your Mongo API account.
SQL API accounts use id as the property for Ids/document identifier, not _id.
Since collection.InsertOne(document) returns void how do i know that the document written to the database for sure? I have a function which need to be run exactly after document is written to the database.
How can I check that without running a new query?
"Since collection.InsertOne(document) returns void" - is wrong, see db.collection.insertOne():
Returns: A document containing:
A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled.
A field insertedId with the _id value of the inserted document.
So, run
ret = db.collection.insertOne({your document})
print(ret.acknowledged);
or
print(ret.insertedId);
to get directly the _id of inserted document.
The write concern can be configured on either the connection string or the MongoClientSettings which are both passed in to the MongoClient object on creation.
var client = new MongoClient(new MongoClientSettings
{
WriteConcern = WriteConcern.W1
});
More information on write concern can be found on the MongoDB documentation - https://docs.mongodb.com/manual/reference/write-concern/
If the document is not saved the C# Driver will throw an exception (MongoWriteException).
Also if you have any write concern > Acknowledged, you'll also get back the Id of the document you've just save.
var client = new MongoClient(new MongoClientSettings
{
WriteConcern = WriteConcern.W1
});
var db = client.GetDatabase("test");
var orders = db.GetCollection<Order>("orders");
var newOrder = new Order {Name = $"Order-{Guid.NewGuid()}"};
await orders.InsertOneAsync(newOrder);
Console.WriteLine($"Order Id: {newOrder.Id}");
// Output
// Order Id: 5f058d599f1f033f3507c368
public class Order
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
So...i created a Sharepoint Add-in (C# MVC) to get list information and create/update items. I've done the creating/updating in the past, not gonna tackle that now.
My problem here is displaying the list items into the MVC View. What i've done so far ->
I created a model (class) with the information that i'll show in the table:
public class IRFItem
{
public string Title { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
//public string CreatedBy { get; set; }
}
In the same file (for the sake of keeping my tests compact) i also added a way to get the items i need:
public static List<IRFItem> GetItems(SharePointContext spContext, CamlQuery camlQuery)
{
List<IRFItem> items = new List<IRFItem>();
//var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
if (clientContext != null)
{
List irfList = clientContext.Web.Lists.GetByTitle("IRF");
ListItemCollection irfListItems = irfList.GetItems(camlQuery);
clientContext.Load(irfListItems);
clientContext.ExecuteQuery();
if (irfListItems != null)
{
foreach (var irfListItem in irfListItems)
{
items.Add(
new IRFItem
{
Title = irfListItem["Title"].ToString(),
StartDate = irfListItem["StartDate"].ToString(),
EndDate = irfListItem["EndDate"].ToString(),
});
}
}
else
{
items.Add(
new IRFItem
{
Title = "Empty",
StartDate = "Empty",
EndDate = "Empty"
});
}
}
}
return items;
}
In my custom controller (called SharepointController so i dont mess up with the default ones) i added this ->
// GET: Sharepoint
[SharePointContextFilter]
public ActionResult Index()
{
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
ViewBag.Username = SharePointService.GetUserName(spContext);
CamlQuery queryProducts = new CamlQuery();
queryProducts.ViewXml =
#"<View>
<ViewFields><FieldRef Name='StartDate' /><FieldRef Name='LinkTitle' /><FieldRef Name='Title' /><FieldRef Name='Author' /><FieldRef Name='EndDate' /><FieldRef Name='ID' /></ViewFields>
</View>";
List<IRFItem> items = SharePointService.GetItems(spContext, queryProducts);
//SharePointService.GetItems(spContext, queryProducts);
return View(items);
}
And finally my desired view contains->
#foreach (AddinTest1Web.Models.IRFItem irfItem in Model)
{
<tr>
<td>#irfItem.Title</td>
<td>#irfItem.StartDate</td>
<td>#irfItem.EndDate</td>
</tr>
}
I get no errors, but my table is always empty...I even added that else part in GetItems to create an item that shows empty just so i know if its a sharepoint related problem or something else. My MVC experience isn't much to be honest (just started learning it a week ago, but im the kind of guy that learns by doing).
Does anyone see any problems here? I've followed a tutorial for this and made my own little changes.
Ref: https://www.chakkaradeep.com/2013/10/18/building-apps-for-sharepoint-with-mvc/
Any kind of tip will be highly appreciated, thx.
EDIT: I jumped over the error by giving the application more permissions (to list & web just to be safe) and i am getting back results, however i am unable to create the items because executeQuery does not finish on time. Any idea how to delay that? I remember i had a bigggg problem with tasks in the past so i have no idea where to start here.
Couple of things I see from debugging.
I would move the code GetItems() to separate Helper class and then put a breakpoint in it to see if it is firing.
If you are not getting any error and return data is always empty, then that means that some part of the code is not getting executed (Sync or async issue?)
Ok so i fixed the problem (been fighting with this for hours and it was so damn simple i wanna punch myself right now).
In case anyone encounters this ->
Why my list didnt get any items (even tho no erorrs) -> Not enough permissions given to the app. Fixed by giving full list permissions and full web permissions.
Why my foreach was giving an error -> Apparently one column had a different name. I figured that out by trying this code (which apparently is better cause it throws an error, unlike getting everything from the list) ->
clientContext.Load(irfListitems,
itemss => itemss.Include(
item => item["Title"],
item => item["StartDate"],
item => item["EndDate"]
));
I am using Parse in a mobile application I am working on Xamarin/C#. I am trying to query a table by the updatedAt field so that I can reduce the amount of data calls being made by my application. I am querying my Parse DB with the most recent "updatedAt" date in my local SQlite DB. The only issue is Parse is returning all items within that table. Here is my function:-
public static async Task getNewLiveTips(Action<bool> callback)
{
DateTime lastDate = App.Current.modelManager.GetOnlyLocalTip().updatedAt;
if (lastDate != null) {
var query = new ParseQuery<ParseTip>();
query.WhereGreaterThanOrEqualTo("updatedAt", lastDate);
IEnumerable<ParseTip> parseTips = await query.FindAsync();
foreach (var tip in parseTips)
{
Log.Debug(TAG, "Adding new updated live tip item");
App.Current.modelManager.SaveLocalTip(ModelUtils.parseToLocalTip((ParseTip)tip));
}
}
callback(true);
}
I don't do any manipulation of dates anywhere so the date from my local SQLite DB looks like this:-
06/09/2016 12:50:02
The dates returned are:-
06/09/2016 15:14:23
17/08/2016 21:12:31
As you can see, one of the dates is more recent and one of the dates is older. Can anyone spot my issue?
Thanks
Didn't manage to figure out why this function didn't work but I managed to get the same results doing:-
private static async Task getAllLiveTips()
{
var query = new ParseQuery<ParseTip>().OrderByDescending("updatedAt").Limit(5);
IEnumerable<ParseTip> parseTips = await query.FindAsync();
if (parseTips != null)
{
foreach (var liveTip in parseTips)
{
Log.Debug(TAG, "Adding live tip item");
App.Current.modelManager.SaveLocalTip(ModelUtils.parseToLocalTip(liveTip));
}
}
}
We are having an issue with searching a custom record through SuiteTalk. Below is a sample of what we are calling. The issue we are having is in trying to set up the search using the internalId of the record. The issue here lies in in our initial development account the internal id of this custom record is 482 but when we deployed it through the our bundle the record was assigned with the internal Id of 314. It would stand to reason that this internal id is not static in a site per site install so we wondered what property to set up to reference the custom record. When we made the record we assigned its “scriptId’ to be 'customrecord_myCustomRecord' but through suitetalk we do not have a “scriptId”. What is the best way for us to allow for this code to work in all environments and not a specific one? And if so, could you give an example of how it might be used.
Code (C#) that we are attempting to make the call from. We are using the 2013.2 endpoints at this time.
private SearchResult NetSuite_getPackageContentsCustomRecord(string sParentRef)
{
List<object> PackageSearchResults = new List<object>();
CustomRecord custRec = new CustomRecord();
CustomRecordSearch customRecordSearch = new CustomRecordSearch();
SearchMultiSelectCustomField searchFilter1 = new SearchMultiSelectCustomField();
searchFilter1.internalId = "customrecord_myCustomRecord_sublist";
searchFilter1.#operator = SearchMultiSelectFieldOperator.anyOf;
searchFilter1.operatorSpecified = true;
ListOrRecordRef lRecordRef = new ListOrRecordRef();
lRecordRef.internalId = sParentRef;
searchFilter1.searchValue = new ListOrRecordRef[] { lRecordRef };
CustomRecordSearchBasic customRecordBasic = new CustomRecordSearchBasic();
customRecordBasic.recType = new RecordRef();
customRecordBasic.recType.internalId = "314"; // "482"; //THIS LINE IS GIVING US THE TROUBLE
//customRecordBasic.recType.name = "customrecord_myCustomRecord";
customRecordBasic.customFieldList = new SearchCustomField[] { searchFilter1 };
customRecordSearch.basic = customRecordBasic;
// Search for the customer entity
SearchResult results = _service.search(customRecordSearch);
return results;
}
I searched all over for a solution to avoid hardcoding internalId's. Even NetSuite support failed to give me a solution. Finally I stumbled upon a solution in NetSuite's knowledgebase, getCustomizationId.
This returns the internalId, scriptId and name for all customRecord's (or customRecordType's in NetSuite terms! Which is what made it hard to find.)
public string GetCustomizationId(string scriptId)
{
// Perform getCustomizationId on custom record type
CustomizationType ct = new CustomizationType();
ct.getCustomizationTypeSpecified = true;
ct.getCustomizationType = GetCustomizationType.customRecordType;
// Retrieve active custom record type IDs. The includeInactives param is set to false.
GetCustomizationIdResult getCustIdResult = _service.getCustomizationId(ct, false);
foreach (var customizationRef in getCustIdResult.customizationRefList)
{
if (customizationRef.scriptId == scriptId) return customizationRef.internalId;
}
return null;
}
you can make the internalid as an external property so that you can change it according to environment.
The internalId will be changed only when you install first time into an environment. when you deploy it into that environment, the internalid will not change with the future deployments unless you choose Add/Rename option during deployment.