Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide a simple search example #9

Open
NaserElziadna opened this issue Feb 9, 2023 · 2 comments
Open

provide a simple search example #9

NaserElziadna opened this issue Feb 9, 2023 · 2 comments

Comments

@NaserElziadna
Copy link

NaserElziadna commented Feb 9, 2023

URGENT ,
hey @callumbwhyte @greystate @dawoe @busrasengul ,

i have a task to create searcher in the website , can any one of you guys
help and provide a simple way to perform a search using this package.

i can continue, i just need a start.

@Eaglef90
Copy link

Did you ever find an example or a starting off point? I want to use this in my umbraco but I too am stuck and needs a basic example so I can figure out how to do a basic call. The docs saying "query.And().IsPublished()" don't really help new people and Visual Studio is clueless on these commands and wants to change them all to LINQ queries.

@Eaglef90
Copy link

After a bit of work, lots of sugar. I was able to build this basic example that should help people in the feature.

`using Examine;
using Our.Umbraco.Extensions.Search;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Web.Common;

namespace Helper_Classes
{
public class SearchHelper
{
private readonly IExamineManager _examineManger;
private readonly UmbracoHelper _umbracoHelper;

    public SearchHelper(IExamineManager examineManager, UmbracoHelper umbracoHelper)
    {
        _examineManger = examineManager;
        _umbracoHelper = umbracoHelper;
    }

    public IEnumerable<IPublishedContent> SearchSite(string searchTearm)
    {
        IEnumerable<string> nodeIDs = Array.Empty<string>();
        var searchHelper = new Our.Umbraco.Extensions.Search.Helpers.SearchHelper();

        if (searchTearm is not null && _examineManger.TryGetIndex("ExternalIndex", out IIndex searchIndex))
        {
            var searcher = searchIndex.Searcher;
            var searchQuery = searcher.CreatePublishedQuery("content")
                                      .And()
                                      .ManagedQuery(searchTearm);

            //var searchResults = searchHelper.Page<IPublishedContent>(searchQuery, 1, 10, out int totalPages, out int totalResults); This way is obsolete, use search method below with variable searchResults2

            var searchResults2 = searchQuery.Execute();
            searchResults2.Page<IPublishedContent>(1, 10, out int totalPages, out int totalResults);


            nodeIDs = searchQuery.Execute()
                                 .Select(x => x.Id);
        }

        foreach (var nodeID in nodeIDs) { yield return _umbracoHelper.Content(nodeID); }
    }
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants