How Elasticsearch generate the unique _id value? - elasticsearch

I'm asking simply out of my favor to the look of its result string and am wondering if the algorithm has some kind of library so that I can use in my own code (C#).

See the answer to this question in the Elasticsearch discuss.
You can find the source code to the method linked there here.
Elasticsearch is written in Java, so you won't be able to import this method into your c# project.
If you don't need the specific optimizations that the Elasticsearch-Team implemented in this method, you can use the build in UUID generator of .NET. Or you can look for other UUID libraries on nuget.
If you just like the format of the Elasticsearch IDs you can also base64 encode your GUIDs/UUIDs to get similar looking IDs:
Guid g = Guid.NewGuid();
var base64GUID = System.Convert.ToBase64String(g.ToByteArray());

Related

FHIR Observation DSTU2 resource

I need some help getting the value and unit of a result from the FHIR Observation DSTU2 resource. I want to map these values to strings but it looks like Observation.value[x] can have different type of data. Any thoughts on how to do this in C#? I tried a few ways but no luck so far because the sandbox I'm using contains results as strings, Quantity and CodeableConcept.
http://hl7.org/fhir/observation-definitions.html#Observation.value_x_
For the Observation.value field you indeed have a choice of type, so the data in the FHIR resource can hold any of the choices listed for that field.
If you use the Hl7.Fhir.Dstu2 library - the official C# reference implementation available through NuGet, you can use it to easily retrieve the resources from your sandbox and get them into a POCO. Here's an example:
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
var client = new FhirClient("<your sandbox url>");
var obs = client.Read<Observation>("Observation/<technical id>");
// now you can access obs.Value regardless of the type in it
if you need to serialize the data to xml or json, you use the serializer:
using Hl7.Fhir.Serialization;
var serializer = new FhirJsonSerializer();
Console.WriteLine(serializer.SerializeToString(obs));

Convert fhir between XML and Json

