Example #1
0
 private static Query QueryFromStringExpression(SearchCriteriaString c, string field, Analyzer a)
 {
     if (c.WhatKind == StringExpressionKind.RegularExpression)
     {
         throw new NotSupportedException(String.Format(ComponentsText.ExceptionLuceneSearchKindNotSupported, c.WhatKind));
     }
     return(QueryFromStringExpression(c.What, field, a));
 }
Example #2
0
 private static Query QueryFromStringExpression(SearchCriteriaString c, string field, Analyzer a)
 {
     if (c.WhatKind == StringExpressionKind.RegularExpression)
     {
         return(new RegexQuery(new Term(field, c.What)));
     }
     else
     {
         return(QueryFromStringExpression(c.What, field, a));
     }
 }
Example #3
0
        private static Query BuildLuceneQuery(SearchCriteriaCollection criteria, INewsFeed[] scope, Analyzer analyzer)
        {
            BooleanQuery masterQuery = null;
            BooleanQuery bTerms      = new BooleanQuery();
            BooleanQuery bRanges     = new BooleanQuery();

            for (int i = 0; criteria != null && i < criteria.Count; i++)
            {
                ISearchCriteria sc = criteria[i];
                if (sc is SearchCriteriaString)
                {
                    SearchCriteriaString c = (SearchCriteriaString)sc;

                    if (string.IsNullOrEmpty(c.What))
                    {
                        continue;
                    }

                    if (c.Where == SearchStringElement.Undefined)
                    {
                        AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, IndexDocument.ItemContent, analyzer));
                    }
                    else
                    {
                        if ((c.Where & SearchStringElement.Title) > 0)
                        {
                            AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, Keyword.ItemTitle, analyzer));
                        }

                        if ((c.Where & SearchStringElement.Link) > 0)
                        {
                            AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, Keyword.ItemLink, analyzer));
                        }

                        if ((c.Where & SearchStringElement.Content) > 0)
                        {
                            AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, IndexDocument.ItemContent, analyzer));
                        }

                        if ((c.Where & SearchStringElement.Subject) > 0)
                        {
                            AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, Keyword.ItemTopic, analyzer));
                        }

                        if ((c.Where & SearchStringElement.Author) > 0)
                        {
                            AddBooleanClauseShould(bTerms, QueryFromStringExpression(c, Keyword.ItemAuthor, analyzer));
                        }
                    }
                }
                else if (sc is SearchCriteriaAge)
                {
                    SearchCriteriaAge c = (SearchCriteriaAge)sc;
                    Term   left, right;
                    string pastDate       = "19900101",
                           pastDateTime   = "199001010001";
                    string futureDate     = DateTime.Now.AddYears(20).DateToInteger().ToString(NumberFormatInfo.InvariantInfo),
                           futureDateTime = DateTime.Now.AddYears(20).DateToInteger().ToString(NumberFormatInfo.InvariantInfo) + "0001";

                    if (c.WhatRelativeToToday.CompareTo(TimeSpan.Zero) == 0)
                    {
                        // compare date only:
                        //TODO: validate provided date(s) to be in the allowed ranges (pastDate, futureDate)!
                        switch (c.WhatKind)
                        {
                        case DateExpressionKind.Equal:
                            AddBooleanClauseMust(bRanges, new PrefixQuery(new Term(Keyword.ItemDate, c.WhatAsIntDateOnly.ToString(NumberFormatInfo.InvariantInfo))));                                      //itemDate == whatYearOnly;
                            break;

                        case DateExpressionKind.OlderThan:
                            left  = new Term(Keyword.ItemDate, pastDate);
                            right = new Term(Keyword.ItemDate, c.What.DateToInteger().ToString(NumberFormatInfo.InvariantInfo));
                            AddBooleanClauseMust(bRanges, new RangeQuery(left, right, true));                                     // return itemDate < whatYearOnly;
                            break;

                        case DateExpressionKind.NewerThan:
                            left  = new Term(Keyword.ItemDate, c.What.DateToInteger().ToString(NumberFormatInfo.InvariantInfo));
                            right = new Term(Keyword.ItemDate, futureDate);
                            AddBooleanClauseMust(bRanges, new RangeQuery(left, right, true));                                     // return itemDate > whatYearOnly;
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        DateTime dt = DateTime.Now.ToUniversalTime().Subtract(c.WhatRelativeToToday);
                        switch (c.WhatKind)
                        {
                        case DateExpressionKind.OlderThan:
                            left  = new Term(Keyword.ItemDate, pastDateTime);
                            right = new Term(Keyword.ItemDate, DateTools.TimeToString(dt.Ticks, DateTools.Resolution.MINUTE));
                            AddBooleanClauseMust(bRanges, new RangeQuery(left, right, true));
                            break;

                        case DateExpressionKind.NewerThan:
                            left  = new Term(Keyword.ItemDate, DateTools.TimeToString(dt.Ticks, DateTools.Resolution.MINUTE));
                            right = new Term(Keyword.ItemDate, futureDateTime);
                            AddBooleanClauseMust(bRanges, new RangeQuery(left, right, true));
                            break;

                        default:
                            break;
                        }
                    }
                }
                else if (sc is SearchCriteriaDateRange)
                {
                    SearchCriteriaDateRange c = (SearchCriteriaDateRange)sc;

                    Term left  = new Term(Keyword.ItemDate, c.Bottom.DateToInteger().ToString(NumberFormatInfo.InvariantInfo));
                    Term right = new Term(Keyword.ItemDate, c.Top.DateToInteger().ToString(NumberFormatInfo.InvariantInfo));
                    AddBooleanClauseMust(bRanges, new RangeQuery(left, right, true));                     // return itemDate > whatYearOnly;
                }
            }

            // now we build: +(terms...) +ranges
            if (bTerms.GetClauses().Length > 0)
            {
                masterQuery = new BooleanQuery();
                AddBooleanClauseMust(masterQuery, bTerms);
            }

            if (bRanges.GetClauses().Length > 0)
            {
                if (masterQuery != null)
                {
                    AddBooleanClauseMust(masterQuery, bRanges);                     // AND
                }
                else
                {
                    masterQuery = bRanges;
                }
            }

            // +scope
            if (scope != null && scope.Length > 0 && masterQuery != null)
            {
                StringBuilder scopeQuery = new StringBuilder("(");
                for (int i = 0; i < scope.Length; i++)
                {
                    scopeQuery.Append(scope[i].id);
                    scopeQuery.Append(" ");
                }
                scopeQuery[scopeQuery.Length - 1] = ')';
                // AND
                AddBooleanClauseMust(masterQuery, QueryFromStringExpression(scopeQuery.ToString(), IndexDocument.FeedID, analyzer));
            }

            return(masterQuery);
        }