/// <summary> /// Get species observation in Json representation. /// </summary> /// <param name="speciesObservation">The species observation.</param> /// <param name="context">Web service context.</param> /// <returns>Specified species observation field.</returns> public static String GetJson(this WebSpeciesObservation speciesObservation, WebServiceContext context) { StringBuilder json; json = new StringBuilder(); if (speciesObservation.IsNotNull() && speciesObservation.Fields.IsNotEmpty()) { json.Append(@"{ "); foreach (WebSpeciesObservationField field in speciesObservation.Fields) { GetFieldJson(context, json, field); } GetSpecialFieldsJson(json, speciesObservation.Fields); json.Append(@" }"); } return(json.ToString()); }
/// <summary> /// Check that all species observation fields has been /// mapped in Elasticsearch. /// </summary> /// <param name="context"> Web service request context.</param> /// <param name="speciesObservation">Species observation.</param> /// <param name="elasticsearch">Proxy to Elasticsearch.</param> public void CheckMappingElasticsearch(WebServiceContext context, WebSpeciesObservation speciesObservation, ElasticsearchSpeciesObservationProxy elasticsearch) { Boolean isMappingUpdated; Dictionary <String, WebSpeciesObservationField> mapping; String cacheKey, fieldName, indexType, newMapping; isMappingUpdated = false; if (speciesObservation.IsNotNull() && speciesObservation.Fields.IsNotEmpty()) { mapping = GetMapping(context, elasticsearch); foreach (WebSpeciesObservationField field in speciesObservation.Fields) { fieldName = field.GetFieldName(); if (!mapping.ContainsKey(fieldName)) { // Add mapping for new field. switch (field.Type) { case WebDataType.Boolean: newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"boolean\"}" + "}}"; break; case WebDataType.DateTime: newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"date\", \"format\": \"dateOptionalTime\"}," + "\"" + fieldName + "_DatePartOnly\": {\"type\": \"string\", \"index\": \"not_analyzed\"}," + "\"" + fieldName + "_DayOfMonth\": {\"type\": \"byte\"}," + "\"" + fieldName + "_DayOfYear\": {\"type\": \"short\"}," + "\"" + fieldName + "_MonthOfYear\": {\"type\": \"byte\"}," + "\"" + fieldName + "_WeekOfYear\": {\"type\": \"byte\"}," + "\"" + fieldName + "_Year\": {\"type\": \"short\"}," + "\"" + fieldName + "_YearAndMonth\": {\"type\": \"string\", \"index\": \"not_analyzed\"}," + "\"" + fieldName + "_YearAndWeek\": {\"type\": \"string\", \"index\": \"not_analyzed\"}" + "}}"; break; case WebDataType.Float64: newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"double\"}" + "}}"; break; case WebDataType.Int32: newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"integer\"}" + "}}"; break; case WebDataType.Int64: newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"long\"}" + "}}"; break; case WebDataType.String: switch (fieldName) { case "DarwinCore_DatasetName": case "DarwinCore_Owner": case "Occurrence_RecordedBy": case "DarwinCore_ReportedBy": // Aggregations are done on these fields in AnalysisService. indexType = "not_analyzed"; break; default: indexType = "no"; break; } newMapping = "{\"properties\": {" + "\"" + fieldName + "\": {\"type\": \"string\", \"index\": \"" + indexType + "\"}," + "\"" + fieldName + "_Lowercase" + "\": {\"type\": \"string\", \"index\": \"not_analyzed\"}" + "}}"; break; default: throw new Exception("Not handled field data type = " + field.Type); } // ReSharper disable once PossibleNullReferenceException elasticsearch.UpdateSpeciesObservationMapping(newMapping); isMappingUpdated = true; } } } if (isMappingUpdated) { // Wait a while in order to make sure that // Elasticsearch has saved updated mapping. Thread.Sleep(6000); // Update cached mapping information. cacheKey = Settings.Default.SpeciesObservationFieldMappingCacheKey; context.RemoveCachedObject(cacheKey); GetMapping(context, elasticsearch); } }