I am trying to put together a very simple predicate builder - I only have one predicate for now. But as far as I see in the logs, it isn't applying any clauses at all. This is what I have:

using (var context = ContentSearchManager.GetIndex("resources_index").CreateSearchContext())
    string searchTerm = "press";

    var filterPredicate = PredicateBuilder.False<ResourceSearchResultItem>();

    string[] terms = searchTerm.Split(' ');

    var termPredicate = PredicateBuilder.False<ResourceSearchResultItem>();

    foreach (string term in terms)
        termPredicate = termPredicate.Or(p => p.BodyContent.Contains(term));


    IQueryable<ResourceSearchResultItem> query = context.GetQueryable<ResourceSearchResultItem>().Where(filterPredicate);

When I do this, the query in the logs is:

34184 00:02:23 INFO  ExecuteQueryAgainstLucene (resources_index): -*:* - Filter : 

It's basically applying no filters at all.

However, if I use just termPredicate in the .Where, like so:

IQueryable<ResourceSearchResultItem> query = context.GetQueryable<ResourceSearchResultItem>().Where(termPredicate);

...Then the query in the logs is just fine:

33564 00:08:25 INFO  ExecuteQueryAgainstLucene (resources_index): SpanWildcardQuery(bodycontent:*press*) - Filter : 

Not sure what I am missing. Any help would be appreciated!

  • What happens when you 'or' the term in the filter predicate rather than the term predicate? – Michael West Oct 23 '16 at 4:17
  • It works fine if I do that - (same as if I just apply termPredicate to the .Where clause. It doesn't work when I add a predicate to another predicate. I have to do that at some point when I have other query clauses (right now I only have the one). – M.R. Oct 23 '16 at 4:22
  • Perhaps you are missing at least one positive value in the boolean queries. Try changing the filter or term predicate to true. – Michael West Oct 23 '16 at 4:42
  • Tried that, too. No luck :( – M.R. Oct 23 '16 at 5:24
  • 1
    Funny what the fix was. So easy to overlook. – Michael West Oct 23 '16 at 22:32

You need to assign the Or statement to the filterPredicate because the Or and other operators return a new expression instead of building upon the current one. You are doing it correctly with the termPredicate.

filterPredicate = filterPredicate.Or(termPredicate);
  • Exactly - since the .Or returns a new predicate expression. – Søren Engel Oct 23 '16 at 5:59
  • 1
    Please edit your answer to add an explanation as to why this will solve the issue. It is important for the OP, as well as for future readers, to know the underlying cause of the problem in addition to how it can be solved. – Dmytro Shevchenko Oct 23 '16 at 7:59
  • 1
    @dmytro I updated my answer. – Thomas D Oct 23 '16 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.