Exemplo n.º 1
0
 public static IndexDocument[] Search(string query, string defaultField, string orderBy, int resultCount)
 {
     Query q = new Query(query);
     q.DefaultField = defaultField;
     q.OrderBy = orderBy;
     q.StartResultIndex = 0;
     q.EndResultIndex = resultCount;
     return Search(new Query(query));
 }
Exemplo n.º 2
0
 public override Query BuildQuery(Query current)
 {
     Mubble.Models.Controllers.Query q = this.Parent.Content as Mubble.Models.Controllers.Query;
     if (q != null)
     {
         current.Add(q.GetSpecifiedQuery());
     }
     return current;
 }
Exemplo n.º 3
0
        public static QueryResults GetQueryResults(Query q, bool aggressiveCaching, out string cacheKey)
        {
            int startIndex = q.StartResultIndex;
            int endIndex = q.EndResultIndex;

            if (endIndex < 50 && aggressiveCaching)
            {
                q.StartResultIndex = 0;
                q.EndResultIndex = 50;
            }

            int hashCode = q.GetHashCode();

            cacheKey = CacheKey(q);

            QueryResults finalResults = null;
            Cacheable results = null;
            try
            {
                using (queryLock.Lock(cacheKey, defaultTimeout))
                {
                    results = HttpRuntime.Cache[cacheKey] as Cacheable;
                    if (results == null)
                    {
                        results = RunQueryAndCache(q, cacheKey);
                    }
                }
            }
            catch (NamedLock<Query>.TimeoutException timeout)
            {
                throw new QueryBrokerException(
                    string.Format("The timeout period expired when trying to retrieve query results: {0}", hashCode),
                    timeout
                    );
            }
            if (results == null || results.Results == null)
            {
                throw new QueryBrokerException("Unable to retrieve query results");
            }
            if (results.LastRunTime < lastIndexUpdate)
            {
                QueueQuery(q, cacheKey, results);
            }
            finalResults = QueryResults.BuildFromRawQuery(results.Results, CachedQueryObjectLoader);
            if (startIndex != finalResults.FirstIndex || endIndex != finalResults.LastIndex)
            {
                finalResults = finalResults.GetRange(startIndex, endIndex);
            }

            return finalResults;
        }
Exemplo n.º 4
0
 public static List<Content> Query(string query, int startIndex, int showCount)
 {
     Query q = new Query(query);
     q.StartResultIndex = startIndex;
     q.EndResultIndex = showCount + startIndex;
     q.OrderBy = "PublishDate DESC";
     return Query(q);
 }
Exemplo n.º 5
0
        public void Add(Query q)
        {
            if (q == null) return;
            if (this.Text != null && this.Text.Length > 0) this.Text += " " + q.Text;
            else this.Text = q.Text;

            if (q.OrderBy != null && q.OrderBy != this.OrderBy)
            {
                this.OrderBy = q.OrderBy;
            }

            if (q.DefaultField != null && q.DefaultField != this.DefaultField)
            {
                this.DefaultField = q.DefaultField;
            }

            foreach (QueryClause t in q.Terms)
            {
                this.Add(t);
            }
        }
Exemplo n.º 6
0
 public static Query Parse(XmlDocument xml, Query q)
 {
     XmlNode root = xml.FirstChild;
     q.OrderBy = root.Attributes["orderBy"] != null ? root.Attributes["orderBy"].Value : q.OrderBy;
     BooleanClause bc = new BooleanClause(
         GetBoolAttribute(root, "required", false),
         GetBoolAttribute(root, "excluded", false)
         );
     foreach (XmlNode node in root.ChildNodes)
     {
         ParseTag(node, bc);
     }
     q.Add(bc);
     return q;
 }
Exemplo n.º 7
0
 public static Query Parse(XmlDocument xml)
 {
     Query q = new Query();
     return Parse(xml, q);
 }
Exemplo n.º 8
0
        public static ContentList Query(Query query, string[] groups, string[] permissions)
        {
            if (groups != null && groups.Length > 0 && permissions != null && permissions.Length > 0)
            {
                foreach (string permission in permissions)
                {
                    BooleanClause bc = new BooleanClause(true, false);
                    foreach (string role in groups)
                    {
                        bc.AddClause(
                            new TermClause(
                                string.Format("GroupWith{0}Permissions", permission),
                                role,
                                false,
                                false)
                          );
                    }
                    query.Add(bc);
                }
            }

            if (query.OrderBy == null) query.OrderBy = "PublishDate DESC";
            if (query.DefaultField == null) query.DefaultField = "Text";

            ContentList results = new ContentList();
            try
            {

                IndexDocument[] docs = Engine.Search(query);
                Dictionary<Guid, bool> usedResults = new Dictionary<Guid, bool>(docs.Length);

                results.StartIndex = query.StartResultIndex;
                results.EndIndex = query.EndResultIndex;
                results.TotalResults = query.TotalResults;

                for (int i = 0; i < docs.Length; i++)
                {
                    Guid id = new Guid(docs[i].Get("ActiveObjectsID"));
                    if (!usedResults.ContainsKey(id))
                    {
                        usedResults.Add(id, true);
                        results.Add(new Content(docs[i]));
                    }
                    else
                    {
                        LogDuplicate(id, docs[i].Get("Title"));
                    }
                }
                usedResults.Clear();
            }
            catch { }

            return results;
        }
