Terradue.ElasticCas.Types.GenericJsonCollection.TransformElasticSearchResponseToGenericJsonCollection C# (CSharp) Method

TransformElasticSearchResponseToGenericJsonCollection() public static method

public static TransformElasticSearchResponseToGenericJsonCollection ( OpenSearchResponse response ) : GenericJsonCollection
response OpenSearchResponse
return GenericJsonCollection
        public static GenericJsonCollection TransformElasticSearchResponseToGenericJsonCollection(OpenSearchResponse<ISearchResponse<GenericJsonItem>> response)
        {
            ISearchResponse<GenericJsonItem> results = null;

            results = (ISearchResponse<GenericJsonItem>)response.GetResponseObject();

            if (results == null) {
                throw new NotImplementedException("GenericCollection only transforms from an ElasticOpenSearchResponse");
            }

            GenericJsonCollection collection = new GenericJsonCollection();
            collection.items = new List<GenericJsonItem>();

            foreach (var doc in results.Documents) {
                if (doc is GenericJsonItem) {
                    collection.items.Add(doc);
                } else
                    throw new InvalidDataException("Result is not a GenericJson document.");
            }
            collection.ShowNamespaces = true;
            collection.Date = DateTime.UtcNow;
            collection.ElementExtensions.Add(new XElement(XName.Get("totalResults", "http://a9.com/-/spec/opensearch/1.1/"), results.Total).CreateReader());

            return collection;
        }