Пример #1
0
        public BulkDeleteResponse BulkDelete(QueryRequest request)
        {
            var url         = string.Format("{0}/{1}/_delete_by_query", _baseUrl, _index);
            var requestText = request.GenerateQuery();

            if (logger.IsDebugEnabled)
            {
                logger.Debug("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
                logger.Debug(string.Format("Delete Request {0} Started:{1}Query:{2}", request.UniqueIdentifer, DateTime.Now.ToShortTimeString(), request.ToString()));
            }
            try
            {
                var responseJson            = PostAsync(url, requestText).Result;
                BulkDeleteResponse response = JsonConvert.DeserializeObject <BulkDeleteResponse>(responseJson);
                response.RequestIdentifier = request.UniqueIdentifer;

                if (logger.IsDebugEnabled)
                {
                    logger.Debug(string.Format("Delete Request {0} completed: {1}; {2} Records deleted", request.UniqueIdentifer, DateTime.Now.ToShortTimeString(), response.Deleted));
                    logger.Debug("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
                }
                return(response);
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("{0} -{1}- Failed. {2}", "DoSearch", request.UniqueIdentifer, ex.Message));
                throw;
            }
        }
Пример #2
0
        public SearchResult DoSearch(QueryRequest request)
        {
            var searchresult = new SearchResult(request.UniqueIdentifer);
            var requestText  = request.GenerateQuery();
            var url          = string.Format("{0}/{1}/_search", _baseUrl, _index);

            if (logger.IsDebugEnabled)
            {
                logger.Debug(string.Format("Search Request Started - {0} -Time:{1} {2}", request.UniqueIdentifer, DateTime.Now.ToShortTimeString(), request.ToString()));
            }

            string responseJson;

            try
            {
                responseJson = PostAsync(url, requestText).Result;
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("{0} -{1}- Failed. {2}", "DoSearch", request.UniqueIdentifer, ex.Message));
                throw;
            }
            JObject awsSearch = JObject.Parse(responseJson);

            searchresult.Took       = (int)awsSearch["took"];
            searchresult.TimedOut   = (bool)awsSearch["timed_out"];
            searchresult.MatchCount = (int)awsSearch["hits"]["total"];

            List <JToken> hits       = awsSearch["hits"]["hits"].Children().ToList();
            var           properties = typeof(SearchResultItem).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanWrite);

            foreach (var token in hits)
            {
                var           resultItem = token.ToObject <SearchResultItem>();
                List <JToken> fields     = token["_source"].Children().ToList();
                foreach (JToken s in fields)
                {
                    JValue jValue = ((JProperty)s).Value as JValue;
                    var    name   = ((JProperty)s).Name;
                    object value  = jValue != null ? jValue.Value : null;
                    resultItem.AddSource(new SearchItem()
                    {
                        Name = name, Value = value
                    });

                    //if has a value then set the property (if present)
                    if (value != null)
                    {
                        //set the result items property
                        var prop = properties.FirstOrDefault(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                        if (prop != null)
                        {
                            Type propType = prop.PropertyType;
                            if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                            {  //get underyling type
                                propType = Nullable.GetUnderlyingType(propType);
                            }
                            prop.SetValue(resultItem, Convert.ChangeType(value, propType), null);
                        }
                    }
                }
                searchresult.AddSearchResultItem(resultItem);
            }
            if (logger.IsDebugEnabled)
            {
                logger.Debug(string.Format("Search {0} Complete: Time: {1})", searchresult.UniqueIdentifier, DateTime.Now.ToShortTimeString()));
            }



            return(searchresult);
        }