Пример #1
0
        public static void SetQueryRules(QueryRulePack rules)
        {
            string[] queryKeys = QueryLogic.Queries.GetTypeQueries(TypeLogic.EntityToType[rules.Type]).Keys.Select(qn => QueryUtils.GetKey(qn)).ToArray();

            cache.SetRules(rules, r => queryKeys.Contains(r.Key));
        }
Пример #2
0
        static Implementations DefaultImplementations(Type type, object queryName)
        {
            var property = type.GetProperty("Entity", BindingFlags.Instance | BindingFlags.Public);

            if (property == null)
            {
                throw new InvalidOperationException("Entity property not found on query {0}".FormatWith(QueryUtils.GetKey(queryName)));
            }

            return(Implementations.By(property.PropertyType.CleanType()));
        }
Пример #3
0
 public Order ToOrder(QueryDescription qd, bool canAggregate)
 {
     return(new Order(QueryUtils.Parse(this.token, qd, SubTokensOptions.CanElement | (canAggregate ? SubTokensOptions.CanAggregate : 0)), orderType));
 }
Пример #4
0
        protected IEnumerable <OmniboxFilterResult> GetFilterQueries(string rawQuery, QueryDescription queryDescription, FilterSyntax syntax, List <OmniboxToken> tokens)
        {
            List <OmniboxFilterResult> result = new List <OmniboxFilterResult>();

            int operatorIndex = syntax.Index + syntax.TokenLength;

            List <(QueryToken token, ImmutableStack <OmniboxMatch> stack)> ambiguousTokens = GetAmbiguousTokens(null, ImmutableStack <OmniboxMatch> .Empty,
                                                                                                                queryDescription, tokens, syntax.Index, operatorIndex).ToList();

            foreach ((QueryToken token, ImmutableStack <OmniboxMatch> stack)pair in ambiguousTokens)
            {
                var distance     = pair.stack.Sum(a => a.Distance);
                var tokenMatches = pair.stack.Reverse().ToArray();
                var token        = pair.token;

                if (syntax.Completion == FilterSyntaxCompletion.Token)
                {
                    if (tokens[operatorIndex - 1].Next(rawQuery) == '.' && pair.stack.All(a => ((QueryToken)a.Value).ToString().ToOmniboxPascal() == a.Text))
                    {
                        foreach (var qt in QueryUtils.SubTokens(pair.token, queryDescription, SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement))
                        {
                            result.Add(new OmniboxFilterResult(distance, syntax, qt, tokenMatches));
                        }
                    }
                    else
                    {
                        result.Add(new OmniboxFilterResult(distance, syntax, token, tokenMatches));
                    }
                }
                else
                {
                    string?canFilter = QueryUtils.CanFilter(pair.token);

                    if (canFilter.HasText())
                    {
                        result.Add(new OmniboxFilterResult(distance, syntax, token, tokenMatches)
                        {
                            CanFilter = canFilter,
                        });
                    }
                    else
                    {
                        FilterOperation operation = FilterValueConverter.ParseOperation(tokens[operatorIndex].Value);

                        if (syntax.Completion == FilterSyntaxCompletion.Operation)
                        {
                            var suggested = SugestedValues(pair.token);

                            if (suggested == null)
                            {
                                result.Add(new OmniboxFilterResult(distance, syntax, token, tokenMatches)
                                {
                                    Operation = operation,
                                });
                            }
                            else
                            {
                                foreach (var item in suggested)
                                {
                                    result.Add(new OmniboxFilterResult(distance, syntax, token, tokenMatches)
                                    {
                                        Operation = operation,
                                        Value     = item.Value
                                    });
                                }
                            }
                        }
                        else
                        {
                            var values = GetValues(pair.token, tokens[operatorIndex + 1]);

                            foreach (var value in values)
                            {
                                result.Add(new OmniboxFilterResult(distance, syntax, token, tokenMatches)
                                {
                                    Operation  = operation,
                                    Value      = value.Value,
                                    ValueMatch = value.Match,
                                });
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #5
0
        public override IEnumerable <DynamicQueryOmniboxResult> GetResults(string rawQuery, List <OmniboxToken> tokens, string tokenPattern)
        {
            Match m = regex.Match(tokenPattern);

            if (!m.Success)
            {
                yield break;
            }

            string pattern = tokens[0].Value;

            bool isPascalCase = OmniboxUtils.IsPascalCasePattern(pattern);

            List <FilterSyntax>?syntaxSequence = null;

            foreach (var match in OmniboxUtils.Matches(OmniboxParser.Manager.GetQueries(), OmniboxParser.Manager.AllowedQuery, pattern, isPascalCase).OrderBy(ma => ma.Distance))
            {
                var queryName = match.Value;

                if (syntaxSequence == null)
                {
                    syntaxSequence = SyntaxSequence(m);
                }

                if (syntaxSequence.Any())
                {
                    QueryDescription description = OmniboxParser.Manager.GetDescription(match.Value);

                    IEnumerable <IEnumerable <OmniboxFilterResult> > bruteFilters = syntaxSequence.Select(a => GetFilterQueries(rawQuery, description, a, tokens));

                    foreach (var list in bruteFilters.CartesianProduct())
                    {
                        yield return(new DynamicQueryOmniboxResult
                        {
                            QueryName = match.Value,
                            QueryNameMatch = match,
                            Distance = match.Distance + list.Average(a => a.Distance),
                            Filters = list.ToList(),
                        });
                    }
                }
                else
                {
                    if (match.Text == pattern && tokens.Count == 1 && tokens[0].Next(rawQuery) == ' ')
                    {
                        QueryDescription description = OmniboxParser.Manager.GetDescription(match.Value);

                        foreach (var qt in QueryUtils.SubTokens(null, description, SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement))
                        {
                            yield return(new DynamicQueryOmniboxResult
                            {
                                QueryName = match.Value,
                                QueryNameMatch = match,
                                Distance = match.Distance,
                                Filters = new List <OmniboxFilterResult> {
                                    new OmniboxFilterResult(0, null, qt, null)
                                },
                            });
                        }
                    }
                    else
                    {
                        yield return(new DynamicQueryOmniboxResult
                        {
                            QueryName = match.Value,
                            QueryNameMatch = match,
                            Distance = match.Distance,
                            Filters = new List <OmniboxFilterResult>()
                        });
                    }
                }
            }
        }
Пример #6
0
        internal Column ToColumn(QueryDescription qd)
        {
            var queryToken = QueryUtils.Parse(token, qd, SubTokensOptions.CanElement);

            return(new Column(queryToken, displayName ?? queryToken.NiceName()));
        }
Пример #7
0
 public static NormalWindowProxy <T> QuickLinkNavigate <T>(this LeftPanelProxy left) where T : Entity
 {
     return(left.QuickLinkCapture(QueryUtils.GetKey(typeof(T).FullName)).ToNormalWindow <T>());
 }
Пример #8
0
        public DynamicQueryBucket(object queryName, Func <IDynamicQueryCore> lazyQueryCore, Implementations entityImplementations)
        {
            if (queryName == null)
            {
                throw new ArgumentNullException("queryName");
            }

            if (lazyQueryCore == null)
            {
                throw new ArgumentNullException("lazyQueryCore");
            }

            this.QueryName             = queryName;
            this.EntityImplementations = entityImplementations;

            this.Core = new Lazy <IDynamicQueryCore>(() =>
            {
                var core = lazyQueryCore();

                core.QueryName = QueryName;

                core.StaticColumns.Where(sc => sc.IsEntity).SingleEx(() => "Entity column on {0}".FormatWith(QueryUtils.GetKey(QueryName)));

                core.EntityColumnFactory().Implementations = entityImplementations;

                var errors = core.StaticColumns.Where(sc => sc.Implementations == null && sc.Type.CleanType().IsIEntity()).ToString(a => a.Name, ", ");

                if (errors.HasText())
                {
                    throw new InvalidOperationException("Column {0} of {1} does not have implementations deffined. Use Column extension method".FormatWith(errors, QueryUtils.GetKey(QueryName)));
                }

                return(core);
            });
        }
Пример #9
0
        // verify results are as expected.
        private void VerifyResults(float boost, IndexSearcher s,
                                   IDictionary <int, float> h1, IDictionary <int, float> h2customNeutral, IDictionary <int, float> h3CustomMul, IDictionary <int, float> h4CustomAdd, IDictionary <int, float> h5CustomMulAdd,
                                   Query q1, Query q2, Query q3, Query q4, Query q5)
        {
            // verify numbers of matches
            Log("#hits = " + h1.Count);
            assertEquals("queries should have same #hits", h1.Count, h2customNeutral.Count);
            assertEquals("queries should have same #hits", h1.Count, h3CustomMul.Count);
            assertEquals("queries should have same #hits", h1.Count, h4CustomAdd.Count);
            assertEquals("queries should have same #hits", h1.Count, h5CustomMulAdd.Count);

            QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, q1, s, Rarely());
            QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, q2, s, Rarely());
            QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, q3, s, Rarely());
            QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, q4, s, Rarely());
            QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, q5, s, Rarely());

            // verify scores ratios
            foreach (int doc in h1.Keys)
            {
                Log("doc = " + doc);

                float fieldScore = ExpectedFieldScore(s.IndexReader.Document(doc).Get(ID_FIELD));
                Log("fieldScore = " + fieldScore);
                assertTrue("fieldScore should not be 0", fieldScore > 0);

                float score1 = h1[doc];
                LogResult("score1=", s, q1, doc, score1);

                float score2 = h2customNeutral[doc];
                LogResult("score2=", s, q2, doc, score2);
                assertEquals("same score (just boosted) for neutral", boost * score1, score2, CheckHits.ExplainToleranceDelta(boost * score1, score2));

                float score3 = h3CustomMul[doc];
                LogResult("score3=", s, q3, doc, score3);
                assertEquals("new score for custom mul", boost * fieldScore * score1, score3, CheckHits.ExplainToleranceDelta(boost * fieldScore * score1, score3));

                float score4 = h4CustomAdd[doc];
                LogResult("score4=", s, q4, doc, score4);
                assertEquals("new score for custom add", boost * (fieldScore + score1), score4, CheckHits.ExplainToleranceDelta(boost * (fieldScore + score1), score4));

                float score5 = h5CustomMulAdd[doc];
                LogResult("score5=", s, q5, doc, score5);
                assertEquals("new score for custom mul add", boost * fieldScore * (score1 + fieldScore), score5, CheckHits.ExplainToleranceDelta(boost * fieldScore * (score1 + fieldScore), score5));
            }
        }
Пример #10
0
        public static void Start()
        {
            if (Navigator.Manager.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                QueryClient.Start();

                UserAssetsClient.Start();
                UserAssetsClient.RegisterExportAssertLink <UserChartEntity>();

                LinksClient.RegisterEntityLinks <UserChartEntity>((lite, ctx) => new[]
                {
                    new QuickLinkAction(ChartMessage.Preview, RouteHelper.New().Action <ChartController>(cc => cc.ViewUserChart(lite, null)))
                    {
                        IsVisible = ChartPermission.ViewCharting.IsAuthorized()
                    }
                });

                Func <SubTokensOptions, Mapping <QueryTokenEmbedded> > qtMapping = ops => ctx =>
                {
                    string tokenStr = UserAssetsHelper.GetTokenString(ctx);

                    string queryKey  = ctx.Parent.Parent.Parent.Inputs[TypeContextUtilities.Compose("Query", "Key")];
                    object queryName = QueryLogic.ToQueryName(queryKey);

                    var chart = ((UserChartEntity)ctx.Parent.Parent.Parent.UntypedValue);

                    QueryDescription qd = DynamicQueryManager.Current.QueryDescription(queryName);
                    return(new QueryTokenEmbedded(QueryUtils.Parse(tokenStr, qd, ops | (chart.GroupResults ? SubTokensOptions.CanAggregate : 0))));
                };

                Navigator.AddSettings(new List <EntitySettings>
                {
                    new EntitySettings <UserChartEntity> {
                        PartialViewName = _ => ChartClient.ViewPrefix.FormatWith("UserChart"), IsCreable = EntityWhen.Never
                    }
                });

                Navigator.EntitySettings <UserChartEntity>().MappingMain = Navigator.EntitySettings <UserChartEntity>().MappingLine =
                    new EntityMapping <UserChartEntity>(true)
                    .SetProperty(cb => cb.Columns, new ChartClient.MListCorrelatedOrDefaultMapping <ChartColumnEmbedded>(ChartClient.MappingChartColumn))
                    .SetProperty(cr => cr.Filters, new MListMapping <QueryFilterEmbedded>
                {
                    ElementMapping = new EntityMapping <QueryFilterEmbedded>(false)
                                     .CreateProperty(a => a.Operation)
                                     .CreateProperty(a => a.ValueString)
                                     .SetProperty(a => a.Token, qtMapping(SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement))
                })
                    .SetProperty(cr => cr.Orders, new MListMapping <QueryOrderEmbedded>
                {
                    ElementMapping = new EntityMapping <QueryOrderEmbedded>(false)
                                     .CreateProperty(a => a.OrderType)
                                     .SetProperty(a => a.Token, qtMapping(SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement))
                })
                    .SetProperty(cb => cb.Parameters, new MListDictionaryMapping <ChartParameterEmbedded, string>(p => p.Name)
                {
                    OnlyIfPossible = true
                });

                RouteTable.Routes.MapRoute(null, "UC/{webQueryName}/{lite}",
                                           new { controller = "Chart", action = "ViewUserChart" });

                UserChartEntity.SetConverters(query => QueryLogic.ToQueryName(query.Key), queryName =>
                                              QueryLogic.GetQueryEntity(queryName));

                OperationClient.AddSetting(new EntityOperationSettings <UserChartEntity>(UserChartOperation.Delete)
                {
                    Click = ctx => ChartClient.Module["deleteUserChart"](ctx.Options(), Finder.FindRoute(((UserChartEntity)ctx.Entity).Query.ToQueryName())),
                });

                LinksClient.RegisterEntityLinks <Entity>((entity, ctrl) =>
                {
                    if (!ChartPermission.ViewCharting.IsAuthorized())
                    {
                        return(null);
                    }

                    return(UserChartLogic.GetUserChartsEntity(entity.EntityType)
                           .Select(cp => new UserChartQuickLink(cp, entity)).ToArray());
                });
            }
        }
Пример #11
0
 public Dictionary <string, object> GetQueries()
 {
     return(queries.GetOrAdd(CultureInfo.CurrentCulture, ci =>
                             GetAllQueryNames().ToOmniboxPascalDictionary(qn => QueryUtils.GetNiceName(qn), qn => qn)));
 }
Пример #12
0
        public void TestRandomIndex()
        {
            Directory    dir      = NewDirectory();
            MockAnalyzer analyzer = new MockAnalyzer(Random);

            analyzer.MaxTokenLength = TestUtil.NextInt32(Random, 1, IndexWriter.MAX_TERM_LENGTH);
            RandomIndexWriter w = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, dir, analyzer);

            CreateRandomIndex(AtLeast(50), w, Random.NextInt64());
            DirectoryReader reader        = w.GetReader();
            AtomicReader    wrapper       = SlowCompositeReaderWrapper.Wrap(reader);
            string          field         = @"body";
            Terms           terms         = wrapper.GetTerms(field);
            var             lowFreqQueue  = new PriorityQueueAnonymousClass(5);
            var             highFreqQueue = new PriorityQueueAnonymousClass1(5);

            try
            {
                TermsEnum iterator = terms.GetEnumerator();
                while (iterator.MoveNext())
                {
                    if (highFreqQueue.Count < 5)
                    {
                        highFreqQueue.Add(new TermAndFreq(
                                              BytesRef.DeepCopyOf(iterator.Term), iterator.DocFreq));
                        lowFreqQueue.Add(new TermAndFreq(
                                             BytesRef.DeepCopyOf(iterator.Term), iterator.DocFreq));
                    }
                    else
                    {
                        if (highFreqQueue.Top.freq < iterator.DocFreq)
                        {
                            highFreqQueue.Top.freq = iterator.DocFreq;
                            highFreqQueue.Top.term = BytesRef.DeepCopyOf(iterator.Term);
                            highFreqQueue.UpdateTop();
                        }

                        if (lowFreqQueue.Top.freq > iterator.DocFreq)
                        {
                            lowFreqQueue.Top.freq = iterator.DocFreq;
                            lowFreqQueue.Top.term = BytesRef.DeepCopyOf(iterator.Term);
                            lowFreqQueue.UpdateTop();
                        }
                    }
                }

                int lowFreq  = lowFreqQueue.Top.freq;
                int highFreq = highFreqQueue.Top.freq;
                AssumeTrue(@"unlucky index", highFreq - 1 > lowFreq);
                List <TermAndFreq> highTerms = QueueToList(highFreqQueue);
                List <TermAndFreq> lowTerms  = QueueToList(lowFreqQueue);

                IndexSearcher    searcher     = NewSearcher(reader);
                Occur            lowFreqOccur = RandomOccur(Random);
                BooleanQuery     verifyQuery  = new BooleanQuery();
                CommonTermsQuery cq           = new CommonTermsQuery(RandomOccur(Random),
                                                                     lowFreqOccur, highFreq - 1, Random.NextBoolean());
                foreach (TermAndFreq termAndFreq in lowTerms)
                {
                    cq.Add(new Term(field, termAndFreq.term));
                    verifyQuery.Add(new BooleanClause(new TermQuery(new Term(field,
                                                                             termAndFreq.term)), lowFreqOccur));
                }
                foreach (TermAndFreq termAndFreq in highTerms)
                {
                    cq.Add(new Term(field, termAndFreq.term));
                }

                TopDocs cqSearch = searcher.Search(cq, reader.MaxDoc);

                TopDocs verifySearch = searcher.Search(verifyQuery, reader.MaxDoc);
                assertEquals(verifySearch.TotalHits, cqSearch.TotalHits);
                var hits = new JCG.HashSet <int>();
                foreach (ScoreDoc doc in verifySearch.ScoreDocs)
                {
                    hits.Add(doc.Doc);
                }

                foreach (ScoreDoc doc in cqSearch.ScoreDocs)
                {
                    assertTrue(hits.Remove(doc.Doc));
                }

                assertTrue(hits.Count == 0);

                /*
                 *  need to force merge here since QueryUtils adds checks based
                 *  on leave readers which have different statistics than the top
                 *  level reader if we have more than one segment. This could
                 *  result in a different query / results.
                 */
                w.ForceMerge(1);
                DirectoryReader reader2 = w.GetReader();
                QueryUtils.Check(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                    this,
#endif
                    Random, cq, NewSearcher(reader2));
                reader2.Dispose();
            }
            finally
            {
                reader.Dispose();
                wrapper.Dispose();
                w.Dispose();
                dir.Dispose();
            }
        }
Пример #13
0
        private GetProfileResponse GetCustomerProfileFromHPIDAndDatabase(GetProfileResponse response, UserAuthenticationInterchange hppAuthInterchange, TokenDetails sessionToken, CustomerIds idS, bool RetainOldValues, APIMethods apiRetainOldValues)
        {
            User profile = null;
            List <RoleMapping> roleMappings = null;

            try
            {
                // check is done based on profile, customerId is also generated for a new profile
                bool IsNewCustomer = false;

                RequestFindOrInsertHPIDProfile requestFindOrInsertHPID = new RequestFindOrInsertHPIDProfile();
                requestFindOrInsertHPID.Locale =
                    string.IsNullOrEmpty(response?.CustomerProfileObject?.Locale)?TranslationUtils.Locale(hppAuthInterchange.LanguageCode, hppAuthInterchange.CountryCode):response?.CustomerProfileObject?.Locale;
                requestFindOrInsertHPID.HPIDprofileId      = idS.HPIDid;
                requestFindOrInsertHPID.HPPprofileId       = idS.HPPid;
                requestFindOrInsertHPID.tokenDetails       = sessionToken;
                requestFindOrInsertHPID.clientId           = hppAuthInterchange.ClientId;
                requestFindOrInsertHPID.apiRetainOldValues = apiRetainOldValues;
                requestFindOrInsertHPID.EmailAddrees       = response?.CustomerProfileObject?.EmailAddress;
                requestFindOrInsertHPID.CompanyName        = response?.CustomerProfileObject?.CompanyName;
                requestFindOrInsertHPID.ActiveHealth       = response.CustomerProfileObject.ActiveHealth;
                requestFindOrInsertHPID.FirstName          = response?.CustomerProfileObject?.FirstName;
                requestFindOrInsertHPID.LastName           = response.CustomerProfileObject.LastName;



                profile = userUtils.FindOrInsertHPIDProfile(response, requestFindOrInsertHPID, out IsNewCustomer);

                if (response.ErrorList.Count > 0)
                {
                    return(response);
                }

                if (profile.RoleMappings.Count == 0)
                {
                    roleMappings = roleUtils.InsertRoleMapping(response, profile.UserId);
                }
                else
                {
                    roleMappings = profile.RoleMappings.ToList();
                }


                hppAuthInterchange.UserId = Convert.ToInt32(profile.UserId);
                hppAuthInterchange.Token  = sessionToken.AccessToken;
                hppAuthInterchange.IsHPID = true;



                // Register profile & session token in database
                customerUtils.InsertOrUpdateHPPToken(response, (UserAuthentication)hppAuthInterchange, RetainOldValues);

                List <int>       roleids   = roleMappings.Select(r => r.RoleId).ToList();
                List <UserRoles> userRoles = new List <UserRoles>();

                RESTAPIGetRolesResponse rolesResponse = new RESTAPIGetRolesResponse();

                statusUtils.GetRoles(rolesResponse);

                if (rolesResponse.RolesList.Count != 0)
                {
                    var rolResponseList = rolesResponse.RolesList.Where(r => roleids.Contains(r.RoleId)).ToList();
                    foreach (var roles in rolResponseList)
                    {
                        userRoles.Add(new UserRoles()
                        {
                            Id = roles.RoleId, Name = roles.RoleName
                        });
                    }
                }


                response.Credentials = new AccessCredentials()
                {
                    UserID       = Convert.ToInt32(profile.UserId),
                    SessionToken = QueryUtils.GetMD5(sessionToken.AccessToken),
                    CallerId     = hppAuthInterchange.CallerId,
                    Token        = sessionToken.AccessToken,
                    Roles        = userRoles
                };

                response = GetProfileDataFromDatabase(response, profile);
                response.CustomerProfileObject.IsNewCustomer = IsNewCustomer;
                response.LoginDate = hppAuthInterchange.LoginDate;
            }
            catch (Exception ex)
            {
                response.ErrorList.Add(new Fault("GetCustomerProfileFailed", ex.Message));
            }

            return(response);
        }
Пример #14
0
        public static MvcHtmlString NewFilter(this HtmlHelper helper, FilterOption filterOptions, Context context, int index)
        {
            HtmlStringBuilder sb = new HtmlStringBuilder();

            FilterType             filterType         = QueryUtils.GetFilterType(filterOptions.Token.Type);
            List <FilterOperation> possibleOperations = QueryUtils.GetFilterOperations(filterType);

            var id = context.Compose("trFilter", index.ToString());

            using (sb.SurroundLine(new HtmlTag("tr").Id(id)))
            {
                using (sb.SurroundLine("td"))
                {
                    if (!filterOptions.Frozen)
                    {
                        sb.AddLine(new HtmlTag("a", context.Compose("btnDelete", index.ToString()))
                                   .Attr("title", SearchMessage.DeleteFilter.NiceToString())
                                   .Attr("onclick", JsModule.Finder["deleteFilter"](id).ToString())
                                   .Class("sf-line-button sf-remove")
                                   .InnerHtml(new HtmlTag("span").Class("glyphicon glyphicon-remove")));
                    }
                }

                using (sb.SurroundLine(new HtmlTag("td")))
                {
                    sb.AddLine(new HtmlTag("input")
                               .Attr("type", "hidden")
                               .Attr("value", filterOptions.Token.FullKey())
                               .ToHtmlSelf());

                    foreach (var t in filterOptions.Token.Follow(tok => tok.Parent).Reverse())
                    {
                        sb.AddLine(new HtmlTag("span")
                                   .Class("sf-filter-token")
                                   .Attr("title", t.NiceTypeName)
                                   .Attr("style", "color:" + t.TypeColor)
                                   .SetInnerText(t.ToString()).ToHtml());
                    }
                }

                using (sb.SurroundLine("td"))
                {
                    var dic = new Dictionary <string, object> {
                        { "class", "form-control" }
                    };
                    if (filterOptions.Frozen)
                    {
                        dic.Add("disabled", "disabled");
                    }

                    sb.AddLine(
                        helper.SafeDropDownList(
                            context.Compose("ddlSelector", index.ToString()),
                            possibleOperations.Select(fo =>
                                                      new SelectListItem
                    {
                        Text     = fo.NiceToString(),
                        Value    = fo.ToString(),
                        Selected = fo == filterOptions.Operation
                    }),
                            dic));
                }

                using (sb.SurroundLine("td"))
                {
                    Context valueContext = new Context(context, "value_" + index.ToString());

                    if (filterOptions.Frozen && !filterOptions.Token.Type.IsLite())
                    {
                        string txtValue = (filterOptions.Value != null) ? filterOptions.Value.ToString() : "";
                        sb.AddLine(helper.TextBox(valueContext.Prefix, txtValue, new { @readonly = "readonly" }));
                    }
                    else
                    {
                        sb.AddLine(PrintValueField(helper, valueContext, filterOptions));
                    }
                }
            }

            return(sb.ToHtml());
        }
Пример #15
0
        public override void Execute()
        {
            WriteLiteral("\r\n\r\n");


            #line 12 "..\..\UserQueries\Views\UserQuery.cshtml"
            using (var e = Html.TypeContext <UserQueryEntity>())
            {
                e.LabelColumns = new BsColumn(2);

                object queryName = QueryLogic.ToQueryName(e.Value.Query.Key);
                ViewData[ViewDataKeys.QueryDescription] = DynamicQueryManager.Current.QueryDescription(queryName); //To be use inside Repeaters



            #line default
            #line hidden

            #line 19 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.EntityLine(e, f => f.Owner, el => el.Create = false));


            #line default
            #line hidden

            #line 19 "..\..\UserQueries\Views\UserQuery.cshtml"


                using (var query = e.SubContext(f => f.Query))
                {
            #line default
            #line hidden

            #line 23 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.HiddenRuntimeInfo(query));


            #line default
            #line hidden

            #line 23 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 25 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.Hidden(query.Compose("Key"), query.Value.Key));


            #line default
            #line hidden

            #line 25 "..\..\UserQueries\Views\UserQuery.cshtml"


                    var niceName = QueryUtils.GetNiceName(query.Value.ToQueryName());



            #line default
            #line hidden

            #line 29 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.FormGroup(e, null, typeof(QueryEntity).NiceName(), Finder.IsFindable(queryName) ?
                                         new HtmlTag("a").Class("form-control-static").Attr("href", Finder.FindRoute(queryName)).SetInnerText(niceName).ToHtml() :
                                         Html.Span(null, niceName, "form-control-static")));


            #line default
            #line hidden

            #line 31 "..\..\UserQueries\Views\UserQuery.cshtml"
                }



            #line default
            #line hidden

            #line 35 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.DisplayName));


            #line default
            #line hidden

            #line 35 "..\..\UserQueries\Views\UserQuery.cshtml"


                var entityTypePrefix = e.SubContext(a => a.EntityType).Prefix;



            #line default
            #line hidden

            #line 39 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.EntityLine(e, f => f.EntityType, el =>
                {
                    el.AutocompleteUrl = Url.Action("TypeAutocomplete", "Finder");
                    el.AttachFunction  = UserAssetsClient.Module["attachShowCurrentEntity"](el);
                }));


            #line default
            #line hidden

            #line 43 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden
                WriteLiteral("    <p");

                WriteLiteral(" class=\"messageEntity col-sm-offset-2\"");

                WriteLiteral(">\r\n");

                WriteLiteral("        ");


            #line 46 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(UserQueryMessage.Use0ToFilterCurrentEntity.NiceToString().FormatWith(CurrentEntityConverter.CurrentEntityKey));


            #line default
            #line hidden
                WriteLiteral("\r\n    </p>\r\n");


            #line 48 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 49 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.SearchOnLoad));


            #line default
            #line hidden

            #line 49 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 50 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.ShowFilterButton));


            #line default
            #line hidden

            #line 50 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 51 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.WithoutFilters));


            #line default
            #line hidden

            #line 51 "..\..\UserQueries\Views\UserQuery.cshtml"


                using (var sc = e.SubContext())
                {
                    sc.FormGroupSize = FormGroupSize.Small;



            #line default
            #line hidden
                    WriteLiteral("    <div");

                    WriteLiteral(" class=\"repeater-inline form-inline sf-filters-list \"");

                    WriteLiteral(">\r\n");

                    WriteLiteral("        ");


            #line 58 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.EntityRepeater(e, f => f.Filters, er => { er.PreserveViewData = true; }));


            #line default
            #line hidden
                    WriteLiteral("\r\n    </div>\r\n");


            #line 60 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 61 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.ValueLine(e, f => f.ColumnsMode));


            #line default
            #line hidden

            #line 61 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden
                    WriteLiteral("    <div");

                    WriteLiteral(" class=\"repeater-inline form-inline sf-filters-list\"");

                    WriteLiteral(">\r\n");

                    WriteLiteral("        ");


            #line 63 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.EntityRepeater(e, f => f.Columns, er => { er.PreserveViewData = true; }));


            #line default
            #line hidden
                    WriteLiteral("\r\n    </div>\r\n");


            #line 65 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden
                    WriteLiteral("    <div");

                    WriteLiteral(" class=\"repeater-inline form-inline sf-filters-list\"");

                    WriteLiteral(">\r\n");

                    WriteLiteral("        ");


            #line 67 "..\..\UserQueries\Views\UserQuery.cshtml"
                    Write(Html.EntityRepeater(e, f => f.Orders, er => { er.PreserveViewData = true; }));


            #line default
            #line hidden
                    WriteLiteral("\r\n    </div>\r\n");


            #line 69 "..\..\UserQueries\Views\UserQuery.cshtml"
                }



            #line default
            #line hidden

            #line 71 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.PaginationMode));


            #line default
            #line hidden

            #line 71 "..\..\UserQueries\Views\UserQuery.cshtml"



            #line default
            #line hidden

            #line 72 "..\..\UserQueries\Views\UserQuery.cshtml"
                Write(Html.ValueLine(e, f => f.ElementsPerPage));


            #line default
            #line hidden

            #line 72 "..\..\UserQueries\Views\UserQuery.cshtml"
            }


            #line default
            #line hidden
            WriteLiteral("</div>\r\n");
        }