Is there a way to convert a fhir bundle from json to xml by means that is independent of the FHIR version used?
I think the .net fhir api by firely can do it, but any given version of the api seems to be specific to a certain release of FHIR.
The problem is that a FHIR Bundle (or any resource) implicitly always has a version. The rationale is that each FHIR version has (or can have) a different underlying data model.
It is possible though, using the .NET FHIR API (specifically package https://www.nuget.org/packages/Hl7.Fhir.Serialization) to do the conversion with minimal version differences.
The following code does the conversion using the version-independent ISourceNode (http://docs.simplifier.net/fhirnetapi/parsing/isourcenode.html)
using Hl7.Fhir.ElementModel;
using Hl7.Fhir.Serialization;
var xml = "<Patient xmlns=\"http://hl7.org/fhir\"><identifier><use value=\"official\" /></identifier></Patient>";
var patientNode = FhirXmlNode.Parse(xml);
var typedElement = patientNode.ToTypedElement();
var json = typedElement.ToJson();
The above code has one problem though, as VS will tell you. Using ToTypedElement() without parameters is dangerous because ignoring the version is. It will work in many cases though and if it is good enough for you that may be the way to go.
A safer solution is to use the same code, but to additionally use a so-called IStructureDefinitionSummaryProvider (apologies for the naming ;) to provide the API with specific version information. Implementations for this interface can be found in version-specific API libraries, e.g. https://www.nuget.org/packages/Hl7.Fhir.R4.
using Hl7.Fhir.ElementModel;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Specification;
var xml = "<Patient xmlns=\"http://hl7.org/fhir\"><identifier><use value=\"official\" /></identifier></Patient>";
var patientNode = FhirXmlNode.Parse(xml);
var summaryProvider = new PocoStructureDefinitionSummaryProvider();
var typedElement = patientNode.ToTypedElement(summaryProvider);
var json = typedElement.ToJson();
You may be able to inject these PocoStructureDefinitionSummaryProviders based on some property of the input you are reading from. That is how we do it in the Vonk FHIR server for instance.
The FHIR java validator can do this for any version. That might be suitable depending on what you need to use it

Subquery, for lack of a better term, when using an API written in GraphQL

I'm relatively new to GraphQL so please bear with me ...
That said, I'm writing an app in node.js to push/pull data from two disparate systems, one of which has an API written in GraphQL.
For the Graph system, I have, something like, the following types defined for me:
Time {
TimeId: Int
TaskId: Int
ProjectId: Int
Project: [Project]
TimeInSeconds: Int
Timestamp: Date
}
and
Task {
TaskId: Int
TaskName: String
TaskDescription: String
}
Where Project is another type whose definition isn't important, only that it is included in the type definition as a field...
What I would like to know is if there is a way to write a query for Time in such a way that I can include the Task type's values in my results in a similar way as the values for the Project type are included in the definition?
I am using someone else's API and do not have the ability to define my own custom types. I can write my own limited queries, but I don't know if the limits are set by the devs that wrote the API or my limited ability with GraphQL.
My suspicion is that I cannot and that I will have to query both separately and combine them after the fact, but I wanted to check here just in case.
Unfortunately, unless the Time type exposes some kind of field to fetch the relevant Task, you won't be able to query for it within the same request. You can include multiple queries within a single GraphQL request; however, they are ran in parallel, which means you won't be able to use the TaskId value returned by one query as a variable used in another query.
This sort of problem is best solved by modifying the schema, but if that's not an option then unfortunately the only other option is to make each request sequentially and then combine the results client-side.

Data abstraction in API Blueprint + Aglio?

Reading the API Blueprint specification, it seems set up to allow one to specify 'Data Structures' like:
Address
street: 100 Main Str. (string) - street address
zip: 77777-7777 (string) - zip / postal code
...
Customer:
handle: mrchirpy (string)
address: (address)
And then in the model, make a reference to the data structure:
Model
[Customer][]
It seems all set up that by referencing the data structure it should generate documentation and examples in-line with the end points.
However, I can't seem to get it to work, nor can I find examples using "fully normalized data abstraction". I want to define my data structures once, and then reference everywhere. It seems like it might be a problem with the tooling, specifically I'm using aglio as the rendering agent.
It seems like all this would be top of the fold type stuff so I'm confused and wondering if I'm missing something or making the wrong assumptions about what's possible here.
#zanerock, I'm the author of Aglio. The data structure support that you mention is a part of MSON, which was recently added as a feature to API Blueprint to describe data structures / schemas. Aglio has not yet been updated to support this, but I do plan on adding the feature.

lock individual documents in elastic search using nest

I am looking for a good example to lock a document in elastic search. The below link explain how to do this in elasticsearch.
http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/concurrency-solutions.html
How can i achieve this using NEST. can someone suggest an approach to start with.
To start with, read the NEST Quick Start article. In particular, note the following:
NEST is a high level elasticsearch client that still maps very closely to the original elasticsearch API. Requests and Responses have been mapped to CLR objects and NEST also comes with a powerful strongly typed query dsl.
Consider also, that you may not require the added level of abstraction that NEST provides, and may prefer to directly utilise Elasticsearch.NET. Read also, it's own Quick Start article.
With that in mind, implementing the steps in the Solving concurrency issues article using NEST / Elasticsearch.NET is simply a matter of identifying the .NET methods that correspond to the necessary Elasticsearch API methods. The first one for example:
PUT /fs/lock/global/_create
{}
In NEST would look something like:
var createGlobalLockResponse = esClient.Index<object>(new object(), f => f
.Index("fs")
.Type("lock")
.Id("global")
.OpType(global::Elasticsearch.Net.OpType.Create));
Regarding the check of whether "this create request fails with a conflict exception", check the properties on the createGlobalLockResponse object. In particular, Created and if you're intending to get a bit more specific in the handling, ServerError.

Resources