protected async Task <T> FindOneAsync(OneOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (EnableCache && options.UseCache)
            {
                var cacheValue = await Cache.GetAsync <T>(GetScopedCacheKey(options.CacheKey)).AnyContext();

#if DEBUG
                Logger.Trace().Message("Cache {0}: type={1}", cacheValue.HasValue ? "hit" : "miss", _entityType).Write();
#endif
                if (cacheValue.HasValue)
                {
                    return(cacheValue.Value);
                }
            }

            var searchDescriptor = new SearchDescriptor <T>().Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes)).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>;
            searchDescriptor.Indices(elasticSearchOptions != null && elasticSearchOptions.Indices.Any() ? elasticSearchOptions.Indices.ToArray() : GetIndices());

            if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0)
            {
                foreach (var sort in elasticSearchOptions.SortBy)
                {
                    searchDescriptor.Sort(sort);
                }
            }

#if DEBUG
            _elasticClient.EnableTrace();
            var sw = Stopwatch.StartNew();
#endif
            var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext();

#if DEBUG
            sw.Stop();
            _elasticClient.DisableTrace();
            Logger.Trace().Message($"FindOneAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write();
#endif
            var result = results.Documents.FirstOrDefault();
            if (EnableCache && options.UseCache && result != null)
            {
                await Cache.SetAsync(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()).AnyContext();
            }

            return(result);
        }
        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 async Task <IDictionary <string, long> > SimpleAggregationAsync(ElasticSearchOptions <T> options, Expression <Func <T, object> > fieldExpression)
        {
            var searchDescriptor = new SearchDescriptor <T>()
                                   .Query(f => f.Filtered(s => s.Filter(f2 => options.GetElasticSearchFilter(_supportsSoftDeletes))))
                                   .Aggregations(a => a.Terms("simple", sel => sel.Field(fieldExpression).Size(10)));

            searchDescriptor.Indices(options.Indices.Any()
                ? options.Indices.ToArray()
                : GetIndices());
#if DEBUG
            _elasticClient.EnableTrace();
            var sw = Stopwatch.StartNew();
#endif
            var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext();

#if DEBUG
            sw.Stop();
            _elasticClient.DisableTrace();
            Logger.Trace().Message($"SimpleAggregationAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write();
#endif

            return(results.Aggs.Terms("simple").Items.ToDictionary(ar => ar.Key, ar => ar.DocCount));
        }
        protected async Task <bool> ExistsAsync(OneOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.Fields.Add("id");
            var searchDescriptor = new SearchDescriptor <T>().Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes)).Size(1);

            var elasticSearchOptions = options as ElasticSearchOptions <T>;

            searchDescriptor.Indices(elasticSearchOptions != null && elasticSearchOptions.Indices.Any()
                ? elasticSearchOptions.Indices.ToArray()
                : GetIndices());
            if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0)
            {
                foreach (var sort in elasticSearchOptions.SortBy)
                {
                    searchDescriptor.Sort(sort);
                }
            }

#if DEBUG
            _elasticClient.EnableTrace();
            var sw = Stopwatch.StartNew();
#endif
            var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext();

#if DEBUG
            sw.Stop();
            _elasticClient.DisableTrace();
            Logger.Trace().Message($"ExistsAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write();
#endif

            return(results.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);
        }
        protected ICollection <TModel> FindAs <TModel>(ElasticSearchOptions <T> options) where TModel : class, new()
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            ICollection <TModel> result;

            if (EnableCache && options.UseCache)
            {
                result = Cache.Get <ICollection <TModel> >(GetScopedCacheKey(options.CacheKey));
                if (result != null)
                {
                    return(result);
                }
            }

            var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter());

            searchDescriptor.Indices(options.Indices);
            searchDescriptor.IgnoreUnavailable();

            if (options.UsePaging)
            {
                searchDescriptor.Skip(options.GetSkip());
            }
            searchDescriptor.Size(options.GetLimit());
            searchDescriptor.Type(typeof(T));
            if (options.Fields.Count > 0)
            {
                searchDescriptor.Source(s => s.Include(options.Fields.ToArray()));
            }
            else
            {
                searchDescriptor.Source(s => s.Exclude("idx"));
            }
            if (options.SortBy.Count > 0)
            {
                foreach (var sort in options.SortBy)
                {
                    searchDescriptor.Sort(sort);
                }
            }

            _elasticClient.EnableTrace();
            var results = _elasticClient.Search <T>(searchDescriptor);

            _elasticClient.DisableTrace();

            if (!results.IsValid)
            {
                throw new ApplicationException(String.Format("ElasticSearch error code \"{0}\".", results.ConnectionStatus.HttpStatusCode), results.ConnectionStatus.OriginalException);
            }

            options.HasMore = options.UseLimit && results.Total > options.GetLimit();

            var items = results.Documents.ToList();

            if (typeof(T) != typeof(TModel))
            {
                if (Mapper.FindTypeMapFor <T, TModel>() == null)
                {
                    Mapper.CreateMap <T, TModel>();
                }

                result = Enumerable.ToList(items.Select(Mapper.Map <T, TModel>));
            }
            else
            {
                result = items as List <TModel>;
            }

            if (EnableCache && options.UseCache)
            {
                Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate());
            }

            return(result);
        }
