public QueryResult <int> ExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context) { var result = Retrier.Retry(SearchServiceClient.RetryCount, SearchServiceClient.RetryWaitMilliseconds, typeof(CommunicationException), () => SearchServiceClient.Instance.ExecuteQuery(query, GetQueryContext(query, context))); return(new QueryResult <int>(result.Hits, result.TotalCount)); }
private void RewritingTest(string inputQuery, bool isValid, string expected) { var context = new TestQueryContext(QuerySettings.AdminSettings, 0, _indexingInfo, new TestQueryEngine(null, null)); using (SenseNet.Tests.Tools.Swindle(typeof(SnQuery), "_permissionFilterFactory", new EverythingAllowedPermissionFilterFactory())) { var queryIn = SnQuery.Parse(inputQuery, context); var snQueryAcc = new PrivateType(typeof(SnQuery)); snQueryAcc.InvokeStatic("PrepareQuery", queryIn, context); var hasError = false; var visitor = new SharingVisitor(); SnQuery queryOut = null; try { queryOut = SnQuery.Create(visitor.Visit(queryIn.QueryTree)); } catch (InvalidContentSharingQueryException) { hasError = true; } Assert.AreNotEqual(isValid, hasError); if (!hasError) { Assert.AreEqual(expected, queryOut.ToString()); } } }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, ServiceQueryContext queryContext) { var context = new GrpcService.ServiceQueryContext() { UserId = queryContext.UserId, FieldLevel = queryContext.FieldLevel }; context.DynamicGroups.AddRange(queryContext.DynamicGroups); try { var result = _searchClient.ExecuteQueryAndProject(new GrpcService.QueryRequest() { Query = Tools.Serialize(query), Context = context }); return(new QueryResult <string>(result.Hits, result.TotalCount)); } catch (Exception ex) { throw LogAndFormatException(ex, "ExecuteQueryAndProject"); } }
private ScoreDoc[] GetDocsUnderTree(string path, bool recurse) { var field = recurse ? "InTree" : "Path"; var queryContext = SnQueryContext.CreateDefault(); var snQuery = SnQuery.Parse($"{field}:'{path.ToLower()}'", null); var lq = Compile(snQuery, queryContext); //LucQuery.Parse(String.Format("{0}:'{1}'", field, path.ToLower())); using (var readerFrame = LuceneSearchManager.GetIndexReaderFrame()) { var idxReader = readerFrame.IndexReader; var searcher = new IndexSearcher(idxReader); var numDocs = idxReader.NumDocs(); try { var collector = TopScoreDocCollector.Create(numDocs, false); searcher.Search(lq.Query, collector); var topDocs = collector.TopDocs(0, numDocs); return(topDocs.ScoreDocs); } finally { searcher.Close(); } } }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, ServiceQueryContext queryContext) { using (var client = WcfServiceClient.GetSearchServiceContract()) { return(client.ExecuteQueryAndProject(query, queryContext)); } }
public QueryResult <int> ExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context) { if (_intResults.TryGetValue(query.Querytext, out var result)) { return(result); } return(QueryResult <int> .Empty); }
private void Compile() { if (_query == null) { _query = SnExpression.BuildQuery(_queryable.Expression, typeof(T), _queryable.ContextPath, _queryable.ChildrenDefinition); } }
//TODO: Part of 'CQL to SQL compiler' for future use. public QueryResult <int> TryExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context) { //var queryInfo = SnQueryClassifier.Classify(query, context.AllVersions); //if (SnLucToSqlCompiler.TryCompile(QueryInfo.Query.QueryTree, QueryInfo.Top, QueryInfo.Skip, // QueryInfo.SortFields, QueryInfo.CountOnly, out _sqlQueryText, out _sqlParameters)) return(null); // means: cannot execute }
public QueryResult <int> ExecuteQuery(SnQuery query, ServiceQueryContext queryContext) { var filter = GetPermissionFilter(query, queryContext); var lucQuery = Compile(query, null); var lucQueryResult = lucQuery.Execute(filter, null); var hits = lucQueryResult?.Select(x => x.NodeId).ToArray() ?? new int[0]; return(new QueryResult <int>(hits, lucQuery.TotalCount)); }
private static ServiceQueryContext GetQueryContext(SnQuery query, IQueryContext context) { return(new ServiceQueryContext { UserId = context.UserId, FieldLevel = PermissionFilter.GetFieldLevel(query).ToString(), DynamicGroups = SystemAccount.Execute(() => Node.Load <User>(context.UserId)?.GetDynamicGroups(0)?.ToArray() ?? new int[0]) }); }
private void Compile() { if (_query == null) { var query = SnExpression.BuildQuery(_queryable.Expression, typeof(T), _queryable.ContextPath, _queryable.ChildrenDefinition); query.AddAndClause(SnExpression.GetPathPredicate(_queryable.ContextPath, _queryable.SubTree)); _query = query; } }
private void NormalizerVisitorTest(SnQueryPredicate inputPredicate, string expectedQuery) { var context = new TestQueryContext(QuerySettings.AdminSettings, 0, _indexingInfo, new TestQueryEngine(null, null)); using (SenseNet.Tests.Tools.Swindle(typeof(SnQuery), "_permissionFilterFactory", new EverythingAllowedPermissionFilterFactory())) { var queryIn = SnQuery.Create(inputPredicate); NormalizerVisitorTest(queryIn, context, expectedQuery); } }
/// <summary> /// Compiles an <see cref="SnQuery"/> to <see cref="LucQuery"/>. /// </summary> public LucQuery Compile(SnQuery snQuery, IQueryContext context) { var visitor = new SnQueryToLucQueryVisitor(_masterAnalyzer, context); visitor.Visit(snQuery.QueryTree); var searchManager = ((ILuceneIndexingEngine)IndexManager.IndexingEngine).LuceneSearchManager; return(LucQuery.Create(visitor.Result, searchManager)); }
public QueryResult <int> ExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context) { _log.AppendLine($"ExecuteQuery: {query}"); var interpreter = new SnQueryInterpreter(_index); var result = interpreter.Execute(query, filter, out var totalCount); var nodeIds = result.Select(h => h.NodeId).ToArray(); var queryResult = new QueryResult <int>(nodeIds, totalCount); return(queryResult); }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, IPermissionFilter filter, IQueryContext context) { var projection = query.Projection ?? IndexFieldName.NodeId; var converter = !(context.GetPerFieldIndexingInfo(projection).IndexFieldHandler is IIndexValueConverter indexFieldHandler) ? DefaultConverter : indexFieldHandler.GetBack; var result = Retrier.Retry(SearchServiceClient.RetryCount, SearchServiceClient.RetryWaitMilliseconds, typeof(CommunicationException), () => SearchServiceClient.Instance.ExecuteQueryAndProject(query, GetQueryContext(query, context))); return(new QueryResult <string>(result.Hits.Select(h => converter(h)?.ToString()), result.TotalCount)); }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, IPermissionFilter filter, IQueryContext context) { _log.AppendLine($"ExecuteQueryAndProject: {query}"); var interpreter = new SnQueryInterpreter(_index); var result = interpreter.Execute(query, filter, out var totalCount); var projectedValues = result.Select(h => h.ValueForProject).Distinct().ToArray(); var queryResult = new QueryResult <string>(projectedValues, totalCount); return(queryResult); }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, ServiceQueryContext queryContext) { var filter = GetPermissionFilter(query, queryContext); var lucQuery = Compile(query, null); var projection = query.Projection ?? IndexFieldName.NodeId; var lucQueryResult = lucQuery.Execute(filter, null); var hits = lucQueryResult? .Select(x => x[projection, false]) .Where(r => !string.IsNullOrEmpty(r)) .ToArray() ?? new string[0]; return(new QueryResult <string>(hits, lucQuery.TotalCount)); }
private static IPermissionFilter GetPermissionFilter(SnQuery query, ServiceQueryContext queryContext) { var security = new SecurityHandler(new ServiceSecurityContext(new SearchUser { Id = queryContext.UserId, DynamicGroups = queryContext.DynamicGroups }, _securitySystem)); if (!Enum.TryParse(queryContext.FieldLevel, true, out QueryFieldLevel queryFieldLevel)) { queryFieldLevel = QueryFieldLevel.HeadOnly; } return(new ServicePermissionFilter(security, queryFieldLevel, query.AllVersions)); }
private void NormalizerVisitorTest(SnQuery query, TestQueryContext context, string expectedQuery) { query.EnableAutofilters = FilterStatus.Disabled; query.EnableLifespanFilter = FilterStatus.Disabled; var snQueryAcc = new PrivateType(typeof(SnQuery)); snQueryAcc.InvokeStatic("PrepareQuery", query, context); var normalizer = new SharingNormalizerVisitor(); var normalized = normalizer.Visit(query.QueryTree); var newQuery = SnQuery.Create(normalized); Assert.AreEqual(expectedQuery, newQuery.ToString()); }
public QueryResult <int> ExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context) { using (var op = SnTrace.Query.StartOperation("InMemoryQueryEngine: ExecuteQuery: {0}", query)) { _log.AppendLine($"ExecuteQuery: {query}"); var interpreter = new SnQueryInterpreter(this.Index); var result = interpreter.Execute(query, filter, out var totalCount); var nodeIds = result.Select(h => h.NodeId).ToArray(); var queryResult = new QueryResult <int>(nodeIds, totalCount); op.Successful = true; return(queryResult); } }
public bool IsTrue(SnQuery predication) { Visit(predication.QueryTree); if (_hitStack.Count == 0) { throw new CompilerException("Compiler error: The stack does not contain any elements."); } if (_hitStack.Count != 1) { throw new CompilerException($"Compiler error: The stack contains more than one elements ({_hitStack.Count})."); } var result = _hitStack.Pop(); return(result); }
/// <inheritdoc /> public QueryResult <string> ExecuteQueryAndProject(SnQuery query, IPermissionFilter filter, IQueryContext context) { var lucQuery = Compile(query, context); var projection = query.Projection ?? IndexFieldName.NodeId; var indexFieldHandler = context.GetPerFieldIndexingInfo(projection).IndexFieldHandler as IIndexValueConverter; var converter = indexFieldHandler == null ? DefaultConverter : indexFieldHandler.GetBack; var lucQueryResult = lucQuery.Execute(filter, context); var hits = lucQueryResult? .Select(x => x[projection, false]) .Where(r => !string.IsNullOrEmpty(r)) .Select(q => converter(q).ToString()) .ToArray() ?? new string[0]; return(new QueryResult <string>(hits, lucQuery.TotalCount)); }
public QueryResult <string> ExecuteQueryAndProject(SnQuery query, IPermissionFilter filter, IQueryContext context) { using (var op = SnTrace.Query.StartOperation("InMemoryQueryEngine: ExecuteQueryAndProject: {0}", query)) { _log.AppendLine($"ExecuteQueryAndProject: {query}"); var interpreter = new SnQueryInterpreter(this.Index); var result = interpreter.Execute(query, filter, out var totalCount); var projectedValues = result.Select(h => h.ValueForProject).Distinct().ToArray(); var queryResult = new QueryResult <string>(projectedValues, totalCount); op.Successful = true; return(queryResult); } }
public void SnQuery_Result_Int() { var intResults = new Dictionary <string, QueryResult <int> > { { "asdf", new QueryResult <int>(new[] { 1, 2, 3 }, 4) } }; var context = new TestQueryContext(QuerySettings.AdminSettings, 0, _indexingInfo, new TestQueryEngine(intResults, null)); using (Tools.Swindle(typeof(SnQuery), "_permissionFilterFactory", new EverythingAllowedPermissionFilterFactory())) { var queryText = "asdf"; var result = SnQuery.Query(queryText, context); var expected = string.Join(", ", intResults[queryText].Hits.Select(x => x.ToString()).ToArray()); var actual = string.Join(", ", result.Hits.Select(x => x.ToString()).ToArray()); Assert.AreEqual(actual, expected); Assert.AreEqual(result.TotalCount, intResults[queryText].TotalCount); } }
private Tuple <SnQuery, string> CreateQueryAndPrepare(string queryText, FilterStatus autoFilters, FilterStatus lifespanFilter) { var parser = new CqlParser(); var context = new TestQueryContext(QuerySettings.AdminSettings, 1, _indexingInfo); var query = new SnQuery { Querytext = queryText, QueryTree = parser.Parse(queryText, context).QueryTree, EnableAutofilters = autoFilters, EnableLifespanFilter = lifespanFilter }; SnQuery.PrepareQuery(query, context); var visitor = new SnQueryToStringVisitor(); visitor.Visit(query.QueryTree); return(new Tuple <SnQuery, string>(query, visitor.Output)); }
private static LucQuery Compile(SnQuery query, IQueryContext context) { var analyzer = SearchManager.Instance.GetAnalyzer(); var visitor = new SnQueryToLucQueryVisitor(analyzer, context); visitor.Visit(query.QueryTree); var result = LucQuery.Create(visitor.Result, SearchManager.Instance); result.Skip = query.Skip; result.Top = query.Top; result.SortFields = query.Sort?.Select(s => CreateSortField(s.FieldName, s.Reverse)).ToArray() ?? new SortField[0]; result.EnableAutofilters = query.EnableAutofilters; result.EnableLifespanFilter = query.EnableLifespanFilter; result.QueryExecutionMode = query.QueryExecutionMode; result.CountOnly = query.CountOnly; result.CountAllPages = query.CountAllPages; return(result); }
public void SnQuery_Result_String() { var stringResults = new Dictionary <string, QueryResult <string> > { { "asdf", new QueryResult <string>(new[] { "1", "2", "3" }, 4) } }; var context = new TestQueryContext(QuerySettings.AdminSettings, 0, _indexingInfo, new TestQueryEngine(null, stringResults)); using (Tools.Swindle(typeof(SnQuery), "_permissionFilterFactory", new EverythingAllowedPermissionFilterFactory())) { var queryText = "asdf"; var result = SnQuery.QueryAndProject(queryText, context); var expected = string.Join(", ", stringResults[queryText].Hits); var actual = string.Join(", ", result.Hits); Assert.AreEqual(actual, expected); Assert.AreEqual(result.TotalCount, stringResults[queryText].TotalCount); } }
private static void AggregateSettings(SnQuery query, QuerySettings settings) { var settingsTop = settings?.Top ?? int.MaxValue; query.Top = Math.Min( settingsTop == 0 ? int.MaxValue : settingsTop, query.Top == 0 ? int.MaxValue : query.Top); if (settings == null) { return; } if (settings.Skip > 0) { query.Skip = settings.Skip; } if (settings.Sort != null && settings.Sort.Any()) { query.Sort = settings.Sort.ToArray(); } if (settings.EnableAutofilters != FilterStatus.Default) { query.EnableAutofilters = settings.EnableAutofilters; } if (settings.EnableLifespanFilter != FilterStatus.Default) { query.EnableLifespanFilter = settings.EnableLifespanFilter; } if (settings.QueryExecutionMode != QueryExecutionMode.Default) { query.QueryExecutionMode = settings.QueryExecutionMode; } if (settings.AllVersions) { query.AllVersions = true; } }
public void Sharing_Query_Rewriting_FieldNames() { var inputQuery = "-a:a Sharing:s0 SharedWith:123 SharedBy:s2 SharingMode:s3 SharingLevel:s4"; var expectedQuery = "-a:a Sharing:s0 Sharing:I123 Sharing:s2 Sharing:s3 Sharing:s4"; string actualQuery; var context = new TestQueryContext(QuerySettings.AdminSettings, 0, _indexingInfo, new TestQueryEngine(null, null)); using (SenseNet.Tests.Tools.Swindle(typeof(SnQuery), "_permissionFilterFactory", new EverythingAllowedPermissionFilterFactory())) { var queryIn = SnQuery.Parse(inputQuery, context); var snQueryAcc = new PrivateType(typeof(SnQuery)); snQueryAcc.InvokeStatic("PrepareQuery", queryIn, context); var visitor = new SharingVisitor(); var newTree = visitor.Visit(queryIn.QueryTree); var snQuery = SnQuery.Create(newTree); actualQuery = snQuery.ToString(); } Assert.AreEqual(expectedQuery, actualQuery); }
public IEnumerable <Hit> Execute(SnQuery query, IPermissionFilter filter, out int totalCount) { Visit(query.QueryTree); if (_hitStack.Count == 0) { throw new CompilerException("Compiler error: The stack does not contain any elements."); } if (_hitStack.Count != 1) { throw new CompilerException($"Compiler error: The stack contains more than one elements ({_hitStack.Count})."); } var foundVersionIds = _hitStack.Pop(); IEnumerable <Hit> permittedHits = foundVersionIds.Select(v => GetHitByVersionId(v, query.Projection, query.Sort)).Where(h => filter.IsPermitted(h.NodeId, h.IsLastPublic, h.IsLastDraft)); var sortedHits = GetSortedResult(permittedHits, query.Sort).ToArray(); totalCount = sortedHits.Length; var result = sortedHits.Skip(query.Skip).Take(query.Top > 0 ? query.Top : totalCount).ToArray(); return(result); }