Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

EPiFind - how to index additional fields

Vote:
 

Hi,

I'm working with EPiFind 10 and EPiServer CMS 9. I need to display a list of events filtered by county.

My EventPage looks like this:

[ContentType(GUID = "904c87fe-e0ba-4245-9f8c-1f8e9aaf4094")]
public class EventPage : PageData
{
    [SelectOne(SelectionFactoryType = typeof(CountySelectionFactory))]
    public virtual PageReference County { get; set; }
	
	...
}

And search logic w/o filtering:

var events = SearchClient.Instance.Search()
                            .OrderBy(x => x.DateAndTime)
                            .Skip((pageNumber - 1) * pageSize)
                            .Take(pageSize)
                            .GetPagesResult();

During indexing, I would like to index a county name (page name of County page type that is referenced by EventPage), and filter events by that field.

When the editor renames the county page, I would like update the search index and all documents of type EventPage (or my custom document) that have

County = renamedPage.PageLink

I basically need something like http://tedgustaf.com/blog/2013/4/add-custom-fields-to-the-episerver-search-index-with-episerver-7/ , but for EPiFind 10.

Should I let EPiFind index EventPage(s) as usual, and explicitly index documents that are optimized for search: http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Find/10/DotNET-Client-API/Indexing/

How can I execute bulk update? Can I use NewCommand for that?

Any help would be greatly appreciated!

#139255
Sep 28, 2015 15:30
Vote:
 

I would add a new readonly property called CountryName to your EventPage that's a string representing the country name (lazy load the country name as it will only be accessed when the EventPage is indexed by Find). Makes it real simple to search and facet on the country when searching events. 

Then I'd add a ContentUpdating event handler for your County page, use Find to look for all events in that country and update the related event pages.

Given I guess the countries are not renamed too often performance shouldn't be a big issue?

David

#139265
Sep 28, 2015 22:03
Vote:
 

I asked a simular question a while ago and use the answer I got a lot and it works great. Read more here:

http://world.episerver.com/forum/developer-forum/EPiServer-Search/Thread-Container/2015/5/best-pratice-for-including-information-that-does-not-exist-on-content/

In summary it says that you should create a extension method for the pagetype and a function in it to pick up county name, then in a initialization module add something like this:

SearchClient.Instance.Conventions
    .ForInstancesOf<EventPage>()
    .IncludeField(x => x.CountyName());

This makes that Find will add a new property named CountyName to the object and it will only access it when indexing.

Later you can do terms filtering och things like that on it

#139275
Sep 29, 2015 9:24
Vote:
 

Thank you guys! Extension method works like a charm :)

I need help with update method.

Init module:

private void ContentEvents_PublishedContent(object sender, EPiServer.ContentEventArgs e)
{
    var countyPage = e.Content as CountyPage;
    if (countyPage != null)
    {
        var eventPages = SearchClient.Instance.Search<EventPage>()
                                        .Filter(x => x.County.Match(countyPage.PageLink))
                                        .Take(1000)
                                        // return any field
                                        .Select(x => x.PageLink)
                                        .GetResult();

        foreach (var searchHit in eventPages.Hits)
        {
            SearchClient.Instance.Update<EventPage>(searchHit.Id)
                        .Field(x => x.CountyName(), countyPage.Name)
                        .Execute();
        }
    }
}

1. Can I update EventPage(s) in one go, or do I have to update them one by one?

2. The above code throws the following exception:

An exception of type 'EPiServer.Find.ServiceException' occurred in EPiServer.Find.dll but was not handled in user code

Additional information: The remote server returned an error: (403) Forbidden.

Your key is not authorized to access (POST) '/INDEX_NAME/Models_Pages_EventPage/_700f7047-ce29-456d-9403-af5d6205584e_no/_update'

Thanks

#139289
Sep 29, 2015 11:42
Vote:
 

If you are using Update, I think you need to do them one by one. 
It would be better to just index a batch of EventPages

SearchClient.Instance.Index(eventPages);
#139291
Sep 29, 2015 11:55
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.