Esempio n. 1
0
        /// <summary>
        /// Translates the search response from NEST and converts it into a view model
        /// </summary>
        /// <param name="model">search response</param>
        /// <returns>transmuted data</returns>
        public ResponseObject TransmuteEmployeeElasticResults(ISearchResponse <EmployeeElasticDto> model)
        {
            var response = new ResponseObject(ResponseType.Success, string.Empty);

            try
            {
                var results = new ElasticSearchResultsSummary();
                if (model.Hits.Any())
                {
                    List <ElasticSearchResults> elasticConsolidatedResults = new List <ElasticSearchResults>();
                    results.SearchDuration = model.Took.ToString();
                    results.Hits           = model.Documents.Count();
                    foreach (var item in model.Hits)
                    {
                        ElasticSearchResults modelToAppend = new ElasticSearchResults();
                        modelToAppend.Id    = item.Id;
                        modelToAppend.Index = item.Index;
                        modelToAppend.Score = item.Score.ToString();
                        modelToAppend.StringSearchResults = item.Source;
                        elasticConsolidatedResults.Add(modelToAppend);
                    }
                    results.SearchOutPut = elasticConsolidatedResults;
                    response.Data        = results;
                }
                else
                {
                    response.Data = results;
                }
            }
            catch (Exception e)
            {
                response = ErrorHandling.LogError(e);
            }
            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Maps the key value into a view model from the search response. the highlighted characters are with the html tags <b></b>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseObject TransmuteAndHighlightEmployeeElasticResults(ISearchResponse <EmployeeElasticDto> model)
        {
            var response = new ResponseObject(ResponseType.Success, string.Empty);

            try
            {
                var results = new ElasticSearchResultsSummary();
                if (model.Hits.Any())
                {
                    List <ElasticSearchResults> elasticConsolidatedResults = new List <ElasticSearchResults>();
                    results.SearchDuration = model.Took.ToString();
                    results.Hits           = model.Documents.Count();
                    foreach (var item in model.Hits)
                    {
                        ElasticSearchResults modelToAppend = new ElasticSearchResults();
                        modelToAppend.Id    = item.Id;
                        modelToAppend.Index = item.Index;
                        modelToAppend.Score = item.Score.ToString();
                        var mappedHighlightedData = new EmployeeElasticDto();
                        if (item.Highlight.Any())
                        {
                            foreach (var x in item.Highlight)
                            {
                                if (x.Key.Equals("address"))
                                {
                                    mappedHighlightedData.Address = x.Value.FirstOrDefault();
                                }
                                else if (x.Key.Equals("notes"))
                                {
                                    mappedHighlightedData.Notes = x.Value.FirstOrDefault();
                                }
                                else if (x.Key.Equals("fullName"))
                                {
                                    mappedHighlightedData.FullName = x.Value.FirstOrDefault();
                                }
                                else if (x.Key.Equals("position"))
                                {
                                    mappedHighlightedData.Position = x.Value.FirstOrDefault();
                                }
                                else if (x.Key.Equals("department"))
                                {
                                    mappedHighlightedData.Department = x.Value.FirstOrDefault();
                                }
                                else if (x.Key.Equals("age"))
                                {
                                    mappedHighlightedData.Age = x.Value.FirstOrDefault();
                                }
                            }
                            modelToAppend.StringSearchResults = mappedHighlightedData;
                        }
                        elasticConsolidatedResults.Add(modelToAppend);
                    }
                    results.SearchOutPut = elasticConsolidatedResults;
                    response.Data        = results;
                }
                else
                {
                    response.Data = results;
                }
            }
            catch (Exception e)
            {
                response = ErrorHandling.LogError(e);
            }
            return(response);
        }