Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Murray Roke 503 posts 966 karma points c-trib
    May 21, 2010 @ 05:31
    Murray Roke
    0

    Examine - Dynamic Queries

    I have this code to build a dynamic Query with Examine:

    var criteria = searchProvider.CreateSearchCriteria(IndexType.Content);
    IBooleanOperation query = ????? ;  

    if (!string.IsNullOrEmpty(q))
        query = query.And().GroupedOr(new[] { "title", "bodyText", "abstract" }, new[] { q });
    if (!string.IsNullOrEmpty(Request["type"]))
        query = query.And().Field("type", Request["type"].Escape());
    if (!string.IsNullOrEmpty(Request["tag"]))
        query = query.And().Field("tags", Request["tag"].Escape());

    var results = searchProvider.Search(query.Compile());

    But I'm not sure how go get from criteria to Iboolean operator where all clauses are optional. (well there should be at least one, but it can be any one)

    I could create each permutation but that would be messy.

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 21, 2010 @ 23:52
    Aaron Powell
    0

    If you don't have any common operation between the queries you can't do what you want. Looking at your logic it'd be possible to get through the if statements without any condition being hit.

  • Murray Roke 503 posts 966 karma points c-trib
    May 24, 2010 @ 00:48
    Murray Roke
    0

    Thanks, "no" is a perfectly valid answer :-) ... BTW, I'd have an earlier condition to check that any one of 'tag', 'type' or 'q' is set.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 24, 2010 @ 00:52
    Aaron Powell
    0

    If you've got a pre-existing query you can create a local variable of type IBooleanOperation which doesn't have an assignment, it's only assigned in the IF statement.

    You'd just have to make sure your conditionals cover all paths or your app wont compile ;)

Please Sign in or register to post replies

Write your reply to:

Draft