コード例 #1
0
        public List <ES_Message> GetByQuery(ViewModels.ES_Query query, int tenantId)
        {
            var json = new JavaScriptSerializer().Serialize(query);
            var path = string.Format(base.Config.GetDocumentsByQueryPath, tenantId.ToString());

            //LogEntry.WriteNLogEntry(
            //    string.Format("ES_MessageService.GetByQuery()\n\nBasePath: {0}\nClusterName: {1}\nquery: \n{2}\ntenantId: {3}\npath: {4}", this.Config.BasePath, this.Config.ClusterName, json, tenantId, path),
            //    NLog.LogLevel.Trace,
            //    LogEvent.EventManagerElasticsearchGeneral
            //);

            var messages     = new List <ES_Message>();
            var responseJson = base.String_WebRequest(path, json, Base_Classes.ES_Verb.POST);

            if (!string.IsNullOrEmpty(responseJson))
            {
                try
                {
                    var serializer = new JavaScriptSerializer();
                    serializer.MaxJsonLength = Int32.MaxValue;
                    var messageResponse = serializer.Deserialize <ES_MessagesQueryResponse>(responseJson);
                    if (messageResponse == null)
                    {
                        throw new Exception("ES_MessageService.GetByQuery() failed to serialize response:" + "\n\n" + responseJson);
                    }

                    //LogEntry.WriteNLogEntry(
                    //    string.Format("ES_MessageService.GetByQuery() success\n\nBasePath: {0}\nClusterName: {1}\nquery: \n{2}\ntenantId: {3}\npath: {4}\ncount: {5}", this.Config.BasePath, this.Config.ClusterName, json, tenantId, path, messageResponse.hits.hits.Count()),
                    //    NLog.LogLevel.Trace,
                    //    LogEvent.EventManagerElasticsearchGeneral
                    //);

                    foreach (var doc in messageResponse.hits.hits)
                    {
                        var em = doc._source;
                        em.TenantId  = tenantId;           // TenantId is not part of the elasticsearch document
                        em.MessageId = int.Parse(doc._id); // MessageId is not part of the elasticsearch document
                        messages.Add(em);
                    }
                }
                catch (System.Exception ex1)
                {
                    //LogEntry.WriteNLogEntryEx(
                    //    "ES_MessageService.GetByQuery() Json deserialization failed on: " + path + ".\n\nResponse looked like this:\n\n" + responseJson,
                    //    NLog.LogLevel.Error,
                    //    ex1,
                    //    LogEvent.EventManagerElasticsearchConnectionError
                    //);
                    throw;
                }
            }

            return(messages);
        }
コード例 #2
0
        /// <summary>
        /// This method returns a Task.  You must use the "await" keyword within an "async" method when calling this method.
        /// </summary>
        /// <returns></returns>
        public async Task <List <ES_Message> > GetByQueryAsync(ViewModels.ES_Query query, int tenantId)
        {
            var message = await Task.Run(() => GetByQuery(query, tenantId));

            return(message);
        }