Copied to clipboard

Flag this post as spam?

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


  • Mark 255 posts 612 karma points
    Oct 25, 2010 @ 16:06
    Mark
    0

    Examine query syntax - Unescaped fields

    I'm new to examine. The help on codeplex implies adding a field's search term without escaping it will cause the term to be searched as a multi query, i.e. a search term of Umbraco Rocks will return results matching either Umbraco or Rocks. I'm not finding this to be the case, but my specific customer requires an or search on each word in the search term. Here's my code:


                    Examine.SearchCriteria.ISearchCriteria criteria = ExamineManager.Instance.SearchProviderCollection[SearchProviderName].CreateSearchCriteria();

                    string[] searchFields = SearchFieldList.Split(',');

                    int fieldCount = searchFields.GetLength(0);
                    string[] searchParams = new string[fieldCount];
                    for (int i = 0; i < fieldCount; i++)
                    {
                        searchParams[i] = searchTerm;
                    }

                    criteria.GroupedOr(searchFields, searchParams).Not().Field("hideFromSearchResults", "1");

                    if (!string.IsNullOrEmpty(OrderByFieldList))
                    {
                        string[] orderByFields = OrderByFieldList.Split(',');

                        if (OrderByDescending)
                            criteria.OrderByDescending(orderByFields);
                        else
                            criteria.OrderBy(orderByFields);
                    }

                    // get the search result set
                    IEnumerable<SearchResult> results = ExamineManager.Instance.SearchProviderCollection[SearchProviderName].Search(criteria).AsEnumerable();

    ...

    As you can see, I'm not escaping the search term. Do I need to break up the search term explicitly?

    I'm using Lucene.Net.Analysis.Standard.StandardAnalyzer for index and search. I'm also using Umbraco 4.52

    Any help would be much appreciated.

    Thanks,

    Mark

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 26, 2010 @ 00:16
    Aaron Powell
    0

    Doing a search like .Field("someField", "Umbraco Rocks") will create a Lucene query which is someField:Umbraco Rocks, and since the word "rocks" isn't associated with a field it'll fall back to the default field for the search, which Examine doesn't set (it has no idea what field should be the default one).

    If you're trying to search on "Umbraco Rocks" as a phrase then you need to escape it. If you want to match documents which contain either Umbraco or Rocks then there is the GroupedOr operator and if you want to match documents which only contain Umbraco and Rocks then you need to use the GroupedAnd.

    Examine doesn't support the Lucene PhraseQuery, you would have to write something custom if you wanted to do that (see my comments herehttp://our.umbraco.org/forum/developers/extending-umbraco/13786-Examine-Multiple-search-terms-vs-Phrases about PhraseQuery)

  • Qube 74 posts 116 karma points
    Oct 26, 2010 @ 01:39
    Qube
    0

    Hi Mark. Slace is right (of course :), but I thought I'd add my 2c anyway.

    The search scenario you mentioned is the most common and desirable. That is, more keywords == more refinement (less results). You want each word to be found at least once accross the fields you're searching. In TSQL, that search would look something like this:

    WHERE
    (field1 CONTAINS keyword1 OR field2 CONTAINS keyword1 OR field3 CONTAINS keyword1) AND
    (field1 CONTAINS keyword2 OR field2 CONTAINS keyword2 OR field3 CONTAINS keyword2) AND
    (field1 CONTAINS keyword3 OR field2 CONTAINS keyword3 OR field3 CONTAINS keyword3)

    From what I've learnt from Examine and Lucene so far, the equivalent syntax ends up looking similar to this:

    +(field1:keyword1 field2:keyword1 field3:keyword1)
    +(field1:keyword2 field2:keyword2 field3:keyword2)
    +(field1:keyword3 field2:keyword3 field3:keyword3)

    I achieved this by manually splitting the keywords (by space), and appending each one using .And().GroupedOr(). You can see what I mean here (last post). I created my own IExamineValue implementation which both escapes the term and appends a multiple character wildcard at the end (the helper methods only allow one or the other).

    Not sure whether that's the best method (Slace will be able to tell us), but it's working great for me. In fact, Examine makes me look good all the time, so I love it!

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 26, 2010 @ 01:52
    Aaron Powell
    0

    Nope, that's basically what you need to go about doing :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies