Copied to clipboard

Flag this post as spam?

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


  • René Pjengaard 118 posts 702 karma points c-trib
    May 22, 2023 @ 14:32
    René Pjengaard
    0

    Is it possible to use leading wildcard in Examine (V10)

    Hi dear Forum,

    in older implementations of Examine (V7 sites), we had the option to use leading wildcards when searching. Meaning that you could search for ex. "lorem" and the Examine would find words like ex.

    • packlorem
    • lorempack
    • lorem

    I can´t seem to find this option anywhere in the V10 implementation. Anybody got a clue?

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    May 22, 2023 @ 15:42
    Anders Bjerner
    102

    Hi dear René

    It's no longer necessary to configure your index to support this - IIRC we had to do that for V7.

    Only requirement now seems to be that your index has a searcher of type BaseLuceneSearcher - which is the case for the default indexes.

    So it should be possible doing like in the snippet below. My local test site has an About page, which is matched when searching for *out.

    Both leading and trailing wildcards are also possible - eg. *ou* still matches my About page, but now also some other pages.

    @using Examine
    @using Examine.Lucene.Providers
    @using Examine.Lucene.Search
    @using Examine.Search
    @using Limbo.Umbraco.Search.Constants
    @inject IExamineManager ExamineManager
    
    @{
    
        // Get a reference to the index that we wish to use (constant comes from the 'Limbo.Umbraco.Search')
        if (!ExamineManager.TryGetIndex(ExamineIndexes.ExternalIndexName, out IIndex? index)) {
            <pre>Computer says NO!</pre>
            return;
        }
    
        // Get a reference to the searcher (must be 'BaseLuceneSearcher' to allow leading wildcard)
        if (index.Searcher is not BaseLuceneSearcher searcher) {
            <pre>Computer says NO!</pre>
            return;
        }
    
        // Initialize a new query
        IQuery query = searcher.CreateQuery(null, default, searcher.LuceneAnalyzer, new LuceneSearchOptions {
            AllowLeadingWildcard = true
        });
    
        // Update and execute the query with out leading wildcard query
        ISearchResults result = query.NativeQuery("nodeName:*out").Execute();
    
        // Total amount of matched items
        <pre>@result.TotalItemCount</pre>
    
        // Iterate through the matched items
        foreach (ISearchResult? item in result) {
            <pre>@item.Id => @item.Values["nodeName"]</pre>
        }
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft