protected bool Exists(OneOptions options) { if (options == null) { throw new ArgumentNullException("options"); } options.Fields.Add("id"); var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter <T>()).Size(1); var elasticSearchOptions = options as ElasticSearchOptions <T>; if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { searchDescriptor.Indices(elasticSearchOptions.Indices); foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } return(_elasticClient.Search <T>(searchDescriptor).HitsMetaData.Total > 0); }
protected TModel FindOneAs <TModel>(OneOptions options) where TModel : class, new() { if (options == null) { throw new ArgumentNullException("options"); } TModel result = null; if (EnableCache) { if (options.UseCache) { result = Cache.Get <TModel>(GetScopedCacheKey(options.CacheKey)); } if (options.UseCache && result != null) { Log.Trace().Message("Cache hit: type={0}", _entityType).Write(); } else if (options.UseCache) { Log.Trace().Message("Cache miss: type={0}", _entityType).Write(); } if (result != null) { return(result); } } var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter <T>()).Size(1); if (options.Fields.Count > 0) { searchDescriptor.Source(s => s.Include(options.Fields.ToArray())); } else { searchDescriptor.Source(s => s.Exclude("idx")); } var elasticSearchOptions = options as ElasticSearchOptions <T>; if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { searchDescriptor.Indices(elasticSearchOptions.Indices); foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } _elasticClient.EnableTrace(); var item = _elasticClient.Search <T>(searchDescriptor).Documents.FirstOrDefault(); _elasticClient.DisableTrace(); if (typeof(T) != typeof(TModel)) { if (Mapper.FindTypeMapFor <T, TModel>() == null) { Mapper.CreateMap <T, TModel>(); } result = Mapper.Map <T, TModel>(item); } else { result = item as TModel; } if (EnableCache && result != null && options.UseCache) { Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()); } return(result); }