Copied to clipboard

Flag this post as spam?

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


  • LewisParkinson 9 posts 49 karma points
    Aug 19, 2022 @ 14:29
    LewisParkinson
    0

    Getting Field values within ISearchResult Item for a Search Index.

    I'm trying to create a search index to get all doucment (within the given document type aliases) where any of the fields (given in the comma separated string) contains the search terms. I'm doing this with the following code:

     ISearcher searcher = index.GetSearcher();
    
                    IQuery criteria = searcher.CreateQuery("content", BooleanOperation.And);
    
                    IBooleanOperation query = criteria.GroupedNot(new string[] { "searchDisabled" }, new string[] { "1" });
    
                    string searchFields = "nodeName,pageTitle,pageSummary,body,seoMetaDescription,seoMetaTitle,bottomGridSection,blogPostCategories";
    
                    if (terms != null && terms.Any())
                        query.And(q => q.GroupedOr(searchFields.Split(','), terms.ToArray()), BooleanOperation.Or);
    
                    if (docTypeAliases != null && docTypeAliases.Any())
                        query.And(q => q.GroupedOr(new[] { "__NodeTypeAlias" }, docTypeAliases));
    
                    ISearchResults allResults = query.Execute();
    
                    List<ISearchResults> results = allResults.ToList();
    

    Once I have the List of ISearchResult items, I need to convert the list to a new object with the values froms the document. I've had a google round and noticed I should be able to use the following 'Fields' property within ISearchResult to get the values out:

    List<MyObject> items = results.Select(r => new MyObject()
                    {
                        Title = r.Fields["title"]
                    });
    

    But, Fields is empty in every ISearchResult item. So, I'm having to use the GetByID() method to grab each document first, which is making the page very slow to load.

    Is there something I've missed to get these ISearchResult items with the Fields property populated?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 24, 2022 @ 11:16
    Marc Goodson
    0

    Hi Lewis

    Looking at the Examine source:

    https://github.com/Shazwazza/Examine/blob/release/1.2/src/Examine/SearchResult.cs#L42

    Does a SearchResult entity have a Values property?

    Which is a dictionary of results?

    r.Values.TryGetValue("title", out var title)
    

    or perhaps even

    r["title"] 
    

    as a short cut?

    regards

    marc

  • LewisParkinson 9 posts 49 karma points
    Aug 31, 2022 @ 15:51
    LewisParkinson
    0

    Hi Marc,

    Sorry for taking so long to respond. I've only just seen the message.

    There is a Values property but it is always Null. But, I've noticed if I change the List<ISearchResults> results to an array, the Values property is populated. Do you have any idea why that would be?

    Regards, Lewis

Please Sign in or register to post replies

Write your reply to:

Draft