Copied to clipboard

Flag this post as spam?

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


  • Max 2 posts 72 karma points
    Dec 10, 2023 @ 19:54
    Max
    0

    Hello Umbraco community,

    I'm currently working on implementing a search feature in my Umbraco project, and I'm looking for guidance on how to perform a search query that returns all results containing a specified string.

    So for example,

    If i search for "Gre" I want all of the products that has "Green" in their title but also a product that contains "Great"

    I would appreciate any help or advice on constructing the search query to achieve the desired functionality.

    Best regards, Max

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Dec 10, 2023 @ 20:59
    Marc Goodson
    0

    Hi Max

    Is it just a section of Products you are searching, and in your example you show a search where the matches 'start with' the search term.. but you say it should use the logic contains - what I'm saying is should you Gre search also match the Ogre 2000 product?

    Umbraco comes with Examine Search built in, which is a friendly syntax around a Lucene search index - this is great for analysing and searching key terms within a lot of text, but is less great for specific 'like' or 'contains' queries that you would typically make to a database.

    If your search is a complex one and involves for searching within the descriptions of the products for the best matching text, then Examine will b e super useful!

    It also has Wildcard search support and Fuzziness to match similar spellings etc, you can read more here: https://shazwazza.github.io/Examine/articles/searching.html#wildcards

    But if you only want to match on the 'name' of the product and you want functionality like contains or starts with only then querying the Umbraco Published Cache would probably be sufficient.

    var productsSection = Umbraco.ContentAtRoot().FirstOrDefault().Children<ProductSection>().FirstOrDefault();
    
    var products = productsSection.Children<Product>("f=>f.ProductTitle.StartsWith(searchTerm));
    

    or

    var products = productsSection.Children<Product>("f=>f.ProductTitle.Contains(searchTerm));
    

    but it just depends on what you are trying to do in terms of complexity of search options and how many products... 70 or 7 million etc

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft