Example #1
0
        public async Task <IActionResult> GetNewsAsync([FromHeader] String source,
                                                       [FromRoute] SiteIdRoute route)
        {
            var response = new Response <NewsResponse>();

            try {
                var request = new GetDescriptor <WebNewsDoc>(_IWebNewsElastic.IndexName, route.id);
                // 排除返回字段
                request.SourceExcludes(a => new { a.DisplayType });
                // 只获取元数据
                var result = await this._IWebNewsElastic.Client
                             .GetAsync <NewsResponse>(request);

                if (result.ApiCall.Success && result.ApiCall.HttpStatusCode == 200)
                {
                    if (!string.IsNullOrEmpty(result.Source.NewsId))
                    {
                        response.Code = true;
                        response.Data = result.Source;
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex) {
                response.SetError(ex, this._ILogger);
            }
            return(response.ToHttpResponse());
        }
        /// <summary>
        /// 根据id查询单条数据(知道具体索引)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TPrimaryKeyType"></typeparam>
        /// <param name="id"></param>
        /// <param name="indexName"></param>
        /// <param name="routing"></param>
        /// <returns></returns>
        public async Task <T> GetAsync <T, TPrimaryKeyType>(QueryModel <TPrimaryKeyType> queryModel) where T : class
        {
            string indexName = queryModel.IndexName?.ToLower();

            if (!string.IsNullOrEmpty(indexName))
            {
                GetDescriptor <T> descriptor = new GetDescriptor <T>(indexName, queryModel.Id.ToString());;
                if (!string.IsNullOrEmpty(queryModel.Routing))
                {
                    descriptor.Routing(queryModel.Routing);
                }
                var response = await client.GetAsync <T>(descriptor).ConfigureAwait(false);

                if (response.Found)
                {
                    return(response.Source);
                }
                return(default(T));
            }
            else
            {
                //不知道indexName 另外一种方式查询
                return(await GetAsync <T, TPrimaryKeyType>(queryModel.Id, queryModel.Routing));
            }
        }
Example #3
0
        public string CreateGetPath <T>(GetDescriptor <T> d) where T : class
        {
            var index = d._Index ?? this.Infer.IndexName <T>();
            var type  = d._Type ?? this.Infer.TypeName <T>();
            var id    = d._Id;

            id.ThrowIfNullOrEmpty("id");

            var path      = "/{0}/{1}/{2}".EscapedFormat(index, type.Resolve(this._connectionSettings), id);
            var urlParams = new Dictionary <string, string>();

            if (d._Refresh.HasValue)
            {
                urlParams.Add("refresh", d._Refresh.Value.ToString().ToLower());
            }
            if (d._Realtime.HasValue)
            {
                urlParams.Add("realtime", d._Realtime.Value.ToString().ToLower());
            }
            if (!d._Preference.IsNullOrEmpty())
            {
                urlParams.Add("preference", d._Preference);
            }
            if (!d._Routing.IsNullOrEmpty())
            {
                urlParams.Add("routing", d._Routing);
            }
            if (d._Fields.HasAny())
            {
                urlParams.Add("fields", string.Join(",", d._Fields));
            }

            return(path + this.ToQueryString(urlParams));
        }
Example #4
0
        public void SimpleGetPath()
        {
            var pr = new PathResolver(Settings);
            var d  = new GetDescriptor <ElasticSearchProject>()
                     .Id(1);
            var expected = "/nest_test_data/elasticsearchprojects/1";
            var path     = pr.CreateGetPath(d);

            Assert.AreEqual(expected, path);
        }
Example #5
0
        public void SimpleGetPath()
        {
            var pr = new PathResolver(Settings);
            var d = new GetDescriptor<ElasticSearchProject>()
                .Id(1);
            var expected = "/nest_test_data/elasticsearchprojects/1";
            var path = pr.CreateGetPath(d);

            Assert.AreEqual(expected, path);
        }
Example #6
0
        public void ComplexGetPath()
        {
            var pr = new PathResolver(Settings);
            var d  = new GetDescriptor <ElasticSearchProject>()
                     .Index("newindex")
                     .Type("myothertype")
                     .Refresh()
                     .Routing("routing")
                     .ExecuteOnPrimary()
                     .Id(1);
            var expected = "/newindex/myothertype/1?refresh=true&preference=_primary&routing=routing";
            var path     = pr.CreateGetPath(d);

            Assert.AreEqual(expected, path, path);
        }
Example #7
0
        public void ComplexGetPath()
        {
            var pr = new PathResolver(Settings);
            var d = new GetDescriptor<ElasticSearchProject>()
                .Index("newindex")
                .Type("myothertype")
                .Refresh()
                .Routing("routing")
                .ExecuteOnPrimary()
                .Id(1);
            var expected = "/newindex/myothertype/1?refresh=true&preference=_primary&routing=routing";
            var path = pr.CreateGetPath(d);

            Assert.AreEqual(expected, path, path);
        }
 protected IGetRequest ReadFluent(string id, GetDescriptor <Project> d) => d;
 protected virtual GetDescriptor <T> BuildQuery(GetDescriptor <T> descriptor)
 {
     return(descriptor);
 }
Example #10
0
 protected override GetDescriptor <T> BuildQuery(GetDescriptor <T> descriptor)
 {
     return(descriptor.Id(_id));
 }
Example #11
0
 protected abstract GetDescriptor <T> BuildQuery(GetDescriptor <T> descriptor);