コード例 #1
0
        public AdminUser _GetById(int id)
        {
            AdminUser user = null;

            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/"))
                               .RequestTimeout(TimeSpan.FromMinutes(2));

                var lowlevelClient = new ElasticLowLevelClient(settings);
                var searchResponse = lowlevelClient.Search <string>("user", "guest", new
                {
                    query = new
                    {
                        match = new
                        {
                            Id = id
                        }
                    }
                });

                bool successful = searchResponse.Success;

                if (!successful)
                {
                    return(user);
                }

                var responseJson = searchResponse.Body;

                ESearchRoot <User> root = JsonHelper.JSONStringObject <ESearchRoot <User> >(responseJson);

                HitsItem <User> item = root.hits.hits[0];
                if (item != null && item._source != null)
                {
                    user = new AdminUser
                    {
                        Id       = item._source.Id,
                        RealName = item._source.RealName,
                        Account  = item._source.Account,
                        Email    = item._source.Email,
                        Phone    = item._source.Phone,
                        //IsUse=item._source.IsUse,
                        Avatar      = item._source.Avatar,
                        Description = item._source.Description,
                        CreateTime  = item._source.CreateTime,
                        ModifyTime  = item._source.ModifyTime
                    };
                }
            }
            catch (Exception ex)
            {
            }
            return(user);
        }
コード例 #2
0
        public List <AdminUser> GetBySomeWhere(string keyword, int limit, int pageSize, out int total)
        {
            List <AdminUser> users = new List <AdminUser>();

            total = 0;
            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/"))
                               .RequestTimeout(TimeSpan.FromMinutes(2));

                var lowlevelClient = new ElasticLowLevelClient(settings);

                //根据不同的参数 来构建不同的查询条件
                var request = new object();
                if (!String.IsNullOrEmpty(keyword))
                {
                    request = new
                    {
                        from  = limit,
                        size  = pageSize,
                        query = new
                        {
                            match = new
                            {
                                Description = keyword
                            }
                        },
                        highlight = new
                        {
                            fields = new
                            {
                                Description = new { }
                            }
                        },
                        sort = new
                        {
                            Id = new
                            {
                                order = "desc"
                            }
                        }
                    };
                }
                else
                {
                    request = new
                    {
                        from  = limit,
                        size  = pageSize,
                        query = new
                        {
                            match_all = new
                            {
                            }
                        },
                        highlight = new
                        {
                            fields = new
                            {
                                Description = new { }
                            }
                        },
                        sort = new
                        {
                            Id = new
                            {
                                order = "desc"
                            }
                        }
                    };
                }


                var searchResponse = lowlevelClient.Search <string>("user", "guest", request);

                bool successful   = searchResponse.Success;
                var  responseJson = searchResponse.Body;

                if (!successful)
                {
                    return(users);
                }

                ESearchRoot <User> root = JsonHelper.JSONStringObject <ESearchRoot <User> >(responseJson);
                if (root != null)
                {
                    total = root.hits.total;
                    foreach (HitsItem <User> item in root.hits.hits)
                    {
                        if (item._source != null)
                        {
                            string        highlightDescription = String.Empty;
                            StringBuilder sbDs = new StringBuilder();
                            if (item.highlight != null && item.highlight.Description.Count > 0)
                            {
                                //ighlightDescription = item.highlight.Description[0];
                                foreach (var d in item.highlight.Description)
                                {
                                    sbDs.Append(d);
                                }
                                highlightDescription = sbDs.ToString();
                            }

                            AdminUser user = new AdminUser
                            {
                                Id       = item._source.Id,
                                RealName = item._source.RealName,
                                Account  = item._source.Account,
                                Email    = item._source.Email,
                                Phone    = item._source.Phone,
                                //IsUse=item._source.IsUse,
                                Avatar               = item._source.Avatar,
                                Description          = item._source.Description,
                                HighlightDescription = highlightDescription,
                                CreateTime           = item._source.CreateTime,
                                ModifyTime           = item._source.ModifyTime
                            };
                            users.Add(user);
                        }
                    }
                }

                return(users);
            }
            catch (ElasticsearchClientException ex)
            {
                //Log4Helper.Error
            }
            return(users);
        }