Copied to clipboard

Flag this post as spam?

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


  • Travis 19 posts 46 karma points
    Sep 14, 2010 @ 11:54
    Travis
    0

    Examine query syntax - Or().Field ignored

    I am a bit confused as to how to structure an examine query the requires a single Field of value X Or Y Or Z - in my cast Wine Type of Red, White or Other.

    Below is the search I am running.  If I select just one type of wine from the options I get all the wines of that type as expected.  If however I select more than one the OR statement is ignored and I only get the first wine type selected.  The code executes correctly and inspecting the filter shows that the required wine types did get added but they are not reflected.  

    var criteria = ExamineManager.Instance.SearchProviderCollection["WineSearcher"].CreateSearchCriteria();
    IBooleanOperation  filter = null; 
    
     foreach (ListItem li in chkWineType.Items.Cast<ListItem>().Where(li => li.Selected))
        if (filter == null)
        {
             filter = criteria.Field("WineType", li.Text.Escape());
        }
        else
        {
            filter.Or().Field("WineType", li.Text.Escape());
        }
    // no error checking yet that filter was set           
    mSearchResults = ExamineManager.Instance.SearchProviderCollection["WineSearcher"].Search(filter.Compile());

     

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 14, 2010 @ 13:23
    Ismail Mayat
    0

    Travis,

    Change your code

    filter.Or().Field("WineType", li.Text.Escape());

    to

    filter = filter.Or().Field("WineType", li.Text.Escape());

    i have done something similar although mine was and not or.

    Regards

    Ismail

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 14, 2010 @ 17:02
    Aaron Powell
    0

    You can solve this in 1 of 2 ways, by using the GroupedOr statement or changing the default boolean operation.

    GroupedOr

    var items = chkWineItems.Items.Cast<ListItem>();
    filter = filter.GroupedOr(Enumerable.Range(0, items.Count().Select(x => "WineType"), items.Select(x => x.Value.Escape()));

    This will create a grouped statement using WineType and the items in the check box list, resulting in this Lucene query when you compile:

    +((WineType:White WineType:Red WineType:Sparkling)) +__IndexType:content

    Changing default boolean operation

    var criteria = ExamineManager.Instance.SearchProviderCollection["WineSearcher"].CreateSearchCriteria(BooleanOperation.Or);

    Then you can use the original code and you should get this:

    (WineType:White WineType:Red) +__IndexType:content

     

    The 2nd option may not work right, I haven't really tested the default boolean operation change. But the reason you aren't getting the results you need is that the first statement in the fluent API defaults to an AND statement, meaning you get this:

    +WineType:White WineType:Red +__IndexType:content

    And that's why the first one is always there. GroupedOr is your safest bet IMO.

  • Travis 19 posts 46 karma points
    Sep 15, 2010 @ 03:53
    Travis
    0

    Thanks!  I was starting to look at GroupedOr last night when I ran out of energy.

     

Please Sign in or register to post replies

Write your reply to:

Draft