Пример #1
0
        public bool IsDocumentCreatedBy(int contactId, int?userId)
        {
            SearchDescriptor <T> descriptor = new SearchDescriptor <T>();
            IList <Type>         types      = new List <Type>()
            {
                typeof(T)
            };

            string[] selectedIndices = indices.Where(i => types.Contains(i.Key)).Select(i => i.Value).Distinct().ToArray();
            descriptor = descriptor.Types(types).Indices(selectedIndices);
            descriptor = descriptor.Filter(f => f.And(a => a.Term("id", contactId), a => a.Term("createdBy", userId)));

            var result = this.ElasticClient().Search <T>(body => descriptor);

            return(result.Total > 0);
        }
Пример #2
0
        public IEnumerable <T> SearchSuppressionList(string q, SearchParameters p)
        {
            ISearchResponse <T> result;

            SearchDescriptor <T> descriptor = new SearchDescriptor <T>();
            string        Field             = string.Empty;
            List <string> Indices           = new List <string>();
            Type          type = p.Types.FirstOrDefault();

            if (p != null && p.Types.FirstOrDefault() != null && type.Equals(typeof(SuppressedEmail)))
            {
                Field = "email";
                Indices.Add("suppressedemails1");
                if (p.AccountId != 1)
                {
                    Indices.Add("suppressedemails" + p.AccountId);
                }
            }
            else if (p != null && p.Types.FirstOrDefault() != null && type.Equals(typeof(SuppressedDomain)))
            {
                Field = "domain";
                Indices.Add("suppresseddomains1");
                if (p.AccountId != 1)
                {
                    Indices.Add("suppresseddomains" + p.AccountId);
                }
            }
            string regexValue = ".*" + q + ".*";

            if (!string.IsNullOrEmpty(q))
            {
                regexValue = ".*" + q.ToLower() + ".*";
            }
            FilterContainer query = new FilterContainer();
            FilterDescriptor <SuppressionList> filterDesc = new FilterDescriptor <SuppressionList>();

            query      = filterDesc.Regexp(rg => rg.OnField(Field).Value(regexValue));
            descriptor = descriptor.Filter(fil => fil.Or(query)).Indices(Indices).Type(type.Name.ToLower() + "s");


            result = ElasticClient().Search <T>(b => descriptor);
            string qu = result.ConnectionStatus.ToString();

            Logger.Current.Informational(qu);

            return(result.Documents);
        }
Пример #3
0
        internal IQueryResponse <Dictionary <string, object> > Execute(ElasticSearchQuery query, Type resultType)
        {
            var index = _context.Index as ElasticSearchIndex;

            if (index == null)
            {
                return(new QueryResponse <Dictionary <string, object> >());
            }

            var descriptor = new SearchDescriptor <Dictionary <string, object> >();

            descriptor.Query(query.Query);

            //TODO: need to determine how to allow type specification, so that we don't have to hard-code AllTypes
            //NEST generates the search query URL based on the descriptor type, so if we use Dictionary<string, object> as the descriptor type, the query URL is:
            //http://[server]:9200/[indexname]/dictionary`2s/_search
            //instead we want it to be:
            //http://[server]:9200/[indexname]/_search
            //when searching over all types. so basically, we want the default to AllTypes unless a specific type is used.
            //In the context of Sitecore search, is it even possible to specify a type for the search? probably, but would need some sort of type mapping...
            descriptor.AllTypes();

            if (query.Filter != null)
            {
                descriptor.Filter(filterDescriptor => filterDescriptor.Query(q => query.Filter));
            }

            if (!Settings.DefaultLanguage.StartsWith(_cultureCode))
            {
                descriptor.Filter(f => f.Query(q => q.Term("_language", _cultureCode)));
            }

            var isResultsSizeSet = false;

            if (query.Methods != null)
            {
                var fields = new List <string>();

                var selectMethods = (from m in query.Methods
                                     where m.MethodType == QueryMethodType.Select
                                     select(SelectMethod) m).ToList <SelectMethod>();

                if (selectMethods.Any())
                {
                    foreach (var method in selectMethods)
                    {
                        fields.AddRange(method.FieldNames.Select(fieldName => fieldName.ToLowerInvariant()));
                    }

                    if (!_context.SecurityOptions.HasFlag(SearchSecurityOptions.DisableSecurityCheck))
                    {
                        fields.Add("_uniqueid");
                        fields.Add("_datasource");
                    }
                }

                var getResultsMethods = (from m in query.Methods
                                         where m.MethodType == QueryMethodType.GetResults
                                         select(GetResultsMethod) m).ToList <GetResultsMethod>();

                if (getResultsMethods.Any())
                {
                    if (fields.Count > 0)
                    {
                        fields.Add("score");
                    }
                }

                if (fields.Count > 0)
                {
                    descriptor.Fields(fields.ToArray());
                }

                var orderByMethods = (from m in query.Methods
                                      where m.MethodType == QueryMethodType.OrderBy
                                      select(OrderByMethod) m).ToList <OrderByMethod>();

                if (orderByMethods.Any())
                {
                    foreach (var method in orderByMethods)
                    {
                        var fieldName = method.Field;
                        switch (method.SortDirection)
                        {
                        case SortDirection.Ascending:
                            descriptor.SortAscending(fieldName);
                            break;

                        case SortDirection.Descending:
                            descriptor.SortDescending(fieldName);
                            break;
                        }
                    }
                }

                var skipMethods = (from m in query.Methods
                                   where m.MethodType == QueryMethodType.Skip
                                   select(SkipMethod) m).ToList <SkipMethod>();

                if (skipMethods.Any())
                {
                    var num = skipMethods.Sum(skipMethod => skipMethod.Count);
                    descriptor.Skip(num);
                }

                var takeMethods = (from m in query.Methods
                                   where m.MethodType == QueryMethodType.Take
                                   select(TakeMethod) m).ToList <TakeMethod>();

                if (takeMethods.Any())
                {
                    var num2 = takeMethods.Sum(takeMethod => takeMethod.Count);
                    descriptor.Size(num2);                     //Take is actually just an alias for Size in NEST, so just use Size instead.
                    isResultsSizeSet = true;
                }

                var countMethods = (from m in query.Methods
                                    where m.MethodType == QueryMethodType.Count
                                    select(CountMethod) m).ToList <CountMethod>();

                if (query.Methods.Count == 1 && countMethods.Any())
                {
                    descriptor.Size(0);                     //TODO: is using Size appropriate here? and is "0" the proper value to send?
                    isResultsSizeSet = true;
                }

                var anyMethods = (from m in query.Methods
                                  where m.MethodType == QueryMethodType.Any
                                  select(AnyMethod) m).ToList <AnyMethod>();

                if (query.Methods.Count == 1 && anyMethods.Any())
                {
                    descriptor.Size(0);                     //TODO: is using Size appropriate here? and is "0" the proper value to send?
                    isResultsSizeSet = true;
                }

                var getFacetsMethods = (from m in query.Methods
                                        where m.MethodType == QueryMethodType.GetFacets
                                        select(GetFacetsMethod) m).ToList <GetFacetsMethod>();

                //TODO: implement facet querying
                if ((query.FacetQueries.Count > 0) && (getFacetsMethods.Any() || getResultsMethods.Any()))
                {
                    //foreach (var facetQuery in GetFacetsPipeline.Run(new GetFacetsArgs(null, query.FacetQueries, _context.Index.Configuration.VirtualFieldProcessors, _context.Index.FieldNameTranslator)).FacetQueries.ToHashSet())
                    //{
                    //	if (!facetQuery.FieldNames.Any())
                    //		continue;

                    //	var nullable = facetQuery.MinimumResultCount;
                    //	if (facetQuery.FieldNames.Count() == 1)
                    //	{
                    //		var fieldNameTranslator = FieldNameTranslator as ElasticSearchFieldNameTranslator;
                    //		var indexFieldName = facetQuery.FieldNames.First();
                    //		if (((fieldNameTranslator != null) && (indexFieldName == fieldNameTranslator.StripKnownExtensions(indexFieldName))) && (_context.Index.Configuration.FieldMap.GetFieldConfiguration(indexFieldName) == null))
                    //		{
                    //			indexFieldName = fieldNameTranslator.GetIndexFieldName(indexFieldName.Replace("__", "!").Replace("_", " ").Replace("!", "__"), true);
                    //		}
                    //		IElasticSearchFacetQuery[] queries = new IElasticSearchFacetQuery[1];
                    //		ElasticSearchFacetFieldQuery query2 = new ElasticSearchFacetFieldQuery(indexFieldName)
                    //			{
                    //				MinCount = nullable
                    //			};
                    //		queries[0] = query2;
                    //		options.AddFacets(queries);
                    //	}
                    //	if (facetQuery.FieldNames.Any())
                    //	{
                    //		IElasticSearchFacetQuery[] queryArray2 = new IElasticSearchFacetQuery[1];
                    //		ElasticSearchFacetPivotQuery query3 = new ElasticSearchFacetPivotQuery
                    //			{
                    //				Fields = new[] { string.Join(",", facetQuery.FieldNames) },
                    //				MinCount = nullable
                    //			};
                    //		queryArray2[0] = query3;
                    //		options.AddFacets(queryArray2);
                    //	}
                    //}
                }
            }

            if (!isResultsSizeSet)
            {
                descriptor.Size(ContentSearchConfigurationSettings.SearchMaxResults);
            }

            //var blee = JsonConvert.SerializeObject(descriptor, Formatting.Indented);

            var serializedDescriptor = index.Client.Serialize(descriptor);

            SearchLog.Log.Info("Serialized Query - " + serializedDescriptor);

            var response = index.Client.Search(descriptor);

            if (!response.ConnectionStatus.Success)
            {
                SearchLog.Log.Error("Query exception - " + response.ConnectionStatus.Error.OriginalException);
            }

            return(response);
        }