Exemplo n.º 9
0
 static Cacheable RunQueryAndCache(Query q, string cacheKey)
 {
     return RunQueryAndCache(q, cacheKey, null);
 }
Exemplo n.º 10
0
        static void QueueQuery(Query q, string cacheKey, Cacheable current)
        {
            lock (queuedQueries)
            {
                if (!queuedQueries.ContainsKey(cacheKey))
                {
                    queuedQueries.Add(cacheKey, DateTime.Now);

                    Worker.Queue(
                        new QueryRunner(SafeRunQueryAndCache),
                        q,
                        cacheKey,
                        current
                        );
                }
            }
        }
Exemplo n.º 11
0
 static string CacheKey(Query q)
 {
     return string.Concat("Query[", q.GetHashCode(), "]");
 }
Exemplo n.º 12
0
        public override Query BuildQuery(Query current)
        {
            System.Web.HttpRequest request = this.Parent.Page.Request;
            System.Web.HttpResponse response = this.Parent.Page.Response;
            string freetext = request.QueryString["search"];
            Query q = new Query();

            bool isValid = false;
            if (freetext != null && freetext.Length > 0)
            {
                q.Text += freetext;
                isValid = true;
            }
            if ("true".Equals(request.QueryString["featured"], StringComparison.CurrentCultureIgnoreCase))
            {
                q.AddTerm("IsFeatured", "true", true, false);
                q.AddTerm("IsFeatured", "false", false, true);
                isValid = true;
            }

            string t = request.QueryString["tag"];
            if (t != null && t.Trim().Length > 0)
            {
                string[] tags = t.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                Regex rx = new Regex(@"(?<Term>\w+)(?<Boost>\^\d+)");
                foreach (string tag in tags)
                {
                    Match m = rx.Match(tag);
                    if (m.Success)
                    {
                        q.Add(new TermClause("Tag", m.Groups["Term"].Value, float.Parse(m.Groups["Boost"].Value.Substring(1))));
                    }
                    else
                    {
                        q.AddTerm("Tag", t, true, false);
                    }
                }
                isValid = true;
            }
            string author = request.QueryString["author"];
            if (author != null && author.Length > 0)
            {
                q.AddTerm("Author", author, true, false);
                isValid = true;
            }
            string section = request.QueryString["path"];
            if (section != null && section.Length > 1)
            {
                q.AddTerm("Path", section + "*", true, true, false);
                isValid = true;
            }

            if ("score".Equals(request.QueryString["sort"], StringComparison.CurrentCultureIgnoreCase))
            {
                q.OrderBy = "Score";
            }

            current.Add(q);

            if (!isValid)
            {
                current.IsValid = false;
            }

            return current;
        }
Exemplo n.º 13
0
 public abstract IndexDocument[] Query(Query query);
Exemplo n.º 14
0
 public static IndexDocument[] Search(Query query)
 {
     return CurrentEngine.Query(query);
 }
Exemplo n.º 15
0
 public static ContentList Query(Query query)
 {
     return Query(query, null);
 }
Exemplo n.º 16
0
 public static ContentList Query(Query query, string[] groups)
 {
     return Query(query, groups, new string[] {"View"});
 }
Exemplo n.º 17
0
        static Cacheable RunQueryAndCache(Query q, string cacheKey, Cacheable current)
        {
            ContentList results = Content.Query(q);
            Cacheable updated = new Cacheable(results, DateTime.Now);

            if (current == null || current.Results != null || current.Results.Count < 2 || results.Count > 0)
            {
                CacheBroker.Set(cacheKey, updated, Config.Caching.Current.GetSlidingExpiration());
                if (CachedQueryChanged != null && (current == null || !results.Equals(current.Results)))
                {
                    CachedQueryChanged(cacheKey);

                }
            }

            lock (queuedQueries)
            {
                queuedQueries.Remove(cacheKey);
            }

            return updated;
        }
Exemplo n.º 18
0
 static Cacheable SafeRunQueryAndCache(Query q, string cacheKey, Cacheable current)
 {
     try
     {
         return RunQueryAndCache(q, cacheKey, current);
     }
     catch (Exception ex)
     {
         log.Error("Running background query failed", ex);
         return null;
     }
 }
Exemplo n.º 19
0
        public IActiveCollection GetData(int startIndex, int endIndex)
        {
            Query q = new Query();
            q.OrderBy = this.OrderBy;
            q.StartResultIndex = startIndex;
            if (endIndex > startIndex)
            {
                q.EndResultIndex = endIndex;
                q.EndResultIndex += this.GroupItems.Count;
            }

            foreach (IQueryFilter filter in this.Filters)
            {
                filter.Parent = this.Parent;
                q = filter.BuildQuery(q);
            }

            if (q.IsValid)
            {
                q.RestrictByGroups(IndexPermissions.View, Security.User.GetRoles());

                string queryKey = null;
                QueryResults qr = QueryBroker.GetQueryResults(q, this.AggressiveCaching, out queryKey);

                    //QueryBroker.GetQueryResults(q, !this.Asynchronous, this.AggressiveCaching, out queryKey);

                if (this.CacheAgainstQuery)
                {
                    Parent.Page.SetQueryResultDependency(queryKey);
                }
                if (qr == null) return null;

                return qr;
            }
            else
            {
                return null;
            }
        }