Exemplo n.º 1
0
        /// <summary>
        /// Implements syncing between Lucene Index and Content Repository.
        /// - Cleans up not referenced tag Nodes (not used on any content)
        /// - Imports new tags which are not stored as Tag node in repository.
        /// - Optimizes Lucene Index for accurate search results.
        /// </summary>
        /// <param name="tagPath">Repository path where to import new tags.</param>
        /// <param name="searchPaths">The search paths.</param>
        public static void SynchronizeTags(string tagPath, List <string> searchPaths)
        {
            //1. adding new tags to repositry
            var usedTags = GetAllTags(null, searchPaths);

            if (tagPath != string.Empty)
            {
                foreach (var tag in usedTags)
                {
                    if (!IsInRepository(tag, tagPath.ToLower()))
                    {
                        AddToRepository(tag.ToLower(), tagPath);
                    }
                }
            }
            //2. deleting unused tag nodes
            var query  = LucQuery.Parse("+Type:tag");
            var result = query.Execute();
            var nodes  = result.Select(o => Node.LoadNode(o.NodeId)).ToList();


            foreach (var node in nodes)
            {
                if (!usedTags.Contains(node.DisplayName) && string.IsNullOrWhiteSpace(node.GetProperty <string>("BlackListPath")))
                {
                    node.Delete();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces the given tag to the given new tag on every content in reppository.
        /// Also used for deleting tags, by calling with newtag = String.Empty parameter.
        /// </summary>
        /// <param name="tag">Tag to replace.</param>
        /// <param name="newtag">Tag to replace with.</param>
        /// <param name="pathList">The path list.</param>
        public static void ReplaceTag(string tag, string newtag, List <string> pathList)
        {
            tag    = tag.ToLower();
            newtag = newtag.ToLower();
            if (tag == string.Empty)
            {
                return;
            }
            var query = String.Format("Tags:\"{0}\"", tag);

            if (pathList != null && pathList.Count > 0)
            {
                query = string.Concat(query, String.Format("+InTree:({0})", pathList.Aggregate(String.Empty, (current, path) => String.Concat(current, path, " ")).TrimEnd(' ')));
            }
            var lq     = LucQuery.Parse(query);
            var result = lq.Execute();

            foreach (var tmp in result.Select(item => Node.LoadNode(item.NodeId)))
            {
                if (tmp["Tags"] != null)
                {
                    tmp["Tags"] = (tmp["Tags"].ToString().ToLower().Replace(tag, newtag)).Trim();
                }
                tmp.Save();
            }
        }
Exemplo n.º 3
0
 private void Compile()
 {
     if (_query == null)
     {
         _query = SnExpression.BuildQuery(_queryable.Expression, typeof(T), _queryable.ContextPath, _queryable.ChildrenDefinition);
     }
 }
Exemplo n.º 4
0
 public void Security_ContentQuery_FieldLevel_Parsing()
 {
     Assert.AreEqual(QueryFieldLevel.HeadOnly, LucQuery.Parse("+Name:a* +InTree:/Root/MyFolder -InFolder:/Root/MyFolder/x +Index:0").FieldLevel);
     Assert.AreEqual(QueryFieldLevel.NoBinaryOrFullText, LucQuery.Parse("+Name:a* +InTree:/Root/MyFolder +Description:value1").FieldLevel);
     Assert.AreEqual(QueryFieldLevel.BinaryOrFullText, LucQuery.Parse("+InTree:/Root/MyFolder searchword").FieldLevel);
     Assert.AreEqual(QueryFieldLevel.BinaryOrFullText, LucQuery.Parse("searchword").FieldLevel);
 }
Exemplo n.º 5
0
        private ScoreDoc[] GetDocsUnderTree(string path, bool recurse)
        {
            var field = recurse ? "InTree" : "Path";
            var lq    = LucQuery.Parse(String.Format("{0}:'{1}'", path, path.ToLower()));

            var idxReader = GetIndexReader();
            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
            {
                if (searcher != null)
                {
                    searcher.Close();
                }
                searcher = null;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Determines if the tag given as parameter is presented in content repository or not.
        /// </summary>
        /// <param name="tag">Tag to search.</param>
        /// <param name="tagPath">The tag path.</param>
        /// <returns>True is present, false if not.</returns>
        public static bool IsInRepository(string tag, string tagPath)
        {
            var query  = LucQuery.Parse(String.Concat("+Type:tag +DisplayName:\"", tag.ToLower(), "\""));
            var result = query.Execute();

            return(result.Count() > 0);
        }
Exemplo n.º 7
0
 private void CheckQueryToString(string text, string expected)
 {
     //var visitor = new ToStringVisitor();
     //visitor.Visit(LucQuery.Parse(text).Query);
     //Assert.AreEqual(expected, visitor.ToString());
     Assert.AreEqual(expected, LucQuery.Parse(text).QueryText);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Determines if the given tag is blacklisted or not.
        /// </summary>
        /// <param name="tag">Tag to search on blacklist.</param>
        /// <param name="blacklistPaths">The blacklist paths.</param>
        /// <returns>True if blacklisted, false if not.</returns>
        public static bool IsBlacklisted(string tag, List <string> blacklistPaths)
        {
            LucQuery query;

            // escaping
            if (!string.IsNullOrEmpty(tag))
            {
                tag = tag.Replace("\"", "\\\"");
            }

            using (var readerFrame = LuceneManager.GetIndexReaderFrame())
            {
                var    reader = readerFrame.IndexReader;
                var    terms  = reader.Terms(new Term("BlackListPath", "*"));
                string queryString;

                if (blacklistPaths != null && blacklistPaths.Count > 0)
                {
                    var usedPaths = new List <string>();

                    do
                    {
                        if (terms.Term().Field() == "BlackListPath")
                        {
                            if (blacklistPaths.Any(path => path.ToLower().StartsWith(terms.Term().Text())))
                            {
                                usedPaths.Add(terms.Term().Text());
                            }
                        }
                    } while (terms.Next());

                    var pathString = usedPaths.Aggregate(string.Empty, (current, usedPath) => String.Concat(current, usedPath, " "));

                    pathString = pathString.TrimEnd(' ');

                    queryString = String.Format("+Type:tag +DisplayName:\"{0}\"", tag.ToLower());
                    queryString = String.Concat(queryString, String.IsNullOrEmpty(pathString) ? " +BlackListPath:/root" : String.Format(" +BlackListPath:({0})", pathString));
                }
                else
                {
                    queryString = String.Concat("+Type:tag +DisplayName:\"", tag.ToLower(), "\" +BlackListPath:/root");
                }

                try
                {
                    query = LucQuery.Parse(queryString);
                }
                catch (ParserException)
                {
                    // wrong query: return no
                    Logger.WriteWarning(1, "TagAdmin query parse error: " + queryString);

                    return(false);
                }

                var result = query.Execute();
                return(result.Count() > 0);
            }
        }
Exemplo n.º 9
0
        public void ContentQuery_CountOnly()
        {
            var expectedCount = ContentType.GetContentTypeNames().Length;
            var query         = LucQuery.Parse("Type:ContentType .COUNTONLY .AUTOFILTERS:OFF .LIFESPAN:OFF");
            var result        = query.Execute().ToArray();
            var totalCount    = query.TotalCount;

            Assert.IsTrue(result.Length == 0, String.Format("Result length is: {0}, expected: 0.", result.Length));
            Assert.IsTrue(expectedCount == totalCount, String.Format("TotalCount is: {0}, expected: {1}.", totalCount, expectedCount));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Determines if the given tag is blacklisted or not.
        /// - Uses Lucene query for fast searching
        /// </summary>
        /// <param name="tag">Tag to check</param>
        /// <returns>True if the tag is on blacklist, false if it isn't.</returns>
        private static bool IsBlacklisted(string tag)
        {
            //if (_blackList != null)
            //    return _blackList.Contains(tag);

            var query  = LucQuery.Parse("+TypePath:genericcontent/tag +IsBlacklisted:true +DisplayName:" + tag.ToLower());
            var result = query.Execute();

            return(result.Count() > 0);
        }
Exemplo n.º 11
0
        public void NodeQuery_PathWithWhitespace()
        {
            var nquery = new NodeQuery();

            nquery.Add(new StringExpression(StringAttribute.Path, StringOperator.StartsWith, "/Root/a b"));
            var lquery = LucQuery.Create(nquery);
            var s      = lquery.ToString();

            Assert.IsTrue(s == "InTree:\"/root/a b\" .AUTOFILTERS:OFF");
        }
Exemplo n.º 12
0
 private void Compile()
 {
     if (_query == null)
     {
         var q     = LucQuery.Create(SnExpression.GetPathQuery(_queryable.ContextPath, _queryable.SubTree));
         var query = SnExpression.BuildQuery(_queryable.Expression, typeof(T), _queryable.ContextPath, _queryable.ChildrenDefinition);
         query.AddAndClause(q);
         _query = query;
     }
 }
Exemplo n.º 13
0
        private SortField CreateSortFieldFromExpr(MethodCallExpression node, bool reverse)
        {
            string name;

            var unaryExpr = node.Arguments[1] as UnaryExpression;

            if (unaryExpr == null)
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            var operand = unaryExpr.Operand as LambdaExpression;

            if (operand == null)
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            var memberExpr = operand.Body as MemberExpression;

            if (memberExpr != null)
            {
                name = memberExpr.Member.Name;
                return(LucQuery.CreateSortField(name, reverse));
            }

            var methodCallExpr = operand.Body as MethodCallExpression;

            if (methodCallExpr == null)
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            if (methodCallExpr.Method.Name != "get_Item")
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            if (methodCallExpr.Arguments.Count != 1)
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            var nameExpr = methodCallExpr.Arguments[0] as ConstantExpression;

            if (nameExpr == null)
            {
                throw new Exception("Invalid sort: " + node.ToString());
            }

            name = (string)nameExpr.Value;
            return(LucQuery.CreateSortField(name, reverse));
        }
Exemplo n.º 14
0
        private int ExecuteQueryWithCountOnly(LucQuery lucQuery, FilterStatus enableAutofilters, FilterStatus enableLifespanFilter)
        {
            lucQuery.CountOnly            = true;
            lucQuery.Top                  = 0;
            lucQuery.Skip                 = 0;
            lucQuery.SortFields           = null;
            lucQuery.EnableAutofilters    = enableAutofilters;
            lucQuery.EnableLifespanFilter = enableLifespanFilter;

            lucQuery.Execute();

            return(lucQuery.TotalCount);
        }
Exemplo n.º 15
0
        public void ContentQuery_LucQueryAddSimpleAndClause()
        {
            var inputText      = ".SKIP:10 Name:My* .TOP:5 Meta:'.TOP:6' Type:Folder";
            var extensionText  = "InTree:/Root/JohnSmith";
            var inputQuery     = LucQuery.Parse(inputText);
            var extensionQuery = LucQuery.Parse(extensionText);

            inputQuery.AddAndClause(extensionQuery);
            var combinedAndText = inputQuery.ToString();

            var expectedAndText = "+(Name:my* Meta:.TOP:6 Type:folder) +InTree:/root/johnsmith .TOP:5 .SKIP:10";

            Assert.AreEqual(expectedAndText, combinedAndText);
        }
Exemplo n.º 16
0
        public void ContentQuery_LucQueryAddDoubleOrClause()
        {
            var inputText      = ".SKIP:10 Name:My* .TOP:5 Meta:'.TOP:6' Type:Folder";
            var extensionText  = ".AUTOFILTERS:OFF InTree:/Root/JohnSmith .TOP:100 InTree:/Root/System";
            var inputQuery     = LucQuery.Parse(inputText);
            var extensionQuery = LucQuery.Parse(extensionText);

            inputQuery.AddOrClause(extensionQuery);
            var combinedAndText = inputQuery.ToString();

            var expectedAndText = "(Name:my* Meta:.TOP:6 Type:folder) (InTree:/root/johnsmith InTree:/root/system) .TOP:5 .SKIP:10";

            Assert.AreEqual(expectedAndText, combinedAndText);
        }
Exemplo n.º 17
0
        public void Querying_Analyzers()
        {
            var query = LucQuery.Parse("'Mr.John Smith'");
            var s     = query.ToString();
            var pq    = query.Query as Lucene.Net.Search.PhraseQuery;

            Assert.IsNotNull(pq, String.Concat("Parsed query is: ", pq.GetType().Name, ". Expected: PhraseQuery"));
            var terms = pq.GetTerms();

            Assert.IsTrue(terms.Length == 2, String.Concat("Count of terms is: ", terms.Length, ". Expected: 2"));
            Assert.IsTrue(terms[0].Text() == "mr.john", String.Concat("First term is ", terms[0].Text(), ". Expected: 'mr.john'"));
            Assert.IsTrue(terms[1].Text() == "smith", String.Concat("Second term is ", terms[1].Text(), ". Expected: 'smith'"));

            var qtext = "\"Mr.John Smith\"";

            //var qtext = "(InTree:/Root/Site1/Folder1/Folder2/Folder3 OR InTree:/Root/Site2/Folder1/Folder2/Folder3/Folder5/Folder6) AND Type:Folder AND _Text:\"Mr.John Smith\"";

            Lucene.Net.Search.Query q;
            var k       = 0;
            var stopper = Stopwatch.StartNew();

            for (int i = 0; i < 10000000; i++)
            {
                k++;
            }
            var t0 = stopper.ElapsedMilliseconds;

            stopper.Stop();

            stopper = Stopwatch.StartNew();
            for (int i = 0; i < 1000; i++)
            {
                q = LucQuery.Parse(qtext).Query;
            }
            var t1 = stopper.ElapsedMilliseconds;

            stopper.Stop();

            stopper = Stopwatch.StartNew();
            for (int i = 0; i < 1000; i++)
            {
                q = new Lucene.Net.QueryParsers.QueryParser(LuceneManager.LuceneVersion, "_Text", IndexManager.GetAnalyzer()).Parse(qtext);
            }
            var t2 = stopper.ElapsedMilliseconds;

            stopper.Stop();
        }
Exemplo n.º 18
0
        public void QueryExecutor_ForceTopByConfig_CheckUsage()
        {
            var lq     = LucQuery.Parse("Type:ContentType .TOP:500 .AUTOFILTERS:OFF");
            var result = lq.Execute();

            Assert.IsTrue(lq.TraceInfo.Searches == 1, String.Format("Searches is {0}, expected: 1.", lq.TraceInfo.Searches));

            StorageContext.Search.DefaultTopAndGrowth = new[] { 10, 30, 50, 0 };
            lq     = LucQuery.Parse("Type:ContentType .AUTOFILTERS:OFF");
            result = lq.Execute();
            Assert.IsTrue(lq.TraceInfo.Searches == 4, String.Format("Searches is {0}, expected: 4.", lq.TraceInfo.Searches));

            StorageContext.Search.DefaultTopAndGrowth = new[] { 10, 100, 1000, 10000, 0 };
            lq     = LucQuery.Parse("Type:ContentType .AUTOFILTERS:OFF");
            result = lq.Execute();
            Assert.IsTrue(lq.TraceInfo.Searches == 3, String.Format("Searches is {0}, expected: 3.", lq.TraceInfo.Searches));
        }
Exemplo n.º 19
0
        /// <summary>
        /// Returns the ID-s of Nodes, that contains the tag given as parameter.
        /// </summary>
        /// <param name="tag">Searched tag as String.</param>
        /// <param name="searchPathArray">Paths to search in.</param>
        /// <param name="contentTypeArray">Types to search.</param>
        /// <returns>Node Id list.</returns>
        public static List <int> GetNodeIds(string tag, string[] searchPathArray, string[] contentTypeArray, string queryFilter = "")
        {
            if (String.IsNullOrEmpty(tag))
            {
                return(new List <int>());
            }
            tag = tag.ToLower();
            var ids      = new List <int>();
            var pathList = string.Empty;
            var typeList = string.Empty;

            if (searchPathArray.Count() > 0 && contentTypeArray.Count() > 0)
            {
                pathList = "+Path:(";
                foreach (var path in searchPathArray)
                {
                    pathList = String.Concat(pathList, path, "* ");
                }
                pathList = String.Concat(pathList, ")");

                typeList = "+Type:(";
                foreach (var path in contentTypeArray)
                {
                    typeList = String.Concat(typeList, path, " ");
                }
                typeList = String.Concat(typeList, ")");
            }

            if (tag != string.Empty)
            {
                LucQuery query;
                if (!String.IsNullOrEmpty(pathList) && !String.IsNullOrEmpty(typeList))
                {
                    query = LucQuery.Parse(string.Format("+Tags:\"{0}\" {1} {2} {3}", tag.Trim(), pathList, typeList, queryFilter));
                }
                else
                {
                    query = LucQuery.Parse(string.Format("+Tags:\"{0}\" {1}", tag.Trim(), queryFilter));
                }

                var results = query.Execute();
                ids.AddRange(results.Select(o => o.NodeId));
            }
            return(ids);
        }
Exemplo n.º 20
0
 internal void Remove(Term[] deleteTerms)
 {
     lock (_sync)
     {
         foreach (var deleteTerm in deleteTerms)
         {
             var executor = new QueryExecutor20100701();
             var q        = new TermQuery(deleteTerm);
             var lucQuery = LucQuery.Create(q);
             lucQuery.EnableAutofilters = false;
             var result = executor.Execute(lucQuery, true);
             foreach (var lucObject in result)
             {
                 _storage.Remove(lucObject.VersionId);
             }
         }
     }
 }
Exemplo n.º 21
0
        /// <summary>
        /// Determines if the given tag is blacklisted or not.
        /// </summary>
        /// <param name="tag">Tag to search on blacklist.</param>
        /// <param name="blacklistPaths">The blacklist paths.</param>
        /// <returns>True if blacklisted, false if not.</returns>
        public static bool IsBlacklisted(string tag, List <string> blacklistPaths)
        {
            LucQuery query;

            using (var readerFrame = LuceneManager.GetIndexReaderFrame())
            {
                var reader = readerFrame.IndexReader;
                var terms  = reader.Terms(new Term("BlackListPath", "*"));

                if (blacklistPaths != null && blacklistPaths.Count > 0)
                {
                    var usedPaths = new List <string>();

                    do
                    {
                        if (terms.Term().Field() == "BlackListPath")
                        {
                            if (blacklistPaths.Any(path => path.ToLower().StartsWith(terms.Term().Text())))
                            {
                                usedPaths.Add(terms.Term().Text());
                            }
                        }
                    } while (terms.Next());

                    var pathString = usedPaths.Aggregate(string.Empty, (current, usedPath) => String.Concat(current, usedPath, " "));

                    pathString = pathString.TrimEnd(' ');

                    var queryString = String.Format("+Type:tag +DisplayName:\"{0}\"", tag.ToLower());

                    queryString = String.Concat(queryString, String.IsNullOrEmpty(pathString) ? " +BlackListPath:/root" : String.Format(" +BlackListPath:({0})", pathString));

                    query = LucQuery.Parse(queryString);
                }
                else
                {
                    query = LucQuery.Parse(String.Concat("+Type:tag +DisplayName:\"", tag.ToLower(), "\" +BlackListPath:/root"));
                }

                var result = query.Execute();
                return(result.Count() > 0);
            }
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
        public static QueryInfo Classify(LucQuery query, int top, int skip, SortField[] orders, bool countOnly, bool allVersions)
        {
            var sortfieldNames = orders == null ? new List <string>() : orders.Select(x => x.GetField()).ToList();
            var queryInfo      = new QueryInfo
            {
                Query          = query,
                SortFields     = orders,
                Top            = top,
                Skip           = skip,
                SortFieldNames = sortfieldNames,
                CountOnly      = countOnly,
                AllVersions    = allVersions
            };

            var visitor = new QueryClassifierVisitor(queryInfo);

            visitor.Visit(query.Query);

            return(queryInfo);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Get the search result.
        /// </summary>
        /// <param name="from">value of searching from</param>
        /// <param name="to">value of searching to</param>
        /// <returns>List of nodes which matches the search.</returns>
        private IEnumerable GetResult(string from, string to)
        {
            if (from == string.Empty || to == string.Empty)
            {
                return(null);
            }
            decimal fromD = 0;
            decimal toD   = 5;

            Decimal.TryParse(from, out fromD);
            Decimal.TryParse(to, out toD);
            fromD -= (decimal)0.01;
            toD   += (decimal)0.01;

            var contentsId = new List <int>();
            var query      =
                LucQuery.Parse("Rate:{" + string.Format("{0} TO {1}", fromD.ToString("F2"), toD.ToString("F2")) + "}");
            var results = query.Execute();

            foreach (var o in results)
            {
                contentsId.Add(o.NodeId);
            }

            var result = new NodeList <Node>(contentsId);

            return(result);

            //Demo query!!!
            //var queryString = "civic";

            //var contentsId = TagManager.GetNodeIds(queryString);
            //var result = new NodeList<Node>(contentsId);

            //return result;
        }
Exemplo n.º 25
0
        private static LucQuery BuildLucQuery(System.Linq.Expressions.Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef)
        {
            Query q0 = null;

            CQVisitor v = null;

            // #1 compiling linq expression
            if (expression != null)
            {
                var v1    = new SetExecVisitor();
                var expr1 = v1.Visit(expression);
                var expr2 = expr1;
                if (v1.HasParameter)
                {
                    var v2 = new ExecutorVisitor(v1.GetExpressions());
                    expr2 = v2.Visit(expr1);
                }
                v = new CQVisitor();
                v.Visit(expr2);
                q0 = v.GetQuery(sourceCollectionItemType, childrenDef);
            }

            // #2 combining with additional query clause
            LucQuery lq = null;

            if (!string.IsNullOrEmpty(childrenDef.ContentQuery))
            {
                lq = LucQuery.Parse(childrenDef.ContentQuery);
                if (q0 == null)
                {
                    q0 = lq.Query;
                }
                else
                {
                    q0 = CombineQueries(q0, lq.Query);
                }
            }

            // #3 combining with context path
            if (q0 == null)
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = GetPathQuery(contextPath, childrenDef.PathUsage == PathUsageMode.InTreeAnd || childrenDef.PathUsage == PathUsageMode.InTreeOr);
                }
            }
            else
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = CombinePathQuery(q0, contextPath, childrenDef.PathUsage);
                }
            }

            // #4 empty query is invalid in this place
            if (q0 == null)
            {
                throw new NotSupportedException("Cannot execute empty query. Expression: " + expression);
            }

            var q1 = OptimizeBooleans(q0);

            // #5 configuring query by linq expression (the smallest priority)
            var query = LucQuery.Create(q1);

            if (v != null)
            {
                query.Skip          = v.Skip;
                query.Top           = v.Top;
                query.CountOnly     = v.CountOnly;
                query.SortFields    = v.SortFields.ToArray();
                query.ThrowIfEmpty  = v.ThrowIfEmpty;
                query.ExistenceOnly = v.ExistenceOnly;
            }
            // #6 configuring query by children definition
            if (childrenDef != null)
            {
                if (childrenDef.Skip > 0)
                {
                    query.Skip = childrenDef.Skip;
                }
                if (childrenDef.Top > 0)
                {
                    query.Top = childrenDef.Top;
                }
                if (childrenDef.Sort != null)
                {
                    query.SetSort(childrenDef.Sort);
                }
                if (childrenDef.CountAllPages != null)
                {
                    query.CountAllPages = childrenDef.CountAllPages.Value;
                }
                if (childrenDef.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = childrenDef.EnableAutofilters;
                }
                if (childrenDef.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = childrenDef.EnableLifespanFilter;
                }
                if (childrenDef.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = childrenDef.QueryExecutionMode;
                }
            }

            // #7 configuring query by additional query clause (the biggest priority)
            if (lq != null)
            {
                if (lq.Skip > 0)
                {
                    query.Skip = lq.Skip;
                }
                if (lq.Top > 0)
                {
                    query.Top = lq.Top;
                }
                if (lq.SortFields != null && lq.SortFields.Length > 0)
                {
                    query.SortFields = lq.SortFields;
                }
                if (lq.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = lq.EnableAutofilters;
                }
                if (lq.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = lq.EnableLifespanFilter;
                }
                if (lq.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = lq.QueryExecutionMode;
                }
            }

            return(query);
        }
Exemplo n.º 26
0
        public void Indexing_RecoverStoredField()
        {
            var ctdTemplate      = @"<?xml version='1.0' encoding='utf-8'?>
                <ContentType name='{0}' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'>
                  <Fields>
                    <Field name='BodyColorIndexingTests' type='Color'>
                      <Indexing>
                        {2}
                        <IndexHandler>{1}</IndexHandler>
                      </Indexing>
                    </Field>
                  </Fields>
                </ContentType>";
            var ctdCleanTemplate = @"<?xml version='1.0' encoding='utf-8'?>
                <ContentType name='{0}' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'>
                </ContentType>";
            var typeName         = "TypeForIndexingTest";
            var fieldHandlerName = typeof(ColorWithNameIndexHandler).FullName;


            Content content = null;

            ContentTypeInstaller.InstallContentType(String.Format(ctdTemplate, typeName, fieldHandlerName, "<Store>No</Store>"));
            var contentType = ContentType.GetByName(typeName); //-- typesystem load back

            var query = LucQuery.Parse("BodyColorIndexingTests:Red");

            try
            {
                //-- without storing: there is query result but there is not value to recover
                content = Content.CreateNew(typeName, TestRoot, "car");
                content["BodyColorIndexingTests"] = Color.Red;
                content.Save();

                var doc = query.Execute().FirstOrDefault();
                Assert.IsNotNull(doc, "Query result document is null");
                Assert.IsTrue(!doc.Names.Contains("BodyColorIndexingTests"), "Document has BodyColorIndexingTests field.");

                //-- clean
                content.ForceDelete();
                ContentTypeInstaller.InstallContentType(String.Format(ctdCleanTemplate, typeName));
                contentType = ContentType.GetByName(typeName); //-- typesystem load back

                //-- with storing: there are query result and value to recover
                ContentTypeInstaller.InstallContentType(String.Format(ctdTemplate, typeName, fieldHandlerName, "<Store>Yes</Store>"));
                content = Content.CreateNew(typeName, TestRoot, "car");
                content["BodyColorIndexingTests"] = Color.Red;
                content.Save();

                doc = query.Execute().FirstOrDefault();
                Assert.IsNotNull(doc, "Query result document is null");
                var fieldValueColor  = doc.Get <Color>("BodyColorIndexingTests");
                var fieldValueObject = doc.Get("BodyColorIndexingTests");
                Assert.IsTrue(SenseNet.ContentRepository.Fields.ColorField.ColorToString(fieldValueColor) == SenseNet.ContentRepository.Fields.ColorField.ColorToString(Color.Red), "Colors are not equal.");
            }
            finally
            {
                if (content != null)
                {
                    content.ForceDelete();
                }
                ContentTypeInstaller.RemoveContentType(typeName);
            }
        }
Exemplo n.º 27
0
        private string CheckQueryObject(string src, string qstring, string qqstring, FilterStatus autofilters, FilterStatus lifespan, int?top, int?skip, bool?countOnly, params string[] sortnames)
        {
            var query = LucQuery.Parse(src);

            var realqstring     = query.ToString();
            var realqqstring    = query.Query.ToString();
            var realtop         = query.Top;
            var realskip        = query.Skip;
            var realCountOnly   = query.CountOnly;
            var sortcount       = query.HasSort ? query.SortFields.Length : 0;
            var realsortnames   = new string[sortcount];
            var realautofilters = query.EnableAutofilters;
            var reallifespan    = query.EnableLifespanFilter;

            for (int i = 0; i < sortcount; i++)
            {
                if (i + 1 <= sortcount)
                {
                    var name    = query.SortFields[i].GetField();
                    var reverse = query.SortFields[i].GetReverse();
                    realsortnames[i] = (reverse ? "-" : "+") + name;
                }
            }
            var msg = new StringBuilder();

            if (qstring != null)
            {
                if (qstring != realqstring)
                {
                    msg.Append("qstring='").Append(realqstring).Append("' expected='").Append(qstring).AppendLine("'");
                }
            }
            if (qqstring != null)
            {
                if (qqstring != realqqstring)
                {
                    msg.Append("qqstring='").Append(realqqstring).Append("' expected='").Append(qqstring).AppendLine("'");
                }
            }
            if (top != null)
            {
                if (top.Value != realtop)
                {
                    msg.Append("top=").Append(realtop).Append(" expected=").Append(top).AppendLine();
                }
            }
            if (skip != null)
            {
                if (skip.Value != realskip)
                {
                    msg.Append("skip=").Append(realskip).Append(" expected=").Append(skip).AppendLine();
                }
            }
            if (countOnly != null)
            {
                if (countOnly.Value != realCountOnly)
                {
                    msg.Append("countOnly=").Append(realCountOnly).Append(" expected=").Append(countOnly).AppendLine();
                }
            }
            var s  = String.Join(", ", sortnames);
            var ss = String.Join(", ", realsortnames);

            if (s != ss)
            {
                msg.Append("sortnames='").Append(ss).Append("' expected='").Append(s).AppendLine("'");
            }
            if (realautofilters != autofilters)
            {
                msg.Append("Autofilters='").Append(realautofilters).Append("' expected='").Append(autofilters).AppendLine("'");
            }
            if (reallifespan != lifespan)
            {
                msg.Append("Lifespan='").Append(reallifespan).Append("' expected='").Append(lifespan).AppendLine("'");
            }

            return(msg.Length == 0 ? null : msg.ToString());
        }
Exemplo n.º 28
0
        public void IndexingHistory_FixAddUpdateOverlap()
        {
            // add overlaps with update
            var fieldvalue1 = "IndexingHistoryFixAddUpdateOverlapFirst";
            var fieldvalue2 = "IndexingHistoryFixAddUpdateOverlapSecond";

            var history = LuceneManager._history;

            var car = new GenericContent(TestRoot, "Car");

            car.Name = Guid.NewGuid().ToString();
            car.Save();
            var id = car.Id;


            // init 1
            var node1 = Node.LoadNode(id);

            node1["Description"] = fieldvalue1;
            node1.Save();
            // delete changes from index
            DataRowTimestampTest.DeleteVersionFromIndex(node1.VersionId);
            DataRowTimestampTest.DeleteVersionIdFromIndexingHistory(node1.VersionId);

            var docInfo1  = IndexDocumentInfo.Create(node1, false);
            var docData1  = DataBackingStore.CreateIndexDocumentData(node1, docInfo1, null, null);
            var document1 = IndexDocumentInfo.CreateDocument(docInfo1, docData1);


            // init 2
            var node2 = Node.LoadNode(id);

            node2["Description"] = fieldvalue2;
            node2.Save();
            // delete changes from index
            DataRowTimestampTest.DeleteVersionFromIndex(node2.VersionId);
            DataRowTimestampTest.DeleteVersionIdFromIndexingHistory(node2.VersionId);

            var docInfo2  = IndexDocumentInfo.Create(node2, false);
            var docData2  = DataBackingStore.CreateIndexDocumentData(node2, docInfo2, null, null);
            var document2 = IndexDocumentInfo.CreateDocument(docInfo2, docData2);


            // 1 check indexing history
            var versionId1 = history.GetVersionId(document1);
            var timestamp1 = history.GetTimestamp(document1);
            var historyOk  = LuceneManager._history.CheckForAdd(versionId1, timestamp1);

            // timestamp in indexing history should be the newest
            var actTimestamp1 = history.Get(versionId1);

            Assert.AreEqual(timestamp1, actTimestamp1, "Timestamp in indexing history did not change.");
            Assert.IsTrue(historyOk, "History indicates indexing should not be executed, but this is not true.");


            // 2 check indexing history
            var versionId2 = history.GetVersionId(document2);
            var timestamp2 = history.GetTimestamp(document2);

            historyOk = LuceneManager._history.CheckForUpdate(versionId2, timestamp2);


            // timestamp in indexing history should change
            var actTimestamp2 = history.Get(versionId2);

            Assert.AreEqual(timestamp2, actTimestamp2, "Timestamp in indexing history did not change.");
            Assert.IsTrue(historyOk, "History indicates indexing should not be executed, but this is not true.");


            // 2 index
            var document   = document2;
            var updateTerm = UpdateDocumentActivity.GetIdTerm(document);

            LuceneManager.SetFlagsForUpdate(document);
            LuceneManager._writer.UpdateDocument(updateTerm, document);


            var firstfound = ContentQuery.Query("Description:" + fieldvalue1, new QuerySettings {
                EnableAutofilters = FilterStatus.Disabled
            }).Count;
            var secondfound = ContentQuery.Query("Description:" + fieldvalue2, new QuerySettings {
                EnableAutofilters = FilterStatus.Disabled
            }).Count;

            // check indexing occured correctly
            // thread 2 writes values in index
            Assert.AreEqual(0, firstfound);
            Assert.AreEqual(1, secondfound);


            // 1 index
            document = document1;
            LuceneManager._writer.AddDocument(document);


            firstfound = ContentQuery.Query("Description:" + fieldvalue1, new QuerySettings {
                EnableAutofilters = FilterStatus.Disabled
            }).Count;
            secondfound = ContentQuery.Query("Description:" + fieldvalue2, new QuerySettings {
                EnableAutofilters = FilterStatus.Disabled
            }).Count;

            // check indexing occured correctly
            // thread 1 adds values to index, duplication occurs
            Assert.AreEqual(1, firstfound);
            Assert.AreEqual(1, secondfound);


            // 1 detects problem
            var detectChange1 = history.CheckHistoryChange(versionId1, timestamp1);

            Assert.IsTrue(detectChange1, "Thread 1 did not detect indexing overlapping although it should have.");

            // 2 detects no problem
            var detectChange2 = history.CheckHistoryChange(versionId2, timestamp2);

            Assert.IsFalse(detectChange2, "Thread 2 detected indexing overlapping although it should not have.");


            // 1 fixes index
            LuceneManager.RefreshDocument(versionId1, false);

            firstfound = ContentQuery.Query("Description:" + fieldvalue1, new QuerySettings {
                EnableAutofilters = FilterStatus.Disabled
            }).Count;
            //secondfound = ContentQuery.Query("Description:" + fieldvalue2, new QuerySettings { EnableAutofilters = FilterStatus.Disabled }).Count;
            var q = LucQuery.Parse("Description:" + fieldvalue2);

            q.EnableAutofilters = FilterStatus.Disabled;
            secondfound         = q.Execute(true).Count();

            Assert.AreEqual(0, firstfound);
            Assert.AreEqual(1, secondfound);

            var node = Node.LoadNode(id);

            node.ForceDelete();
        }
Exemplo n.º 29
0
 public void Initialize(LucQuery lucQuery, IPermissionFilter permissionChecker)
 {
     LucQuery          = lucQuery;
     PermissionChecker = permissionChecker;
 }
Exemplo n.º 30
0
        /// <summary>
        /// Add or edit a saved content query.
        /// </summary>
        /// <param name="content">A query content to modify, a user, or any content under a workspace.</param>
        /// <param name="query">Query text.</param>
        /// <param name="displayName">Display name for the saved query.</param>
        /// <param name="queryType">Type of the query.</param>
        /// <returns></returns>
        public static object SaveQuery(Content content, string query, string displayName, QueryType queryType)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

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

            try
            {
                // We need to validate the query to avoid saving unknown texts.
                LucQuery.Parse(query);
            }
            catch (Exception ex)
            {
                Logger.WriteWarning(EventId.Query.ParseError, "Content query parse error during query save.", properties: new Dictionary <string, object>
                {
                    { "User", User.Current.Name },
                    { "Query text", query },
                    { "Error", ex.Message }
                });

                return(string.Empty);
            }

            ContentList queryContainer = null;
            Content     queryContent   = null;

            switch (queryType)
            {
            case QueryType.Private:

                //load the user and his profile
                var user = content.ContentHandler as User ?? (User)User.Current;
                if (!user.IsProfileExist())
                {
                    user.CreateProfile();
                }

                var profile = user.Profile;
                if (profile == null)
                {
                    throw new InvalidOperationException("User profile could not be created.");
                }

                queryContainer = GetQueryContainer(profile);
                break;

            case QueryType.Public:
                //store the query under the current workspace
                queryContainer = GetQueryContainer(((GenericContent)content.ContentHandler).Workspace);
                if (queryContainer == null)
                {
                    throw new InvalidOperationException("Query container could not be created for a public query. Content: " + content.Path);
                }
                break;

            case QueryType.NonDefined:
                if (!content.ContentType.IsInstaceOfOrDerivedFrom(QueryTypeName))
                {
                    throw new InvalidOperationException("If the query type is nondefined, the content must be a query to save.");
                }
                queryContent = content;
                break;

            default:
                throw new InvalidOperationException("Unknown query type: " + queryType);
            }

            if (queryContainer != null)
            {
                //create a new query under the previously found container
                queryContent = Content.CreateNew(QueryTypeName, queryContainer, null);
            }

            if (queryContent == null)
            {
                throw new InvalidOperationException("No query content to save.");
            }

            // Elevation: a simple user does not necessarily have
            // 'Add' permission for the public queries folder.
            using (new SystemAccount())
            {
                if (!string.IsNullOrEmpty(displayName))
                {
                    queryContent.DisplayName = displayName;
                }

                queryContent["Query"] = query;
                queryContent.Save();
            }

            return(queryContent);
        }