Пример #16
0
 private static Dictionary <string, object> CreateQueryNames()
 {
     return(Queries.GetQueryNames().ToDictionaryEx(qn => QueryUtils.GetKey(qn), "queryName"));
 }
Пример #17
0
        public static WikiLink LinkParser(string content)
        {
            Match m = HelpLogic.HelpLinkRegex.Match(content);

            if (m.Success)
            {
                string letter = m.Groups["letter"].ToString();
                string link   = m.Groups["link"].ToString();
                string text   = m.Groups["text"].ToString();

                switch (letter)
                {
                case WikiFormat.EntityLink:
                    Type t = TypeLogic.TryGetType(link);
                    return(new WikiLink(
                               HelpUrls.EntityUrl(t),
                               text.HasText() ? text : t.NiceName()));

                case WikiFormat.Hyperlink:
                    return(new WikiLink(link, text));

                case WikiFormat.OperationLink:
                    OperationSymbol operation = SymbolLogic <OperationSymbol> .TryToSymbol(link);

                    List <Type> types = OperationLogic.FindTypes(operation).Where(TypeLogic.TypeToEntity.ContainsKey).ToList();
                    if (types.Count == 1)
                    {
                        return(new WikiLink(
                                   HelpUrls.OperationUrl(types[0], operation),
                                   text.HasText() ? text : operation.NiceToString()));
                    }
                    else
                    {
                        return(new MultiWikiLink(operation.NiceToString())
                        {
                            Links = types.Select(currentType =>
                                                 new WikiLink(
                                                     HelpUrls.OperationUrl(currentType, operation),
                                                     currentType.NiceName(), operation.NiceToString())).ToList()
                        });
                    }

                case WikiFormat.PropertyLink:
                    PropertyRoute route = PropertyRoute.Parse(TypeLogic.TryGetType(link.Before('.')), link.After('.'));

                    while (route.PropertyRouteType == PropertyRouteType.LiteEntity ||
                           route.PropertyRouteType == PropertyRouteType.Mixin ||
                           route.PropertyRouteType == PropertyRouteType.MListItems)
                    {
                        route = route.Parent;
                    }

                    return(new WikiLink(HelpUrls.PropertyUrl(route), route.PropertyInfo.NiceName()));

                case WikiFormat.QueryLink:
                    object o = QueryLogic.TryToQueryName(link);
                    if (o as Enum != null)
                    {
                        Enum query = (Enum)o;
                        return(new WikiLink(
                                   HelpUrls.QueryUrl(query),
                                   text.HasText() ? text : QueryUtils.GetNiceName(query)));
                    }
                    else
                    {
                        Type query = (Type)o;
                        return(new WikiLink(
                                   HelpUrls.QueryUrl(query),
                                   text.HasText() ? text : QueryUtils.GetNiceName(query)));
                    }

                case WikiFormat.NamespaceLink:
                    NamespaceHelp nameSpace = HelpLogic.GetNamespaceHelp(link);
                    return(new WikiLink(
                               HelpUrls.NamespaceUrl(link),
                               text.HasText() ? text : link,
                               nameSpace != null ? "" : "unavailable"));

                case WikiFormat.AppendixLink:
                    AppendixHelp appendix = HelpLogic.GetAppendixHelp(link);
                    return(new WikiLink(
                               HelpUrls.AppendixUrl(link),
                               text.HasText() ? text : link,
                               appendix != null ? "" : "unavailable"));
                }
            }
            return(null);
        }
Пример #18
0
        static SqlPreCommand SynchronizeOperations(Replacements replacements, SyncData data)
        {
            var dic = Database.Query <OperationHelpEntity>().ToList();

            if (dic.IsEmpty())
            {
                return(null);
            }

            var queryKeys = DynamicQueryManager.Current.GetQueryNames().ToDictionary(a => QueryUtils.GetKey(a));

            var table = Schema.Current.Table <OperationHelpEntity>();

            var replace = replacements.TryGetC(QueryLogic.QueriesKey);

            using (replacements.WithReplacedDatabaseName())
                return(dic.Select(qh =>
                {
                    qh.Description = SynchronizeContent(qh.Description, replacements, data);

                    return table.UpdateSqlSync(qh);
                }).Combine(Spacing.Simple));
        }
Пример #19
0
 public static SearchWindowProxy QuickLinkExplore(this LeftPanelProxy left, object queryName)
 {
     return(left.QuickLinkCapture(QueryUtils.GetKey(queryName)).ToSearchWindow());
 }
Пример #20
0
        public void TestMoreLikeThisQuery()
        {
            Query query = new MoreLikeThisQuery("this is a test", new[] { "text" }, new MockAnalyzer(Random), "text");

            QueryUtils.Check(Random, query, searcher);
        }
Пример #21
0
        protected override string?PropertyValidation(PropertyInfo pi)
        {
            if (IsGroup)
            {
                if (pi.Name == nameof(GroupOperation) && GroupOperation == null)
                {
                    return(ValidationMessage._0IsNotSet.NiceToString(pi.NiceName()));
                }


                if (token != null && token.ParseException == null)
                {
                    if (pi.Name == nameof(Token))
                    {
                        return(QueryUtils.CanFilter(token.Token));
                    }
                }
            }
            else
            {
                if (pi.Name == nameof(Operation) && Operation == null)
                {
                    return(ValidationMessage._0IsNotSet.NiceToString(pi.NiceName()));
                }

                if (pi.Name == nameof(Token) && Token == null)
                {
                    return(ValidationMessage._0IsNotSet.NiceToString(pi.NiceName()));
                }

                if (token != null && token.ParseException == null)
                {
                    if (pi.Name == nameof(Token))
                    {
                        return(QueryUtils.CanFilter(token.Token));
                    }

                    if (pi.Name == nameof(Operation) && Operation != null)
                    {
                        FilterType?filterType = QueryUtils.TryGetFilterType(Token !.Token.Type);

                        if (filterType == null)
                        {
                            return(UserQueryMessage._0IsNotFilterable.NiceToString().FormatWith(token));
                        }

                        if (!QueryUtils.GetFilterOperations(filterType.Value).Contains(Operation.Value))
                        {
                            return(UserQueryMessage.TheFilterOperation0isNotCompatibleWith1.NiceToString().FormatWith(Operation, filterType));
                        }
                    }

                    if (pi.Name == nameof(ValueString))
                    {
                        var result = FilterValueConverter.TryParse(ValueString, Token !.Token.Type, Operation !.Value.IsList());
                        return(result is Result <object> .Error e ? e.ErrorText : null);
                    }
                }
            }

            return(null);
        }
Пример #22
0
        static SqlPreCommand?SynchronizeQueries(Replacements replacements, SyncData data)
        {
            var dic = Database.Query <QueryHelpEntity>().ToList();

            if (dic.IsEmpty())
            {
                return(null);
            }

            var queryKeys = QueryLogic.Queries.GetQueryNames().ToDictionary(a => QueryUtils.GetKey(a));

            var table = Schema.Current.Table <QueryHelpEntity>();

            var replace = replacements.TryGetC(QueryLogic.QueriesKey);

            return(dic.Select(qh =>
            {
                object?queryName = queryKeys.TryGetC(replace?.TryGetC(qh.Query.Key) ?? qh.Query.Key);
                if (queryName == null)
                {
                    return null; //PreDeleteSqlSync
                }
                if (qh.Columns.Any())
                {
                    var columns = QueryLogic.Queries.GetQuery(queryName).Core.Value.StaticColumns;

                    Synchronizer.SynchronizeReplacing(replacements, "ColumnsOfQuery:" + QueryUtils.GetKey(queryName),
                                                      columns.ToDictionary(a => a.Name),
                                                      qh.Columns.ToDictionary(a => a.ColumnName),
                                                      null,
                                                      (qn, oldQ) => qh.Columns.Remove(oldQ),
                                                      (qn, newQ, oldQ) =>
                    {
                        oldQ.ColumnName = newQ.Name;
                    });

                    foreach (var col in qh.Columns)
                    {
                        col.Description = SynchronizeContent(col.Description, replacements, data) !;
                    }
                }

                qh.Description = SynchronizeContent(qh.Description, replacements, data);

                return table.UpdateSqlSync(qh, h => h.Query.Key == qh.Query.Key);
            }).Combine(Spacing.Simple));
        }
