private static Expression<Func<PlaceName, bool>> AsciiEquivalentMatches(string term, StringMatchStrategy matchStrategy, StringComparison? stringComparison = null) { Expression<Func<PlaceName, bool>> expression; switch (matchStrategy) { case StringMatchStrategy.Equals: if (stringComparison.HasValue) expression = name => name.AsciiEquivalent.Equals(term, stringComparison.Value); else expression = name => name.AsciiEquivalent.Equals(term); break; case StringMatchStrategy.StartsWith: if (stringComparison.HasValue) expression = name => name.AsciiEquivalent.StartsWith(term, stringComparison.Value); else expression = name => name.AsciiEquivalent.StartsWith(term); break; case StringMatchStrategy.Contains: if (stringComparison.HasValue) expression = name => name.AsciiEquivalent.Contains(term, stringComparison.Value); else expression = name => name.AsciiEquivalent.Contains(term); break; default: throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); } return AsciiEquivalentIsNotNull().And(expression).Expand(); }
private static Expression<Func<EstablishmentUrl, bool>> TextMatches(string term, StringMatchStrategy matchStrategy, StringComparison? stringComparison = null) { Expression<Func<EstablishmentUrl, bool>> expression; switch (matchStrategy) { case StringMatchStrategy.Equals: if (stringComparison.HasValue) expression = url => url.Value.Equals(term, stringComparison.Value); else expression = url => url.Value.Equals(term); break; case StringMatchStrategy.StartsWith: if (stringComparison.HasValue) expression = url => url.Value.StartsWith(term, stringComparison.Value); else expression = url => url.Value.StartsWith(term); break; case StringMatchStrategy.Contains: if (stringComparison.HasValue) expression = url => url.Value.Contains(term, stringComparison.Value); else expression = url => url.Value.Contains(term); break; default: throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); } return expression; }
public static Expression<Func<EstablishmentUrl, bool>> SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison? stringComparison = null) { var textMatches = TextMatches(term, matchStrategy, stringComparison).Expand() ; var officialUrlMatches = IsOfficialUrl() .And ( textMatches ) ; var nonOfficialNameMatches = IsNotOfficialUrl() //.And //( // TranslationToLanguageMatchesCurrentUiCulture() //) .And ( textMatches ) .Expand() ; var urlMatches = officialUrlMatches .Or ( nonOfficialNameMatches ) ; return urlMatches; }
internal static IQueryable<Establishment> WithName(this IQueryable<Establishment> queryable, string term, StringMatchStrategy matchStrategy) { var names = QueryEstablishmentNames.SearchTermMatches(term, matchStrategy); // ReSharper disable ConvertClosureToMethodGroup Expression<Func<Establishment, bool>> establishments = establishment => establishment.Names.Any(name => names.Invoke(name)); // ReSharper restore ConvertClosureToMethodGroup return queryable.AsExpandable().Where(establishments.Expand()); }
private static Expression<Func<Place, bool>> NonOfficialNameMatches(string term, StringMatchStrategy matchStrategy) { // using this predicate causes the 'name' parameter to not be bound // fixed: http://stackoverflow.com/questions/10689506/unable-to-refactor-using-linq-to-entities-and-linqkit-predicatebuilder var names = QueryPlaceNames.SearchTermMatches(term, matchStrategy); // ReSharper disable ConvertClosureToMethodGroup Expression<Func<Place, bool>> places = place => place.Names.Any(name => names.Invoke(name)); // ReSharper restore ConvertClosureToMethodGroup return places.Expand(); }
internal static IQueryable<Place> WithName(this IQueryable<Place> queryable, string term, StringMatchStrategy matchStrategy) { var matchesName = OfficialNameMatches(term, matchStrategy) //.Or //( // NonOfficialNameMatches(term, matchStrategy) //) ; return queryable.AsExpandable().Where(matchesName); }
internal static IQueryable<Person> WithEmail(this IQueryable<Person> queryable, string term, StringMatchStrategy matchStrategy) { queryable = queryable.Where(EmailValueMatches(term, matchStrategy)); if (matchStrategy == StringMatchStrategy.Equals) { var person = queryable.SingleOrDefault(); queryable = person != null ? new Collection<Person> { person }.AsQueryable() : Enumerable.Empty<Person>().AsQueryable(); } return queryable; }
private static Expression<Func<Place, bool>> OfficialNameMatches(string term, StringMatchStrategy matchStrategy) { switch (matchStrategy) { case StringMatchStrategy.Equals: return place => place.OfficialName.Equals(term); case StringMatchStrategy.StartsWith: return place => place.OfficialName.StartsWith(term); case StringMatchStrategy.Contains: return place => place.OfficialName.Contains(term); } throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); }
private static Expression<Func<Person, bool>> FirstNameMatches(string term, StringMatchStrategy matchStrategy) { switch (matchStrategy) { case StringMatchStrategy.Equals: return person => person.FirstName != null && person.FirstName.Equals(term, StringComparison.OrdinalIgnoreCase); case StringMatchStrategy.StartsWith: return person => person.FirstName != null && person.FirstName.ToLower().StartsWith(term.ToLower()); case StringMatchStrategy.Contains: return person => person.FirstName != null && person.FirstName.Contains(term); } throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); }
public static Expression<Func<PlaceName, bool>> SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison? stringComparison = null) { var nameMatches = TranslationToLanguageMatchesCurrentUiCulture() .And ( TextMatches(term, matchStrategy, stringComparison) .Or ( AsciiEquivalentMatches(term, matchStrategy, stringComparison) ) .Expand() ) ; return nameMatches; }
private static Expression <Func <PlaceName, bool> > TextMatches(string term, StringMatchStrategy matchStrategy, StringComparison?stringComparison = null) { Expression <Func <PlaceName, bool> > expression; switch (matchStrategy) { case StringMatchStrategy.Equals: if (stringComparison.HasValue) { expression = name => name.Text.Equals(term, stringComparison.Value); } else { expression = name => name.Text.Equals(term); } break; case StringMatchStrategy.StartsWith: if (stringComparison.HasValue) { expression = name => name.Text.StartsWith(term, stringComparison.Value); } else { expression = name => name.Text.StartsWith(term); } break; case StringMatchStrategy.Contains: if (stringComparison.HasValue) { expression = name => name.Text.Contains(term, stringComparison.Value); } else { expression = name => name.Text.Contains(term); } break; default: throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); } return(expression); }
public virtual JsonResult WithEmail(string term, StringMatchStrategy matchStrategy = StringMatchStrategy.Equals) { if (string.IsNullOrWhiteSpace(term)) return Json(null); var people = _services.QueryProcessor.Execute( new FindPeopleWithEmailQuery { Term = term, TermMatchStrategy = matchStrategy, EagerLoad = PersonInfoEagerLoad, OrderBy = new Dictionary<Expression<Func<Person, object>>, OrderByDirection> { { p => p.Emails.FirstOrDefault(e => e.IsDefault).Value, OrderByDirection.Ascending }, }, } ); var models = Mapper.Map<PersonInfoModel[]>(people); return Json(models); }
public static Expression<Func<EstablishmentName, bool>> SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison? stringComparison = null) { var textOrAsciiMatches = TextMatches(term, matchStrategy, stringComparison) .Or ( AsciiEquivalentMatches(term, matchStrategy, stringComparison) ) .Expand() ; var officialNameMatches = IsOfficialName() .And ( textOrAsciiMatches ) ; var nonOfficialNameMatches = IsNotOfficialName() .And ( TranslationToLanguageMatchesCurrentUiCulture() ) .And ( textOrAsciiMatches ) .Expand() ; var nameMatches = officialNameMatches .Or ( nonOfficialNameMatches ) ; return nameMatches; }
public virtual JsonResult WithEmail(string term, StringMatchStrategy matchStrategy = StringMatchStrategy.Equals) { if (string.IsNullOrWhiteSpace(term)) { return(Json(null)); } var people = _services.QueryProcessor.Execute( new FindPeopleWithEmailQuery { Term = term, TermMatchStrategy = matchStrategy, EagerLoad = PersonInfoEagerLoad, OrderBy = new Dictionary <Expression <Func <Person, object> >, OrderByDirection> { { p => p.Emails.FirstOrDefault(e => e.IsDefault).Value, OrderByDirection.Ascending }, }, } ); var models = Mapper.Map <PersonInfoModel[]>(people); return(Json(models)); }
public static Expression <Func <EstablishmentName, bool> > SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison?stringComparison = null) { var textOrAsciiMatches = TextMatches(term, matchStrategy, stringComparison) .Or ( AsciiEquivalentMatches(term, matchStrategy, stringComparison) ) .Expand() ; var officialNameMatches = IsOfficialName() .And ( textOrAsciiMatches ) ; var nonOfficialNameMatches = IsNotOfficialName() .And ( TranslationToLanguageMatchesCurrentUiCulture() ) .And ( textOrAsciiMatches ) .Expand() ; var nameMatches = officialNameMatches .Or ( nonOfficialNameMatches ) ; return(nameMatches); }
private static Expression <Func <Person, bool> > EmailValueMatches(string term, StringMatchStrategy matchStrategy) { switch (matchStrategy) { case StringMatchStrategy.Equals: return(person => person.Emails.Any(email => email.Value.Equals(term, StringComparison.OrdinalIgnoreCase))); case StringMatchStrategy.StartsWith: return(person => person.Emails.Any(email => email.Value.ToLower().StartsWith(term.ToLower()))); case StringMatchStrategy.Contains: return(person => person.Emails.Any(email => email.Value.Contains(term))); } throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); }
internal static IQueryable<Person> WithFirstName(this IQueryable<Person> queryable, string term, StringMatchStrategy matchStrategy) { return queryable.Where(FirstNameMatches(term, matchStrategy)); }
internal static IQueryable <Establishment> WithNameOrUrl(this IQueryable <Establishment> queryable, string term, StringMatchStrategy matchStrategy) { var names = QueryEstablishmentNames.SearchTermMatches(term, matchStrategy); var urls = QueryEstablishmentUrls.SearchTermMatches(term, matchStrategy); // ReSharper disable ConvertClosureToMethodGroup Expression <Func <Establishment, bool> > establishments = establishment => establishment.Names.Any(name => names.Invoke(name)) || establishment.Urls.Any(url => urls.Invoke(url)); // ReSharper restore ConvertClosureToMethodGroup return(queryable.AsExpandable().Where(establishments.Expand())); }
public virtual PartialViewResult Post(string term, string[] excludes) { const StringComparison caseInsensitive = StringComparison.OrdinalIgnoreCase; const StringMatchStrategy contains = StringMatchStrategy.Contains; const int maxResults = 30; // get places var places = _services.QueryProcessor.Execute( new FindPlacesWithNameQuery { Term = term, TermMatchStrategy = contains, MaxResults = maxResults, EagerLoad = new Expression <Func <Place, object> >[] { p => p.GeoPlanetPlace.Type, p => p.Ancestors.Select(a => a.Ancestor), p => p.Names.Select(n => n.TranslationToLanguage), }, } ); // explain place matches var placeTags = Mapper.Map <TagMenuItem[]>(places); foreach (var placeTag in placeTags.Where(t => !t.Text.Contains(term, caseInsensitive))) { var matchingName = places.Single(p => p.RevisionId == placeTag.DomainKey) .Names.AsQueryable().FirstOrDefault (QueryPlaceNames.SearchTermMatches(term, contains, caseInsensitive)); placeTag.MatchingText = matchingName != null ? matchingName.Text : null; } // get establishments var establishments = _services.QueryProcessor.Execute( new FindEstablishmentsWithNameQuery { Term = term, TermMatchStrategy = contains, MaxResults = maxResults, EagerLoad = new Expression <Func <Establishment, object> >[] { e => e.Names.Select(n => n.TranslationToLanguage), e => e.Ancestors.Select(a => a.Ancestor), e => e.Location.Places, e => e.Type, }, } ); // explain establishment matches var establishmentTags = Mapper.Map <TagMenuItem[]>(establishments); foreach (var establishmentTag in establishmentTags.Where(t => !t.Text.Contains(term, caseInsensitive))) { var establishment = establishments.Single(e => e.RevisionId == establishmentTag.DomainKey); var matchingName = establishment.Names.AsQueryable().FirstOrDefault (QueryEstablishmentNames.SearchTermMatches(term, contains, caseInsensitive)); establishmentTag.MatchingText = matchingName != null ? matchingName.Text : null; } // merge var tags = new List <TagMenuItem>(); tags.AddRange(placeTags); tags.AddRange(establishmentTags); tags = tags.OrderBy(t => t.Text).Take(maxResults).ToList(); // place exact match(es) at the top var exacts = tags.Where(t => t.Text.Equals(term, caseInsensitive)).ToArray(); foreach (var exact in exacts) { tags.Remove(exact); tags.Insert(0, exact); } // remove all excluded tags if (excludes != null && excludes.Any()) { foreach (var exclude in excludes) { foreach (var excludeTag in tags.Where(t => t.Text.Equals(exclude)).ToArray()) { tags.Remove(excludeTag); } } } return(PartialView(MVC.Activities.Shared.Views._tag_menu, tags)); }
public static Expression <Func <PlaceName, bool> > SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison?stringComparison = null) { var nameMatches = TranslationToLanguageMatchesCurrentUiCulture() .And ( TextMatches(term, matchStrategy, stringComparison) .Or ( AsciiEquivalentMatches(term, matchStrategy, stringComparison) ) .Expand() ) ; return(nameMatches); }
private static Expression <Func <EstablishmentName, bool> > AsciiEquivalentMatches(string term, StringMatchStrategy matchStrategy, StringComparison?stringComparison = null) { Expression <Func <EstablishmentName, bool> > expression; switch (matchStrategy) { case StringMatchStrategy.Equals: if (stringComparison.HasValue) { expression = name => name.AsciiEquivalent.Equals(term, stringComparison.Value); } else { expression = name => name.AsciiEquivalent.Equals(term); } break; case StringMatchStrategy.StartsWith: if (stringComparison.HasValue) { expression = name => name.AsciiEquivalent.StartsWith(term, stringComparison.Value); } else { expression = name => name.AsciiEquivalent.StartsWith(term); } break; case StringMatchStrategy.Contains: if (stringComparison.HasValue) { expression = name => name.AsciiEquivalent.Contains(term, stringComparison.Value); } else { expression = name => name.AsciiEquivalent.Contains(term); } break; default: throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); } return(AsciiEquivalentIsNotNull().And(expression).Expand()); }
internal static Expression <Func <EstablishmentUrl, bool> > SearchTermMatches(string term, StringMatchStrategy matchStrategy, StringComparison?stringComparison = null) { var textMatches = TextMatches(term, matchStrategy, stringComparison).Expand() ; var officialUrlMatches = IsOfficialUrl() .And ( textMatches ) ; var nonOfficialNameMatches = IsNotOfficialUrl() //.And //( // TranslationToLanguageMatchesCurrentUiCulture() //) .And ( textMatches ) .Expand() ; var urlMatches = officialUrlMatches .Or ( nonOfficialNameMatches ) ; return(urlMatches); }
private static Expression <Func <Place, bool> > OfficialNameMatches(string term, StringMatchStrategy matchStrategy) { switch (matchStrategy) { case StringMatchStrategy.Equals: return(place => place.OfficialName.Equals(term)); case StringMatchStrategy.StartsWith: return(place => place.OfficialName.StartsWith(term)); case StringMatchStrategy.Contains: return(place => place.OfficialName.Contains(term)); } throw new NotSupportedException(string.Format("StringMatchStrategy '{0}' is not supported.", matchStrategy)); }
private static Expression <Func <Place, bool> > NonOfficialNameMatches(string term, StringMatchStrategy matchStrategy) { // using this predicate causes the 'name' parameter to not be bound // fixed: http://stackoverflow.com/questions/10689506/unable-to-refactor-using-linq-to-entities-and-linqkit-predicatebuilder var names = QueryPlaceNames.SearchTermMatches(term, matchStrategy); // ReSharper disable ConvertClosureToMethodGroup Expression <Func <Place, bool> > places = place => place.Names.Any(name => names.Invoke(name)); // ReSharper restore ConvertClosureToMethodGroup return(places.Expand()); }
internal static IQueryable <Place> WithName(this IQueryable <Place> queryable, string term, StringMatchStrategy matchStrategy) { var matchesName = OfficialNameMatches(term, matchStrategy) //.Or //( // NonOfficialNameMatches(term, matchStrategy) //) ; return(queryable.AsExpandable().Where(matchesName)); }