Copied to clipboard

Flag this post as spam?

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


  • Stephen 47 posts 270 karma points
    Apr 25, 2013 @ 13:22
    Stephen
    0

    Search for an exact phrase in all fields with Examine

    Is there an easy way to search for an exact phrase over all indexed fields?

    I want to search for the phrase "high speed rail" and only return documents with that exact phrase in them, and I don't want to maintain a list of fields to search in.

    I've tried 

    ExamineManager.Instance.SearchProviderCollection[searchProvider].Search("high speed rail", true); - returns anything with high* or speed* or rail* in it

    ExamineManager.Instance.SearchProviderCollection[searchProvider].Search("high speed rail", false); - returns anything with the words high, speed or rail (but not all of them and in that order

    ExamineManager.Instance.SearchProviderCollection[searchProvider].Search("\"high speed rail\"", true); - returns a strange set of results: the lucene query ends up looking like description:"high* id:speed* id:rail"* which doesn't look right at all.

    ExamineManager.Instance.SearchProviderCollection[searchProvider].Search("\"high speed rail\"", false); - again returns anything with the words in the doc.

     

    Most of the examples I find create a searchCriteria by GroupedOr ing a list of fields, but I don't want to maintain that list - is there any way of avoiding that, and why doesn't the Search() work with a phrase passed in.

    As a side note I notice http://umbraco.com/search?q=case%20studies returns nothing but http://umbraco.com/search?q=studies returns a page called Case Studies, so maybe I'm not the only one having problems with multiple words.

     

    Stephen

     

     

  • Robert Dougan 48 posts 154 karma points
    Apr 26, 2013 @ 10:55
    Robert Dougan
    1

    Hi Stephen,

    I decided to use raw Lucene syntax rather than the fluent API. I found that this gave me a lot more control over the query.

    For an exact match using raw lucene, it's simply a case of putting the phrase in double quotes:

    var Searcher = ExamineManager.Instance.SearchProviderCollection["MySearchProvider"];

    var searchCriteria = Searcher.CreateSearchCriteria();

    string searchQueryString = "mySearchProperty: \"high speed rail\"";

    var query = searchCriteria.RawQuery(searchQueryString);

    IEnumerable<SearchResult> searchResults = Searcher.Search(query);

    I'm not sure how you could use this to search accross all indexed fields without explicitly specifying them however. You might be able to read in a list from your ExamineIndex.config file.

    Robert

  • Jebastin 42 posts 91 karma points
    Sep 30, 2013 @ 11:04
    Jebastin
    0

    I've found an easy solution to be used in the code behind. It may help someone.

    if ((searchKeyword.StartsWith("'") && searchKeyword.EndsWith("'")) || ((searchKeyword.StartsWith('"'.ToString()) && searchKeyword.EndsWith('"'.ToString()))))
    {
    if (searchFilter == null)
    {
    searchFilter = siteSearcher.CreateSearchCriteria().GroupedOr(fields, searchKeyword);
    }
    else
    {
    searchFilter = searchFilter.Or().GroupedOr(fields, searchKeyword);
    }
    }
    else
    {
    var content = searchKeyword.Split(' ');
    foreach (var word in content)
    {
    if (!string.IsNullOrEmpty(word))
    {
    if (searchFilter == null)
    {
    searchFilter = siteSearcher.CreateSearchCriteria().GroupedOr(fields, word);
    }
    else
    {
    searchFilter = searchFilter.Or().GroupedOr(fields, word);
    }
    }
    }
    }
  • Connie DeCinko 931 posts 1160 karma points
    Jul 07, 2016 @ 22:16
    Connie DeCinko
    1

    Sorry for replying to an old discussion, but does this still work? I have tried this and similar and even though it all looks right, I never get to the second GroupedOr. It's as if Examine only looks at the first one and even if it finds no results it never bothers to check the second GroupedOr.

Please Sign in or register to post replies

Write your reply to:

Draft