コード例 #1
0
 internal int IndexNewsItems(IList <INewsItem> newsItems)
 {
     if (newsItems != null)
     {
         for (int i = 0; i < newsItems.Count; i++)
         {
             INewsItem item = newsItems[i];
             if (item != null)
             {
                 try {
                     // we do not always have the content loaded:
                     if (item.ContentType == ContentType.None && item.Feed != null && item.Feed.owner != null)
                     {
                         if (item.Feed.owner != null)
                         {
                             FeedSource handler = (FeedSource)item.Feed.owner;
                             handler.GetCachedContentForItem(item);
                         }
                     }
                     indexModifier.Add(LuceneNewsItemSearch.Document(item), item.Language);
                 } catch (Exception ex) {
                     Log.Error("IndexNewsItems() error", ex);
                 }
             }
         }
         return(newsItems.Count);
     }
     return(0);
 }
コード例 #2
0
 /// <summary>
 /// Removes the index documents of the news items.
 /// </summary>
 /// <param name="newsItems">The news items.</param>
 /// <returns>Number of index documents removed</returns>
 internal void RemoveNewsItems(IList newsItems)
 {
     if (newsItems != null && newsItems.Count > 0)
     {
         //int removed = 0;
         for (int i = 0; i < newsItems.Count; i++)
         {
             INewsItem item = newsItems[i] as INewsItem;
             if (item != null)
             {
                 Term term = new Term(LuceneSearch.IndexDocument.ItemID,
                                      LuceneNewsItemSearch.UID(item));
                 indexModifier.Delete(term);
             }
         }
         //return removed;
     }
     //return 0;
 }
コード例 #3
0
ファイル: LuceneSearch.cs プロジェクト: zzxxhhzxh/RssBandit
        /// <summary>
        /// Executes a search.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="feedSources">The news handlers.</param>
        /// <param name="cultureName">Name of the culture.</param>
        /// <returns></returns>
        public Result ExecuteSearch(SearchCriteriaCollection criteria, INewsFeed[] scope, IEnumerable <FeedSource> feedSources, string cultureName)
        {
            if (!UseIndex)
            {
                return(null);
            }

            Query q = BuildLuceneQuery(criteria, scope, GetAnalyzer(cultureName));

            if (q == null)              // not validated
            {
                return(new Result(0, 0, GetList <SearchHitNewsItem> .Empty, GetArrayList.Empty));
            }

            //TODO: to be fixed -
            // next line causes issues with concurrent thread access to the search index:
            IndexSearcher searcher = new IndexSearcher(this._settings.GetIndexDirectory());
            Hits          hits     = null;

            while (hits == null)
            {
                try {
                    DateTime start = DateTime.Now;
                    hits = searcher.Search(q, Sort.RELEVANCE);
                    TimeSpan timeRequired = TimeSpan.FromTicks(DateTime.Now.Ticks - start.Ticks);
                    _log.Info(String.Format("Found {0} document(s) that matched query '{1}' (time required: {2})", hits.Length(), q, timeRequired));
                } catch (BooleanQuery.TooManyClauses) {
                    BooleanQuery.SetMaxClauseCount(BooleanQuery.GetMaxClauseCount() * 2);
                    _log.Info(String.Format("Search failed with error 'BooleanQuery.TooManyClauses'. Retry with BooleanQuery.MaxClauseCount == {0}", BooleanQuery.GetMaxClauseCount()));
                }
            }

            List <SearchHitNewsItem> items        = new List <SearchHitNewsItem>(hits.Length());
            HybridDictionary         matchedFeeds = new HybridDictionary();


            for (int i = 0; i < hits.Length(); i++)
            {
                Document doc = hits.Doc(i);

                INewsFeed f        = null;
                string    feedLink = doc.Get(Keyword.FeedLink);
                if (matchedFeeds.Contains(feedLink))
                {
                    f = (INewsFeed)matchedFeeds[feedLink];
                }

                if (f == null)
                {
                    foreach (FeedSource h in feedSources)
                    {
                        if (h.IsSubscribed(feedLink))
                        {
                            f = h.GetFeeds()[feedLink];
                            break;
                        }
                    }
                }

                if (f == null)
                {
                    continue;
                }
                SearchHitNewsItem item = new SearchHitNewsItem(f,
                                                               doc.Get(Keyword.ItemTitle),
                                                               doc.Get(Keyword.ItemLink),
                                                               doc.Get(IndexDocument.ItemSummary),
                                                               doc.Get(Keyword.ItemAuthor),
                                                               new DateTime(DateTools.StringToTime(doc.Get(Keyword.ItemDate))),
                                                               LuceneNewsItemSearch.NewsItemIDFromUID(doc.Get(IndexDocument.ItemID)));

                items.Add(item);
                if (!matchedFeeds.Contains(feedLink))
                {
                    matchedFeeds.Add(feedLink, f);
                }
            }


            return(new Result(items.Count, matchedFeeds.Count, items, new ArrayList(matchedFeeds.Values)));
        }