Skip to main content
deleted 3 characters in body
Source Link
Marek Musielak
  • 24.2k
  • 3
  • 31
  • 64

You have an issue while adding search term predicate to your query.

You just have your main query and you add OR Title Like SearchTerm OR ... so it's enough that title contains search term and it none of the other conditions is needed to evaluate to true:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    andPredicate = andPredicate.And(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        andPredicate = andPredicate.Or(p => p.Title.Equals(tempTerm));
    }
}

Instead it should be:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    var titlePredicate = PredicateBuilder.False<ProductSearchResultItem>();
    titlePredicate = titlePredicate.Or(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        titlePredicate = titlePredicate.Or(p => p.Title.Equals(tempTerm));
    }

    andPredicate = andPredicate.And(titlePredicate);
}

You have an issue while adding search term predicate to your query.

You just have your main query and you add OR Title Like SearchTerm OR ... so it's enough that title contains search term and it none of the other conditions is needed to evaluate to true:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    andPredicate = andPredicate.And(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        andPredicate = andPredicate.Or(p => p.Title.Equals(tempTerm));
    }
}

Instead it should be:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    var titlePredicate = PredicateBuilder.False<ProductSearchResultItem>();
    titlePredicate = titlePredicate.Or(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        titlePredicate = titlePredicate.Or(p => p.Title.Equals(tempTerm));
    }

    andPredicate = andPredicate.And(titlePredicate);
}

You have an issue while adding search term predicate to your query.

You just have your main query and you add OR Title Like SearchTerm OR ... so it's enough that title contains search term and none of the other conditions is needed to evaluate to true:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    andPredicate = andPredicate.And(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        andPredicate = andPredicate.Or(p => p.Title.Equals(tempTerm));
    }
}

Instead it should be:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    var titlePredicate = PredicateBuilder.False<ProductSearchResultItem>();
    titlePredicate = titlePredicate.Or(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        titlePredicate = titlePredicate.Or(p => p.Title.Equals(tempTerm));
    }

    andPredicate = andPredicate.And(titlePredicate);
}
Source Link
Marek Musielak
  • 24.2k
  • 3
  • 31
  • 64

You have an issue while adding search term predicate to your query.

You just have your main query and you add OR Title Like SearchTerm OR ... so it's enough that title contains search term and it none of the other conditions is needed to evaluate to true:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    andPredicate = andPredicate.And(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        andPredicate = andPredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        andPredicate = andPredicate.Or(p => p.Title.Equals(tempTerm));
    }
}

Instead it should be:

if (!string.IsNullOrWhiteSpace(searchTerm))
{
    var titlePredicate = PredicateBuilder.False<ProductSearchResultItem>();
    titlePredicate = titlePredicate.Or(x => x.Title.Like(searchTerm, 0.75f));

    foreach (var t in searchTerm.Split(' '))
    {
        var tempTerm = t;
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*").Boost(5.5f));
        titlePredicate = titlePredicate.Or(p => p.Title.MatchWildcard(tempTerm + "*"));
        titlePredicate = titlePredicate.Or(p => p.Title.Equals(tempTerm));
    }

    andPredicate = andPredicate.And(titlePredicate);
}