I'm developing an app for iOS and Android with Xamarin 6.1 and I'm using Xamarin.Forms 2.3.1 The app scans a QR using ZXing.Net.Mobile.Forms 2.1.4 that contains a Guid Id and saves it as a string into my ElasticSearch. To connect with ElasticSearch I'm using NEST 2.x
The problem is that when I scan the same QR again (when I know for sure that it is already indexed) it is detected as a new one even tho the values are the same (both compared as strings). However I tried to remove the dashes ( - ) from the id before storing or comparing them and it works.
This is my model:
public class Box
{
[String(Analyzer = "null")]
public string id { get; set; }
public string lastUpdate { get; set; }
}
result.Text is what I get from the QR and I know for sure that is a string, and this is how I index it:
scannedQR = result.Text;
// INDEXING
var timeStamp = GetTimestamp(DateTime.Now);
var customBox = new Box {
id= scannedQR,
lastUpdate = timeStamp
};
var res = client.Index(customBox, p => p
.Index("airstorage")
.Type("boxes")
.Id(scannedQR)
.Refresh()
);
And this is how I check if the QR already exists:
var resSearch = client.Search<Box>(s => s
.Index("airstorage")
.Type("boxes")
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.id, scannedQR)
)
);
if (resSearch.Documents.Count() > 0) {
Console.WriteLine("EXISTING");
}
else {
Console.WriteLine("NEW BOX");
}
I have also tried to set the property to .NotAnalyse to the index in ElasticSearch when creating it like suggested in here but still doesn't work.
Anyone any idea? Everything is welcome!
Thank you!
I would set the id field on your Box POCO to not analyzed
public class Box
{
[String(Index = FieldIndexOption.NotAnalyzed)]
public string id { get; set; }
public string lastUpdate { get; set; }
}
The id property will still be indexed but will not undergo analysis, so will be indexed verbatim.
I would also check for the existence of the document using
var existsResponse = client.DocumentExists<Box>("document-id", e => e.Index("airstorage").Type("boxes"));
if (!existsResponse.Exists)
{
Console.WriteLine("document exists")
}
BUT, actually what I think you want is to use optimistic concurrency control for the creation document in the index call i.e. if the document doesn't exist then index it, but if it does exist then return a 409 Conflict invalid response. The OpType.Create can be used for this
var indexResponse = client.Index(box, i => i
.OpType(OpType.Create)
.Index("airstorage")
.Type("boxes"));
if (!indexResponse.IsValid)
{
if (indexResponse.ApiCall.HttpStatusCode == 409)
{
Console.WriteLine("document exists");
}
else
{
Console.WriteLine($"error indexing document: {indexResponse.DebugInformation}");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With