Пример #23
0
        protected virtual ValueTuple[] GetValues(QueryToken queryToken, OmniboxToken omniboxToken)
        {
            if (omniboxToken.IsNull())
            {
                return new[] { new ValueTuple {
                                   Value = null, Match = null
                               } }
            }
            ;

            var ft = QueryUtils.GetFilterType(queryToken.Type);

            switch (ft)
            {
            case FilterType.Integer:
            case FilterType.Decimal:
                if (omniboxToken.Type == OmniboxTokenType.Number)
                {
                    if (ReflectionTools.TryParse(omniboxToken.Value, queryToken.Type, out object?result))
                    {
                        return new[] { new ValueTuple {
                                           Value = result, Match = null
                                       } }
                    }
                    ;
                }
                break;

            case FilterType.String:
                if (omniboxToken.Type == OmniboxTokenType.String)
                {
                    return new[] { new ValueTuple {
                                       Value = OmniboxUtils.CleanCommas(omniboxToken.Value), Match = null
                                   } }
                }
                ;
                break;

            case FilterType.DateTime:
                if (omniboxToken.Type == OmniboxTokenType.String)
                {
                    var str = OmniboxUtils.CleanCommas(omniboxToken.Value);

                    if (ReflectionTools.TryParse(str, queryToken.Type, out object?result))
                    {
                        return new[] { new ValueTuple {
                                           Value = result, Match = null
                                       } }
                    }
                    ;
                }
                break;

            case FilterType.Lite:
                if (omniboxToken.Type == OmniboxTokenType.String)
                {
                    var patten = OmniboxUtils.CleanCommas(omniboxToken.Value);

                    var result = OmniboxParser.Manager.Autocomplete(queryToken.GetImplementations() !.Value, patten, AutoCompleteLimit);

                    return(result.Select(lite => new ValueTuple {
                        Value = lite, Match = OmniboxUtils.Contains(lite, lite.ToString() !, patten)
                    }).ToArray());
                }
                else if (omniboxToken.Type == OmniboxTokenType.Entity)
                {
                    var error = Lite.TryParseLite(omniboxToken.Value, out Lite <Entity>?lite);
                    if (string.IsNullOrEmpty(error))
                    {
                        return new [] { new ValueTuple {
                                            Value = lite
                                        } }
                    }
                    ;
                }
                else if (omniboxToken.Type == OmniboxTokenType.Number)
                {
                    var imp = queryToken.GetImplementations() !.Value;

                    if (!imp.IsByAll)
                    {
                        return(imp.Types.Select(t => CreateLite(t, omniboxToken.Value))
                               .NotNull().Select(t => new ValueTuple {
                            Value = t
                        }).ToArray());
                    }
                }
                break;
Пример #24
0
        public DynamicQueryBucket(object queryName, Func <IDynamicQueryCore> lazyQueryCore, Implementations entityImplementations)
        {
            if (lazyQueryCore == null)
            {
                throw new ArgumentNullException(nameof(lazyQueryCore));
            }

            this.QueryName             = queryName ?? throw new ArgumentNullException(nameof(queryName));
            this.EntityImplementations = entityImplementations;

            this.Core = new ResetLazy <IDynamicQueryCore>(() =>
            {
                var core = lazyQueryCore();

                core.QueryName = QueryName;

                core.StaticColumns.Where(sc => sc.IsEntity).SingleEx(() => "Entity column on {0}".FormatWith(QueryUtils.GetKey(QueryName)));

                core.EntityColumnFactory().Implementations = entityImplementations;

                var errors = core.StaticColumns.Where(sc => sc.Implementations == null && sc.Type.CleanType().IsIEntity());

                if (errors.Any())
                {
                    throw new InvalidOperationException("Column {0} of query '{1}' do(es) not have implementations defined. Use Column extension method".FormatWith(errors.CommaAnd(a => $"'{a.Name}'"), QueryUtils.GetKey(QueryName)));
                }

                return(core);
            });
        }
        /// <summary>
        /// Attempts to merge group columns with select/order columns, returns group columns which were not used in select.
        /// </summary>
        private IEnumerable <QueryGroupColumnData> MergeAndGetOrphanGroupColumns()
        {
            if (_queryParts.Groups.Count == 1)
            {
                foreach (var selectColumn in _queryParts.Select)
                {
                    var shouldReplaceColumnPath =
                        (selectColumn.AggregationType.HasValue && string.IsNullOrEmpty(selectColumn.ColumnPath)) ||
                        (!selectColumn.AggregationType.HasValue && selectColumn.ColumnPath == "Key");

                    if (shouldReplaceColumnPath)
                    {
                        selectColumn.ColumnPath = _queryParts.Groups.First().ColumnPath;
                    }
                }
            }

            foreach (var groupColumn in _queryParts.Groups)
            {
                // in some cases ColumnPath contains wrong ESQ column path
                // because of re-linq tree visit rules. All cases are described:
                Func <QuerySelectColumnData, bool> selector = (column) =>
                                                              // case:
                                                              // .GroupBy(item => new object[] { item.Column<int>("Column1"), item.Column<string>("Column2") })
                                                              // .Select(group => new { Column1 = group.Key[0], Column2 = group.Key[1] })
                                                              column.ColumnPath == QueryUtils.GetIndexMemberName(groupColumn.Position)
                                                              // case:
                                                              // .GroupBy(item => new { Column1 = item.Column<int>("Column1"), Column2 = item.Column<string>("Column2") })
                                                              // .Select(group => new { Alias1 = group.Key.Column1, Alias2 = group.Key.Column2 })
                                                              || column.ColumnPath == QueryUtils.GetAliasMemberName(groupColumn.Alias)
                                                              // case:
                                                              // .GroupBy(item => item.Column<int>("Column1"))
                                                              // .Select(group => new { Alias1 = group.Key })
                                                              || !column.AggregationType.HasValue && column.ColumnPath == QueryUtils.SingleGroupColumnAlias
                                                              // case (aggregate columns have no column path):
                                                              // .GroupBy(item => item.Column<int>("Column1"))
                                                              // .Select(group => new { Alias1 = group.Key, Count = group.Count() })
                                                              || column.AggregationType.HasValue && string.IsNullOrEmpty(column.ColumnPath);

                // get select columns with incorrect column path
                var selectKeyColumns = _queryParts.Select
                                       .Where(selector)
                                       .ToArray();

                // same for order columns
                var orderKeyColumns = _queryParts.Orders
                                      .Where(selector)
                                      .ToArray();

                // 1. if column is contained in both Select/OrderBy AND GroupBy clause, then it will be added to ESQ
                //    in Select/OrderBy block and should not be added again in GroupBy block.
                // 2. if column is only contained in GroupBy clause, it will not be added in Select/OrderBy block
                //    and query will be broken - so we need to add it in GroupBy block, hence yield return.
                if (selectKeyColumns.Any() || orderKeyColumns.Any())
                {
                    selectKeyColumns.ForEach(column => column.ColumnPath = groupColumn.ColumnPath);
                    orderKeyColumns.ForEach(column => column.ColumnPath  = groupColumn.ColumnPath);
                }
                else
                {
                    yield return(groupColumn);
                }
            }
        }
Пример #26
0
 static string QueryKey(object tokenList)
 {
     return("tokens-Query-" + QueryUtils.GetKey(tokenList));
 }
        public void TestRandom()
        {
            int numberOfRuns = TestUtil.NextInt32(Random, 3, 6);

            for (int iter = 0; iter < numberOfRuns; iter++)
            {
                if (VERBOSE)
                {
                    Console.WriteLine(string.Format("TEST: iter={0} total={1}", iter, numberOfRuns));
                }

                int numDocs   = TestUtil.NextInt32(Random, 100, 1000) * RANDOM_MULTIPLIER;
                int numGroups = TestUtil.NextInt32(Random, 1, numDocs);

                if (VERBOSE)
                {
                    Console.WriteLine("TEST: numDocs=" + numDocs + " numGroups=" + numGroups);
                }

                List <BytesRef> groups = new List <BytesRef>();
                for (int i = 0; i < numGroups; i++)
                {
                    string randomValue;
                    do
                    {
                        // B/c of DV based impl we can't see the difference between an empty string and a null value.
                        // For that reason we don't generate empty string groups.
                        randomValue = TestUtil.RandomRealisticUnicodeString(Random);
                    } while ("".Equals(randomValue, StringComparison.Ordinal));
                    groups.Add(new BytesRef(randomValue));
                }
                string[] contentStrings = new string[TestUtil.NextInt32(Random, 2, 20)];
                if (VERBOSE)
                {
                    Console.WriteLine("TEST: create fake content");
                }
                for (int contentIDX = 0; contentIDX < contentStrings.Length; contentIDX++)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.append("real").append(Random.nextInt(3)).append(' ');
                    int fakeCount = Random.nextInt(10);
                    for (int fakeIDX = 0; fakeIDX < fakeCount; fakeIDX++)
                    {
                        sb.append("fake ");
                    }
                    contentStrings[contentIDX] = sb.toString();
                    if (VERBOSE)
                    {
                        Console.WriteLine("  content=" + sb.toString());
                    }
                }

                Directory         dir = NewDirectory();
                RandomIndexWriter w   = new RandomIndexWriter(
                    Random,
                    dir,
                    NewIndexWriterConfig(TEST_VERSION_CURRENT,
                                         new MockAnalyzer(Random)));
                bool          preFlex   = "Lucene3x".Equals(w.IndexWriter.Config.Codec.Name, StringComparison.Ordinal);
                bool          canUseIDV = !preFlex;
                DocValuesType valueType = vts[Random.nextInt(vts.Length)];

                Document doc        = new Document();
                Document docNoGroup = new Document();
                Field    group      = NewStringField("group", "", Field.Store.NO);
                doc.Add(group);
                Field valuesField = null;
                if (canUseIDV)
                {
                    switch (valueType)
                    {
                    case DocValuesType.BINARY:
                        valuesField = new BinaryDocValuesField("group_dv", new BytesRef());
                        break;

                    case DocValuesType.SORTED:
                        valuesField = new SortedDocValuesField("group_dv", new BytesRef());
                        break;

                    default:
                        fail("unhandled type");
                        break;
                    }
                    doc.Add(valuesField);
                }
                Field sort1 = NewStringField("sort1", "", Field.Store.NO);
                doc.Add(sort1);
                docNoGroup.Add(sort1);
                Field sort2 = NewStringField("sort2", "", Field.Store.NO);
                doc.Add(sort2);
                docNoGroup.Add(sort2);
                Field sort3 = NewStringField("sort3", "", Field.Store.NO);
                doc.Add(sort3);
                docNoGroup.Add(sort3);
                Field content = NewTextField("content", "", Field.Store.NO);
                doc.Add(content);
                docNoGroup.Add(content);
                Int32Field id = new Int32Field("id", 0, Field.Store.NO);
                doc.Add(id);
                docNoGroup.Add(id);
                GroupDoc[] groupDocs = new GroupDoc[numDocs];
                for (int i = 0; i < numDocs; i++)
                {
                    BytesRef groupValue;
                    if (Random.nextInt(24) == 17)
                    {
                        // So we test the "doc doesn't have the group'd
                        // field" case:
                        groupValue = null;
                    }
                    else
                    {
                        groupValue = groups[Random.nextInt(groups.size())];
                    }

                    GroupDoc groupDoc = new GroupDoc(
                        i,
                        groupValue,
                        groups[Random.nextInt(groups.size())],
                        groups[Random.nextInt(groups.size())],
                        new BytesRef(string.Format(CultureInfo.InvariantCulture, "{0:D5}", i)),
                        contentStrings[Random.nextInt(contentStrings.Length)]
                        );

                    if (VERBOSE)
                    {
                        Console.WriteLine("  doc content=" + groupDoc.content + " id=" + i + " group=" + (groupDoc.group == null ? "null" : groupDoc.group.Utf8ToString()) + " sort1=" + groupDoc.sort1.Utf8ToString() + " sort2=" + groupDoc.sort2.Utf8ToString() + " sort3=" + groupDoc.sort3.Utf8ToString());
                    }

                    groupDocs[i] = groupDoc;
                    if (groupDoc.group != null)
                    {
                        group.SetStringValue(groupDoc.group.Utf8ToString());
                        if (canUseIDV)
                        {
                            valuesField.SetBytesValue(new BytesRef(groupDoc.group.Utf8ToString()));
                        }
                    }
                    sort1.SetStringValue(groupDoc.sort1.Utf8ToString());
                    sort2.SetStringValue(groupDoc.sort2.Utf8ToString());
                    sort3.SetStringValue(groupDoc.sort3.Utf8ToString());
                    content.SetStringValue(groupDoc.content);
                    id.SetInt32Value(groupDoc.id);
                    if (groupDoc.group == null)
                    {
                        w.AddDocument(docNoGroup);
                    }
                    else
                    {
                        w.AddDocument(doc);
                    }
                }

                DirectoryReader r = w.GetReader();
                w.Dispose();

                // NOTE: intentional but temporary field cache insanity!
                FieldCache.Int32s docIdToFieldId = FieldCache.DEFAULT.GetInt32s(SlowCompositeReaderWrapper.Wrap(r), "id", false);
                int[]             fieldIdToDocID = new int[numDocs];
                for (int i = 0; i < numDocs; i++)
                {
                    int fieldId = docIdToFieldId.Get(i);
                    fieldIdToDocID[fieldId] = i;
                }

                try
                {
                    IndexSearcher s = NewSearcher(r);
                    if (typeof(SlowCompositeReaderWrapper).GetTypeInfo().IsAssignableFrom(s.IndexReader.GetType()))
                    {
                        canUseIDV = false;
                    }
                    else
                    {
                        canUseIDV = !preFlex;
                    }

                    for (int contentID = 0; contentID < 3; contentID++)
                    {
                        ScoreDoc[] hits = s.Search(new TermQuery(new Term("content", "real" + contentID)), numDocs).ScoreDocs;
                        foreach (ScoreDoc hit in hits)
                        {
                            GroupDoc gd = groupDocs[docIdToFieldId.Get(hit.Doc)];
                            assertTrue(gd.score == 0.0);
                            gd.score = hit.Score;
                            int docId = gd.id;
                            assertEquals(docId, docIdToFieldId.Get(hit.Doc));
                        }
                    }

                    foreach (GroupDoc gd in groupDocs)
                    {
                        assertTrue(gd.score != 0.0);
                    }

                    for (int searchIter = 0; searchIter < 100; searchIter++)
                    {
                        if (VERBOSE)
                        {
                            Console.WriteLine("TEST: searchIter=" + searchIter);
                        }

                        string searchTerm      = "real" + Random.nextInt(3);
                        bool   sortByScoreOnly = Random.nextBoolean();
                        Sort   sortWithinGroup = GetRandomSort(sortByScoreOnly);
                        AbstractAllGroupHeadsCollector allGroupHeadsCollector = CreateRandomCollector("group", sortWithinGroup, canUseIDV, valueType);
                        s.Search(new TermQuery(new Term("content", searchTerm)), allGroupHeadsCollector);
                        int[] expectedGroupHeads = CreateExpectedGroupHeads(searchTerm, groupDocs, sortWithinGroup, sortByScoreOnly, fieldIdToDocID);
                        int[] actualGroupHeads   = allGroupHeadsCollector.RetrieveGroupHeads();
                        // The actual group heads contains Lucene ids. Need to change them into our id value.
                        for (int i = 0; i < actualGroupHeads.Length; i++)
                        {
                            actualGroupHeads[i] = docIdToFieldId.Get(actualGroupHeads[i]);
                        }
                        // Allows us the easily iterate and assert the actual and expected results.
                        Array.Sort(expectedGroupHeads);
                        Array.Sort(actualGroupHeads);

                        if (VERBOSE)
                        {
                            Console.WriteLine("Collector: " + allGroupHeadsCollector.GetType().Name);
                            Console.WriteLine("Sort within group: " + sortWithinGroup);
                            Console.WriteLine("Num group: " + numGroups);
                            Console.WriteLine("Num doc: " + numDocs);
                            Console.WriteLine("\n=== Expected: \n");
                            foreach (int expectedDocId in expectedGroupHeads)
                            {
                                GroupDoc expectedGroupDoc = groupDocs[expectedDocId];
                                string   expectedGroup    = expectedGroupDoc.group == null ? null : expectedGroupDoc.group.Utf8ToString();
                                Console.WriteLine(
                                    string.Format(CultureInfo.InvariantCulture,
                                                  "Group:{0,10} score{1:0.0#######,5} Sort1:{2,10} Sort2:{3,10} Sort3:{4,10} doc:{5,10}",
                                                  expectedGroup, expectedGroupDoc.score, expectedGroupDoc.sort1.Utf8ToString(),
                                                  expectedGroupDoc.sort2.Utf8ToString(), expectedGroupDoc.sort3.Utf8ToString(), expectedDocId)
                                    );
                            }
                            Console.WriteLine("\n=== Actual: \n");
                            foreach (int actualDocId in actualGroupHeads)
                            {
                                GroupDoc actualGroupDoc = groupDocs[actualDocId];
                                string   actualGroup    = actualGroupDoc.group == null ? null : actualGroupDoc.group.Utf8ToString();
                                Console.WriteLine(
                                    string.Format(CultureInfo.InvariantCulture,
                                                  "Group:{0,10} score{1:0.0#######,5} Sort1:{2,10} Sort2:{3,10} Sort3:{4,10} doc:{5,10}",
                                                  actualGroup, actualGroupDoc.score, actualGroupDoc.sort1.Utf8ToString(),
                                                  actualGroupDoc.sort2.Utf8ToString(), actualGroupDoc.sort3.Utf8ToString(), actualDocId)
                                    );
                            }
                            Console.WriteLine("\n===================================================================================");
                        }

                        assertArrayEquals(expectedGroupHeads, actualGroupHeads);
                    }
                }
                finally
                {
                    QueryUtils.PurgeFieldCache(r);
                }

                r.Dispose();
                dir.Dispose();
            }
        }
Пример #28
0
        static bool TryParseRemember(Replacements replacements, string tokenString, QueryDescription qd, SubTokensOptions options, out QueryToken result)
        {
            string[] parts = tokenString.Split('.');

            result = null;
            for (int i = 0; i < parts.Length; i++)
            {
                string part = parts[i];

                QueryToken newResult = QueryUtils.SubToken(result, qd, options, part);

                if (newResult != null)
                {
                    result = newResult;
                }
                else
                {
                    if (i == 0)
                    {
                        var        entity       = QueryUtils.SubToken(result, qd, options, "Entity");
                        QueryToken newSubResult = QueryUtils.SubToken(entity, qd, options, part);

                        if (newSubResult != null)
                        {
                            result = newSubResult;
                            continue;
                        }
                    }


                    if (Replacements.AutoReplacement != null)
                    {
                        Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext {
                            ReplacementKey = "QueryToken", OldValue = part, NewValues = result.SubTokens(qd, options).Select(a => a.Key).ToList()
                        });

                        if (sel != null && sel.Value.NewValue != null)
                        {
                            newResult = QueryUtils.SubToken(result, qd, options, sel.Value.NewValue);

                            if (newResult != null)
                            {
                                result = newResult;
                                continue;
                            }
                        }
                    }

                    string key = result == null?QueryKey(qd.QueryName) : TypeKey(result.Type);

                    Dictionary <string, string> dic = replacements.TryGetC(key);

                    if (dic == null)
                    {
                        return(false);
                    }

                    string remainging = parts.Skip(i).ToString(".");

                    string old = dic.Keys.OrderByDescending(a => a.Length).FirstOrDefault(s => remainging.StartsWith(s));

                    if (old == null)
                    {
                        return(false);
                    }

                    var subParts = dic[old].Let(s => s.HasText() ? s.Split('.') : new string[0]);

                    for (int j = 0; j < subParts.Length; j++)
                    {
                        string subPart = subParts[j];

                        QueryToken subNewResult = QueryUtils.SubToken(result, qd, options, subPart);

                        if (subNewResult == null)
                        {
                            return(false);
                        }

                        result = subNewResult;
                    }

                    i += (old == "" ? 0 : old.Split('.').Length) - 1;
                }
            }

            return(true);
        }
Пример #29
0
        public Column ToColumn(QueryDescription qd, bool canAggregate)
        {
            var queryToken = QueryUtils.Parse(token, qd, SubTokensOptions.CanElement | (canAggregate ? SubTokensOptions.CanAggregate : 0));

            return(new Column(queryToken, displayName ?? queryToken.NiceName()));
        }
Пример #30
0
 public Dictionary <string, object> GetQueries()
 {
     return(queries.GetOrAdd(CultureInfo.CurrentCulture, ci =>
                             GetAllQueryNames().ToOmniboxPascalDictionary(qn =>
                                                                          qn is Type t ? (EnumEntity.Extract(t) ?? t).NiceName() : QueryUtils.GetNiceName(qn), qn => qn)));
 }