Ejemplo n.º 1
0
        void AddSearch(ISearch target)
        {
            target.Id = Guid.NewGuid();
            target.CreatedOnUtc = System.DateTime.UtcNow;

            var search = new Search();
            search.Id = target.Id;
            search.CreatedOnUtc = target.CreatedOnUtc;
            search.SearchName = target.SearchName;
            target.SearchTerms.ForEach(x =>
            {
                x.Id = Guid.NewGuid();
                x.CreatedOnUtc = System.DateTime.UtcNow;
                search.SearchTerms.Add(new SearchTerm
                {
                    CreatedOnUtc = x.CreatedOnUtc,
                    Id = x.Id,
                    Score = x.Score,
                    Term = x.Term
                });
            });
            using (var ctx = new SearcherEntities())
            {
                ctx.Searches.Add(search);
                ctx.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public void SaveSearchTest_WhenUpdate()
        {
            var target = GetTarget();
            var targetSearch = new SearchModel();
            targetSearch.SearchName = Guid.NewGuid().ToString();
            targetSearch.CreatedOnUtc = System.DateTime.UtcNow;
            var t1 = new SearchTermModel();
            t1.Term = "Static";
            t1.Score = 1;
            var t2 = new SearchTermModel();
            t2.Term = "Dynamic";
            t2.Score = 2;
            targetSearch.SearchTerms = new List<ISearchTerm>();
            targetSearch.SearchTerms.Add(t1);
            targetSearch.SearchTerms.Add(t2);
            target.SaveSearch(targetSearch);

            //check existing
            using (var ctx = new SearcherEntities())
            {
                var existing = ctx.Searches.Single(x => x.SearchName == targetSearch.SearchName);

                var existingT1 = existing.SearchTerms.Single(x => x.Term == t1.Term);
                var existingT2 = existing.SearchTerms.Single(x => x.Term == t2.Term);
                Assert.AreEqual(t1.Score, existingT1.Score, "First Term Scores do not match");
                Assert.AreEqual(t2.Score, existingT2.Score, "Second Term Scores do not match");

                targetSearch.Id = existing.Id;

            }
            var t3 = new SearchTermModel();
            t3.Term = "Brand New";
            t3.Score = 3;

            targetSearch.SearchTerms.Remove(t1);
            t2.Score = 100;
            targetSearch.SearchTerms.Add(t3);
            target.SaveSearch(targetSearch);
            using (var ctx = new SearcherEntities())
            {
                var existing = ctx.Searches.Single(x => x.Id == targetSearch.Id);
                var newTerm = existing.SearchTerms.Single(x => x.Term == t3.Term);
                var deleted = existing.SearchTerms.SingleOrDefault(x => x.Term == t1.Term);
                Assert.IsNull(deleted, "Deleted Term exists in Database");
                var existingTerm = existing.SearchTerms.SingleOrDefault(x => x.Term == t2.Term);
                Assert.IsTrue(existingTerm.Score == t2.Score, "Updated Score does not match");
            }
        }
        //
        // GET: /SearchResults/
        public ActionResult Index()
        {
            var model = new List<SearchResultData>();
            using (var ctx = new SearcherEntities())
            {

                var created = System.DateTime.Now.AddDays(-5);
                model.AddRange(
                    ctx.SearchResultDatas.Where(
                        x =>
                            x.Hidden == false &&
                            x.PostDateTime>created &&
                            x.KeywordScore > 105 &&
                            x.AppliedDateTime.HasValue == false).ToList());
            }
            return View(model);
        }
 public ActionResult SaveChanges(FormCollection form)
 {
     using (var context = new SearcherEntities())
     {
         form.AllKeys.Where(x => x.Contains("PostId_")).ToList().ForEach(x =>
         {
             var id = Guid.Parse(x.Replace("PostId_", ""));
             context.SearchResultDatas.Single(z => z.Id == id).AppliedDateTime = System.DateTime.Now;
         });
         form.AllKeys.Where(x => x.Contains("Hidden_")).ToList().ForEach(x =>
         {
             var id = Guid.Parse(x.Replace("Hidden_", ""));
             context.SearchResultDatas.Single(z => z.Id == id).Hidden= true;
         });
         context.SaveChanges();
     }
     return RedirectToAction("Index");
 }
Ejemplo n.º 5
0
        public List<SearchResult> GetSearchResults(string urlFilePath, List<string> searchTerms, Dictionary<string, int> scoredKeywords, SearcherBase searcher)
        {
            var collectorStrategy = new HtmlCollectorStrategyBase();
            var searchResultType = searcher.GetType().ToString();

            using (var ctx = new SearcherEntities())
            {
                collectorStrategy.ExcludeUrls = ctx.SearchResultDatas.Where(x=>x.PostType==searchResultType).Select(x => x.PostUrl).ToList();
            }
            collectorStrategy.ScoredKeywords = scoredKeywords;
            collectorStrategy.SearchBaseUrls = System.IO.File.ReadAllLines(urlFilePath).ToList();
            collectorStrategy.SearchTerms = searchTerms;
            if (searcher.GetType() == typeof (CraigslistSearcher))
                InitializeCraigslistSearcher(searcher);
            collectorStrategy.Searcher = searcher;
            collectorStrategy.CollectSearchQueries();
            collectorStrategy.CollectSearches();
            collectorStrategy.ScoreResults();
            PersistSearchResults(collectorStrategy.SearchResults, searchResultType);
            return collectorStrategy.SearchResults;
        }
Ejemplo n.º 6
0
        void PersistSearchResults(List<SearchResult> results,string postType)
        {
            var searchResultDatas = new List<SearchResultData>();
            results.ToList().ForEach(x =>
            {
                var searchResultData = new SearchResultData
                {
                    Id = Guid.NewGuid(),
                    CreatedOnUtc = System.DateTime.UtcNow,
                    PostDateTime = x.PostDate,
                    PostUrl = x.Uri.ToString(),
                    PostId = x.Key,
                    PostTitle = x.Title,
                    PostType = postType,
                    KeywordScore = x.KeywordScore

                };
                searchResultDatas.Add(searchResultData);

            });
            using (var ctx = new SearcherEntities())
            {

                ctx.SearchResultDatas.AddRange(searchResultDatas);
                ctx.SaveChanges();
            }
        }
Ejemplo n.º 7
0
        void UpdateSearch(ISearch target)
        {
            using (var ctx = new SearcherEntities())
            {
                var original = ctx.Searches.Single(x => x.Id == target.Id);
                var targetTerms = target.SearchTerms.Select(x => x.Term.ToLower());
                var sourceTerms = original.SearchTerms.Select(x => x.Term.ToLower());
                //handle deletes
                original.SearchTerms.Where(x => !targetTerms.Contains(x.Term.ToLower()))
                    .ToList()
                    .ForEach(x =>
                    {
                        ctx.SearchTerms.Remove(x);
                    });

                //handle updates to existing
                original.SearchTerms.ToList().ForEach(x =>
                {
                    var updated = target.SearchTerms.Single(n => n.Term.ToLower() == x.Term.ToLower());
                    if (updated.Score != x.Score)
                    {
                        x.Score = updated.Score;
                        x.UpdatedOnUtc = System.DateTime.UtcNow;
                    }
                });
                //handle inserts
                target.SearchTerms.Where(x => !sourceTerms.Contains(x.Term.ToLower())).ToList().ForEach(x =>
                {
                    var newTerm = new SearchTerm
                    {
                        Id = Guid.NewGuid(),
                        CreatedOnUtc = System.DateTime.UtcNow,
                        Score = x.Score,
                        Term = x.Term
                    };
                    original.SearchTerms.Add(newTerm);
                });
                ctx.SaveChanges();
            }
        }