Пример #4
0
        public static SearchDescriptor <T> FilterOn <T>(this SearchDescriptor <T> searchDescriptor, Expression <Func <T, bool> > filterRule) where T : class
        {
            var filterDescriptor = GenerateFilterDescription <T>(filterRule.Body);

            return(searchDescriptor.Filter(f => filterDescriptor));
        }
Пример #5
0
        SearchResult <T> search <D>(string q, Expression <Func <T, bool> > filter, SearchParameters searchParameters)
        {
            SearchResult <T> searchResult = new SearchResult <T>();
            int limit = searchParameters.Limit == 0 ? 10 : searchParameters.Limit;
            int from  = (searchParameters.PageNumber - 1) * limit;
            ISearchResponse <T> result;
            //string[] types = searchParameters.Types.Select(t => t.FullName).ToArray();

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

            descriptor = descriptor.Types(searchParameters.Types)
                         .From(from)
                         .Size(limit);
            descriptor = descriptor.Filter(f => f.Term("accountID", searchParameters.AccountId));

            if (typeof(T).Equals(typeof(Campaign)))
            {
                if (searchParameters.SortField == ContactSortFieldType.CampaignClickrate)
                {
                    descriptor = descriptor.Sort(s => s.OnField("uniqueClicks").Descending().UnmappedType(Nest.FieldType.None));
                }
                else if (!searchParameters.SortFields.IsAny())
                {
                    descriptor = descriptor.Sort(s => s.OnField("lastUpdatedOn").Descending().UnmappedType(Nest.FieldType.None));
                }
            }
            else if (typeof(T).Equals(typeof(Form)) && !searchParameters.SortFields.IsAny())
            {
                descriptor = descriptor.Sort(s => s.OnField("lastModifiedOn").Descending().UnmappedType(Nest.FieldType.None));
            }
            else if (typeof(T).Equals(typeof(Opportunity)) && !searchParameters.SortFields.IsAny())
            {
                descriptor = descriptor.Sort(s => s.OnField("createdOn").Descending().UnmappedType(Nest.FieldType.None));
            }

            if (searchParameters != null && searchParameters.SortFields.IsAny())
            {
                //todo multi-level sorting.
                string sortField = searchParameters.SortFields.First();
                if (searchParameters.SortDirection == System.ComponentModel.ListSortDirection.Ascending)
                {
                    descriptor = descriptor.Sort(s => s.OnField(sortField.ToCamelCase()).Ascending().UnmappedType(Nest.FieldType.None));
                }
                else
                {
                    descriptor = descriptor.Sort(s => s.OnField(sortField.ToCamelCase()).Descending().UnmappedType(Nest.FieldType.None));
                }
            }

            if (!string.IsNullOrEmpty(q))
            {
                descriptor = descriptor.Query(query => query.QueryString(qs => qs.Query(q)));
            }

            if (filter != null)
            {
                string createddate = typeof(T).Equals(typeof(Form)) ? "createdDate" : (typeof(T).Equals(typeof(Campaign)) ? "createdDate" : "createdOn");
                Expression <Func <T, bool> > expression = filter;
                parseExpression(expression.Body);
                FilterContainer      baseFilter         = stack.Pop();
                FilterDescriptor <T> filterD            = new FilterDescriptor <T>();
                FilterContainer      accountIDFilter    = filterD.Term("accountID", searchParameters.AccountId);
                FilterContainer      dateRangeContainer = new FilterContainer();
                if (searchParameters.StartDate.HasValue && searchParameters.EndDate.HasValue)
                {
                    FilterDescriptor <T> dateRangeFilter = new FilterDescriptor <T>();
                    dateRangeContainer = dateRangeFilter.Range(r =>
                                                               r.GreaterOrEquals(searchParameters.StartDate.Value.ToJSDate()).
                                                               LowerOrEquals(searchParameters.EndDate.Value.ToJSDate()).OnField(createddate));
                }
                descriptor = descriptor.Filter(f => f.And(baseFilter, accountIDFilter, dateRangeContainer));
            }

            if (searchParameters != null && searchParameters.Ids.IsAny())
            {
                if (!string.IsNullOrEmpty(q))
                {
                    descriptor = descriptor.Filter(ds => ds.Terms("id", searchParameters.Ids, TermsExecution.Plain));
                }
                else
                {
                    descriptor = descriptor.Query(query => query.Ids(searchParameters.Ids.Select(i => i.ToString()).ToList()));
                }
            }



            result = ElasticClient().Search <T>(body => descriptor);
            string que = result.ConnectionStatus.ToString();

            Logger.Current.Verbose(que);

            searchResult.Results   = result.Documents;
            searchResult.TotalHits = result.Total;

            return(searchResult);
        }
Пример #6
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);
        }