Пример #7
0
        public SearchResult <Suggestion> QuickSearch(string q, SearchParameters parameters)
        {
            int limit = parameters.Limit == 0 ? 10 : parameters.Limit;
            int from  = (parameters.PageNumber - 1) * limit;

            SearchDescriptor <object> descriptor = new SearchDescriptor <object>();

            descriptor = descriptor.From(from).Size(limit);

            IList <FilterContainer> filters = new List <FilterContainer>();

            FilterDescriptor <object> baseFilter = new FilterDescriptor <object>();

            filters.Add(baseFilter.Term("accountID", parameters.AccountId));
            if (parameters != null && parameters.IsPrivateSearch && parameters.PrivateModules.IsAny() &&
                (parameters.PrivateModules.Contains(Entities.AppModules.Campaigns) || parameters.PrivateModules.Contains(Entities.AppModules.Forms)))
            {
                filters.Add(baseFilter.Term("ownerId", parameters.DocumentOwnerId.ToString()));
                //if(parameters.PrivateModules.Count() > 0 &&
                //parameters.PrivateModules.Contains(Entities.AppModules.Campaigns) )
                //    filters.Add(baseFilter.Term("status", 109));
            }

            if (filters.Count > 1)
            {
                descriptor = descriptor.Filter(f => f.And(filters.ToArray()));
            }
            else
            {
                descriptor = descriptor.Filter(f => f.Term("accountID", parameters.AccountId));
            }
            //descriptor = descriptor.Filter(f => f.Term("accountID", parameters.AccountId));

            string[] selectedIndices = null;
            Type[]   types           = null;

            if (parameters != null && parameters.Types.IsAny())
            {
                selectedIndices = parameters.Types.Where(t => indices.ContainsKey(t)).Select(t => indices[t]).Distinct().ToArray();
                types           = parameters.Types.Distinct().ToArray();
            }
            else
            {
                selectedIndices = indices.Select(c => c.Value).Distinct().ToArray();
                types           = indices.Select(c => c.Key).Distinct().ToArray();
            }

            descriptor = descriptor.Indices(selectedIndices).IndicesBoost(b => b
                                                                          .Add("contacts" + this.accountId, 3.5).Add("campaigns" + this.accountId, 3.0).Add("opportunities", 2.5));

            descriptor = descriptor.Types(types);
            if (!string.IsNullOrEmpty(q))
            {
                string queryString = q + "*" + " " + q;
                if (parameters != null && parameters.PrivateModules.IsAny())
                {
                    string createdByTerm      = string.Empty;
                    string createdByTermValue = string.Empty;
                    string ownerIdTerm        = string.Empty;
                    string ownerIdTermValue   = string.Empty;
                    if (parameters.PrivateModules.Contains(Entities.AppModules.Campaigns) || parameters.PrivateModules.Contains(Entities.AppModules.Forms))
                    {
                        createdByTerm      = "createdBy";
                        createdByTermValue = parameters.DocumentOwnerId.ToString();
                    }


                    if (parameters.PrivateModules.Contains(Entities.AppModules.Contacts) || parameters.PrivateModules.Contains(Entities.AppModules.Opportunity))
                    {
                        ownerIdTerm      = "ownerId";
                        ownerIdTermValue = parameters.DocumentOwnerId.ToString();
                    }

                    IList <string> contactIndices = new List <string>();
                    if (parameters.PrivateModules.Contains(Entities.AppModules.Contacts))
                    {
                        contactIndices.Add("contacts" + this.accountId);
                    }
                    //if (parameters.PrivateModules.Contains(Entities.AppModules.Opportunity))  commented by Ram on 2nd May 2018 CR NEXG-3001
                    //    contactIndices.Add("opportunities");  commented by Ram on 2nd May 2018 CR NEXG-3001
                    //if (parameters.PrivateModules.Contains(Entities.AppModules.Campaigns)) commented by Ram on 2nd May 2018 CR NEXG-3001
                    //    contactIndices.Add("campaigns" + this.accountId);  commented by Ram on 2nd May 2018 CR NEXG-3001
                    if (parameters.PrivateModules.Contains(Entities.AppModules.Forms))
                    {
                        contactIndices.Add("forms");
                    }

                    //descriptor = descriptor.Query(qr=>qr.Filtered(flt=>flt.Query(qs=>qs.in)))
                    // descriptor = descriptor.Query(qu => qu.Indices(i => i.Indices(contactIndices)
                    //.NoMatchQuery(nm => nm.Term(createdByTerm, createdByTermValue)).NoMatchQuery(nm => nm.Term(ownerIdTerm, ownerIdTermValue)).Query(qr => qr.QueryString(qs => qs.Query(q)))));//Term(ownerIdTerm, ownerIdTermValue)

                    //modified

                    descriptor = descriptor.Query(qu =>
                                                  qu.Indices(i => i.Indices(contactIndices).
                                                             NoMatchQuery(nm => nm.QueryString(qs => qs.Query(queryString))).
                                                             Query(que => que.Bool(b => b.Must(
                                                                                       s => s.Bool(bo => bo.Should(sh => sh.Term(createdByTerm, createdByTermValue), sh => sh.Term(ownerIdTerm, ownerIdTermValue))),
                                                                                       s => s.QueryString(qs => qs.Query(queryString).OnFieldsWithBoost(o => o.Add("firstName", 5).Add("lastName", 5).Add("emails.emailId", 5).Add("companyName", 3.5).Add("_all", 2))))))));
                }
                else
                {
                    descriptor = descriptor.Query(query => query.QueryString(qs => qs.Query(queryString).OnFieldsWithBoost(o => o.Add("firstName", 5).Add("lastName", 5).Add("emails.emailId", 5).Add("companyName", 3.5).Add("_all", 2))));
                }
            }

            /*
             * Don't show archived campaigns, search by account id, don't show unsubscribed email
             * */
            descriptor = descriptor.Filter(f => f.And(query => !query.Term("campaignStatus", CampaignStatus.Archive),
                                                      query => query.Term("accountID", parameters.AccountId)));
            var result = this.ElasticClient().Search <object>(body => descriptor);

            string qe = result.ConnectionStatus.ToString();

            Logger.Current.Verbose(qe);

            var searchResult = convertToSuggestions(result.Documents, parameters);

            searchResult.TotalHits = result.Total;

            return(searchResult);
        }
        protected ICollection <TModel> FindAs <TModel>(ElasticSearchOptions <T> options) where TModel : class, new()
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            ICollection <TModel> result = null;

            if (options.UseCache)
            {
                result = Cache.Get <ICollection <TModel> >(GetScopedCacheKey(options.CacheKey));
            }

            if (result != null)
            {
                return(result);
            }

            var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter());

            searchDescriptor.Indices(options.Indices);

            if (options.UsePaging)
            {
                searchDescriptor.Skip(options.GetSkip());
            }
            searchDescriptor.Size(options.GetLimit());
            searchDescriptor.Type(typeof(T));
            if (options.Fields.Count > 0)
            {
                searchDescriptor.Source(s => s.Include(options.Fields.ToArray()));
            }
            if (options.SortBy.Count > 0)
            {
                foreach (var sort in options.SortBy)
                {
                    searchDescriptor.Sort(sort);
                }
            }

            var results = _elasticClient.Search <T>(searchDescriptor);

            options.HasMore = options.UseLimit && results.Total > options.GetLimit();

            var items = results.Documents.ToList();

            if (typeof(T) != typeof(TModel))
            {
                if (Mapper.FindTypeMapFor <T, TModel>() == null)
                {
                    Mapper.CreateMap <T, TModel>();
                }

                result = items.Select(Mapper.Map <T, TModel>).ToList();
            }
            else
            {
                result = items as List <TModel>;
            }

            if (options.UseCache)
            {
                Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate());
            }

            return(result);
        }
        protected async Task <FindResults <T> > FindAsync(ElasticSearchOptions <T> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (EnableCache && options.UseCache)
            {
                var cacheValue = await Cache.GetAsync <FindResults <T> >(GetScopedCacheKey(options.CacheKey)).AnyContext();

#if DEBUG
                Logger.Trace().Message("Cache {0}: type={1}", cacheValue.HasValue ? "hit" : "miss", _entityType).Write();
#endif
                if (cacheValue.HasValue)
                {
                    return(cacheValue.Value);
                }
            }

            var searchDescriptor = new SearchDescriptor <T>();
            searchDescriptor.Query(options.GetElasticSearchQuery(_supportsSoftDeletes));
            searchDescriptor.Indices(options.Indices.Any() ? options.Indices.ToArray() : GetIndices());
            searchDescriptor.IgnoreUnavailable();
            searchDescriptor.Size(options.GetLimit());
            searchDescriptor.Type(typeof(T));

            if (options.UsePaging)
            {
                searchDescriptor.Skip(options.GetSkip());
            }

            if (options.Fields.Count > 0)
            {
                searchDescriptor.Source(s => s.Include(options.Fields.ToArray()));
            }
            else
            {
                searchDescriptor.Source(s => s.Exclude("idx"));
            }

            if (options.SortBy.Count > 0)
            {
                foreach (var sort in options.SortBy)
                {
                    searchDescriptor.Sort(sort);
                }
            }
#if DEBUG
            _elasticClient.EnableTrace();
            var sw = Stopwatch.StartNew();
#endif
            var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext();

#if DEBUG
            sw.Stop();
            _elasticClient.DisableTrace();
            Logger.Trace().Message($"FindAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write();
#endif

            if (!results.IsValid)
            {
                throw new ApplicationException($"ElasticSearch error code \"{results.ConnectionStatus.HttpStatusCode}\".", results.ConnectionStatus.OriginalException);
            }

            options.HasMore = options.UseLimit && results.Total > options.GetLimit();

            var result = new FindResults <T> {
                Documents = results.Documents.ToList(),
                Total     = results.Total
            };

            if (EnableCache && options.UseCache)
            {
                await Cache.SetAsync(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()).AnyContext();
            }

            return(result);
        }