public QueryContainer GetQuery() { string field = LoadFieldHelper.GetFiledStr <T, TV>(this._expression); if (typeof(TV) == typeof(DateTime)) { var rangQuery = new DateRangeQuery { Field = field, GreaterThanOrEqualTo = this._greaterThanOrEqualTo.HasValue ? DateTime.Parse(this._greaterThanOrEqualTo.Value.ToString()) : default(DateTime?), GreaterThan = this._greaterThan.HasValue ? DateTime.Parse(this._greaterThan.Value.ToString()) : default(DateTime?), LessThan = this._lessThan.HasValue ? DateTime.Parse(this._lessThan.Value.ToString()) : default(DateTime?), LessThanOrEqualTo = this._lessThanOrEqualTo.HasValue ? DateTime.Parse(this._lessThanOrEqualTo.Value.ToString()) : default(DateTime?), TimeZone = "Asia/Shanghai" }; return(rangQuery); } return(new NumericRangeQuery { Field = field, GreaterThanOrEqualTo = this._greaterThanOrEqualTo.HasValue ? double.Parse(this._greaterThanOrEqualTo.Value.ToString()) : default(double?), GreaterThan = this._greaterThan.HasValue ? double.Parse(this._greaterThan.Value.ToString()) : default(double?), LessThan = this._lessThan.HasValue ? double.Parse(this._lessThan.Value.ToString()) : default(double?), LessThanOrEqualTo = this._lessThanOrEqualTo.HasValue ? double.Parse(this._lessThanOrEqualTo.Value.ToString()) : default(double?) }); }
public QueryContainer GetQuery() { string field = LoadFieldHelper.GetFiledStr <T, TV>(this._expression); return(new TermsQuery { Field = field, Terms = this._values.Select(x => (object)x) }); }
/// <summary> /// 结构化查询 /// </summary> /// <typeparam name="T">查询模型类型</typeparam> /// <typeparam name="TR">查询结果模型类型</typeparam> /// <param name="searchCondition">查询条件</param> /// <returns></returns> public IEnumerable <TR> ExactValueSearch <T, TR>(IExactValueSearchCondition <T, TR> searchCondition) where T : SearchEntityBase where TR : class { var indexMappingName = IndexNameHelper.GetIndexMappingName(SetupConfig.SetupConfig.ServiceSign, searchCondition.Index); var status = this.CacheClient.GetCache <IndexOperateStatus>(indexMappingName); this.IndexOperate.CkeckIndexStatus <T>(status, indexMappingName); if (searchCondition.Selector == null) { throw new ElasticsearchException("index查询异常:未设置查询结果类型转换表达式"); } var queryContainer = searchCondition.QueryPredicate == null ? new MatchAllQuery() : searchCondition.QueryPredicate.GetQuery(); var properties = LoadFieldHelper.GetProperty(searchCondition.Selector); var sourefilter = new SourceFilter() { Includes = properties }; var searchRequest = new SearchRequest <T>(indexMappingName); searchRequest.Query = new ConstantScoreQuery() { Filter = queryContainer }; searchRequest.Source = new Union <bool, ISourceFilter>(sourefilter); if (searchCondition.Sort != null && searchCondition.Sort.Count() > 0) { searchRequest.Sort = searchCondition.Sort.Select(x => (ISort) new SortField { Field = x.Field, Order = x.isAsc ? SortOrder.Ascending : SortOrder.Descending }).ToList(); } searchRequest.Size = searchCondition.SizeNumber; searchRequest.From = searchCondition.SkipNumber; var response = this.Client.Search <T>(searchRequest); if (!response.IsValid) { throw new ElasticsearchException("index查询异常:" + response.OriginalException.Message); } return(response.Hits.Select(x => x.Source).Select(searchCondition.Selector.Compile())); }
/// <summary> /// 对某个字段正向排序 /// </summary> /// <typeparam name="TKey">正向排序字段类型</typeparam> /// <param name="expression">lamda表达式</param> /// <returns></returns> public IExactValueSearchCondition <T> OrderBy <TKey>(Expression <Func <T, TKey> > expression) { if (this.Sort == default(List <FieldSort>)) { this.Sort = new List <FieldSort>(); } string field = LoadFieldHelper.GetFiledStr <T, TKey>(expression); this.Sort.Add(new FieldSort { Field = field, isAsc = true }); return(this); }
/// <summary> /// 设置查询条件 /// </summary> /// <param name="value">查询的值</param> /// <param name="searchKeyExpressions">查询字段表达式</param> /// <returns></returns> public IFullTextSearchCondition <T> Where(string value, params Expression <Func <T, string> >[] searchKeyExpressions) { this.SearchFields = searchKeyExpressions.Select(x => LoadFieldHelper.GetFiledStr <T, string>(x)).ToArray(); this.SearchValue = value; return(this); }
public QueryContainer GetQuery() { string field = LoadFieldHelper.GetFiledStr <T, TV>(this._expression); return(new QueryContainerDescriptor <T>().Term(field, this._value)); }
/// <summary> /// 全文检索 /// </summary> /// <typeparam name="T">查询模型类型</typeparam> /// <typeparam name="TR">查询结果模型类型</typeparam> /// <param name="condition">查询条件</param> /// <returns></returns> public IEnumerable <TR> FullTextSearch <T, TR>(IFullTextSearchCondition <T, TR> condition) where T : SearchEntityBase where TR : class { var indexMappingName = IndexNameHelper.GetIndexMappingName(SetupConfig.SetupConfig.ServiceSign, condition.Index); var status = this.CacheClient.GetCache <IndexOperateStatus>(indexMappingName); this.IndexOperate.CkeckIndexStatus <T>(status, indexMappingName); if (condition.SearchFields == null || condition.SearchFields.Length == 0) { throw new ElasticsearchException("全文检索失败:查询字段不能为空"); } if (condition.Selector == null) { throw new ElasticsearchException("全文检索失败:未设置查询结果类型转换表达式"); } var searchRequest = new SearchRequest <T>(indexMappingName); searchRequest.Size = condition.SizeNumber; searchRequest.From = condition.SkipNumber; var boolQuery = new BoolQuery(); List <QueryContainer> queryContainers = new List <QueryContainer>(); if (condition.SearchFields.Length > 1) { queryContainers.Add(new MultiMatchQuery { Fields = condition.SearchFields, Query = condition.SearchValue }); } else { queryContainers.Add(new MatchQuery { Field = condition.SearchFields[0], Query = condition.SearchValue }); } boolQuery.Must = queryContainers; if (condition.FilterPredicate != null) { boolQuery.Filter = new List <QueryContainer> { condition.FilterPredicate.GetQuery() }; } searchRequest.Query = boolQuery; if (condition.Sort != null && condition.Sort.Count() > 0) { searchRequest.Sort = condition.Sort.Select(x => (ISort) new SortField { Field = x.Field, Order = x.isAsc ? SortOrder.Ascending : SortOrder.Descending }).ToList(); } var properties = LoadFieldHelper.GetProperty(condition.Selector); var sourefilter = new SourceFilter() { Includes = properties }; searchRequest.Source = new Union <bool, ISourceFilter>(sourefilter); var highlight = new Highlight(); var dic = new Dictionary <Field, IHighlightField>(); foreach (var field in condition.SearchFields) { dic.Add(field, new HighlightField()); } highlight.Fields = dic; searchRequest.Highlight = highlight; var response = this.Client.Search <T>(searchRequest); if (!response.IsValid) { throw new ElasticsearchException("全文检索失败:" + response.OriginalException.Message); } if (condition.IsHighLight) { return(response.Hits.Select(x => this.SetHightValue(x.Source, x.Highlights)).Select(condition.Selector.Compile())); } return(response.Hits.Select(x => x.Source).Select(condition.Selector.Compile())); }