Copied to clipboard

Flag this post as spam?

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


  • Mihail 39 posts 142 karma points
    Sep 20, 2021 @ 21:55
    Mihail
    0

    Umbraco searching in multi indexer returns no result

    I have 3 indexers: content from Umbraco contents, other two from external source (I use some repositories and services)

    I created component for search in order to register creators and indexer

    public sealed class SearchComponent : IComponent
    {
      private readonly IExamineManager mExamineManager;
      private readonly SystemsIndexCreator mSystemsIndexCreator;
      private readonly ApiDocumentationIndexCreator mApiDocumentationIndexCreator;
    
      public SearchComponent(IExamineManager examineManager, SystemsIndexCreator SystemsIndexCreator, ApiDocumentationIndexCreator apiDocumentationIndexCreator)
      {
        mExamineManager = examineManager;
        mSystemsIndexCreator = SystemsIndexCreator;
        mApiDocumentationIndexCreator = apiDocumentationIndexCreator;
      }
    
      public void Initialize()
      {
        foreach (var index in mSystemsIndexCreator.Create())
          mExamineManager.AddIndex(index);
    
        foreach (var index in mApiDocumentationIndexCreator.Create())
          mExamineManager.AddIndex(index);
      }
    

    The items are populated correctly, as I see data in TEMP\ExamineManager... locations

    for example for api documentation set builder class I do:

    public class ApiDocumentationValuesSetBuilder : IValueSetBuilder<SiteMapItem>
    {
      public IEnumerable<ValueSet> GetValueSets(params MyData[] myDataItems)
      {
        var list = new List<ValueSet>();
    
        if (myDataItems != null)
        {
          foreach (var sitemap in myDataItems)
          {
            var fp = !string.IsNullOrWhiteSpace(sitemap.Path) ? HostingEnvironment.MapPath(ApiDocumentationService.cDocumentationFolder + sitemap.Path) : "";
            var indexValues = new Dictionary<string, object>
            {
              ["name"] = sitemap.Name,
              ["path"] = sitemap.Path,
              ["version"] = sitemap.Version ?? "",
              ["content"] = File.Exists(fp) ? File.ReadAllText(fp) : "",
              ["dircount"] = (sitemap.Directories?.Count ?? 0).ToString(),
            };
    
            list.Add(new ValueSet(Guid.NewGuid().ToString(), "apidocumentation", indexValues));
          }
        }
    
        return list;
      }
    }
    

    All good here.

    I have a SearchEngine class:

    public class SearchEngine
    {
      private static readonly int cPerPage = 10;
    
      public SearchEngine(string term,
          string orderBy = null,
          int maxResults = 20)
      {
        Term = term;
        OrderBy = orderBy;
        MaxResults = maxResults;
      }
    
      public string Term { get; }
      public string OrderBy { get; }
      public int MaxResults { get; }
    
      public SearchResultModel SearchAll(IExamineManager examineManager, int page = 0)
      {
        var itemsFound = new List<SearchResultModel>
        {
          Search(examineManager, "ExternalIndex"),
          Search(examineManager, ApiDocumentationIndexRebuilder.cIndexerName, "apidocumentation"),
          Search(examineManager, SystemsIndexRebuilder.cIndexerName, "systems")
        };
    
        itemsFound.RemoveAll(c => c == null);
        var finalItems = itemsFound.Skip(page * cPerPage);
        return new SearchResultModel(finalItems.SelectMany(s => s.SearchResults), finalItems.Sum(s => s.Totalmilliseconds), Term, OrderBy ?? "score");
      }
    
      private SearchResultModel Search(IExamineManager examineManager, string indexerName, string category = null)
      {
        examineManager.TryGetIndex(indexerName, out IIndex index);
        if (index == null)
        {
          return new SearchResultModel(new EmptySearchResults(), 0, "", "");
        }
    
        var watch = new Stopwatch();
        watch.Start();
    
        var searcher = (BaseLuceneSearcher)index.GetSearcher();
        var criteria = searcher.CreateQuery(category, BooleanOperation.Or);
    
        // I put all fields (some are from Umbraco data, others for outside source)
        string searchFields = "nodeName,pageTitle,metaDescription,bodyText,name,content";
        IBooleanOperation terms = criteria.GroupedOr(searchFields.Split(','), Term);
        var result = terms.Execute(MaxResults);
    
        watch.Stop();
    
        return new SearchResultModel(result, watch.ElapsedMilliseconds, Term, OrderBy ?? "score");
    
      }
    }
    

    The problem is that term is not found nowhere since I had a lot of content that contains such keyword.

    What I'm doing wrong here ?

  • 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