Search on Custom Content Part

Topics: Customizing Orchard, Writing modules
Apr 25, 2011 at 4:44 PM
Edited Apr 25, 2011 at 5:09 PM

I want to use the Orchard.Search to search my custom "Contacts" content part which has FirstName, LastName, Email, and Phone fields.

How do I index my Contacts Module and added the "Contacts" check box to the search setting page?

I added this to the handler:

public ContactPartHandler( IRepository<ContactPartRecord> repository ) {
            Filters.Add(StorageFilter.For(repository));

            OnIndexing<ContactPart>((context, contactPart) => context.DocumentIndex
                                                   .Add("contact_firstName", contactPart.Contact.FirstName).Analyze().Store()
                                                   .Add("contact_lastName", contactPart.Contact.LastName).Analyze().Store()
                                                   .Add("contact_email", contactPart.Contact.Email).Analyze().Store()
                                                   .Add("contact_phone", contactPart.Contact.Phone).Analyze().Store()
                                                    );
        }

but the fields doesn't appear on the search setting.

Thanks!

Coordinator
Apr 25, 2011 at 5:12 PM

Go to Content, Content Type, edit your content type, and under fields, expand and check Index this field. Do the same at the content type level. Update your search index from the Settings menu, and check for those fields which should appear.

Apr 25, 2011 at 6:18 PM

Thanks for your reply but I need to index a content part but not fields.

The contact_firstName....contact_phone are properties of ContactRecord class in Contacts Module.

I check the index for the content type but still don't know how to index the content part and the properties of the content part.

Thanks.

Coordinator
Apr 25, 2011 at 6:27 PM

So if it is a code based part, then you just need to hook the indexing event in the handler to add those elements. Search for OnIndexing accross the code, and you will find a lot of examples. And if you want to automatically add a content type to be indexed, by code too, then you need to use Orchard.Indexing.MetadataExtesions.Indexed in your migration code.

Apr 25, 2011 at 6:33 PM

Yes. I found the example and I tried to use the codes and my original question of this post is that I do not understand why it doesn't work.

Here is the codes:

public ContactPartHandler( IRepository<ContactPartRecord> repository ) {
            Filters.Add(StorageFilter.For(repository));

            OnIndexing<ContactPart>((context, contactPart) => context.DocumentIndex
                                                   .Add("contact_firstName", contactPart.Record.ContactRecord.FirstName).RemoveTags().Analyze().Store()
                                                   .Add("contact_lastName", contactPart.Record.ContactRecord.LastName).RemoveTags().Analyze().Store()
                                                   .Add("contact_email", contactPart.Record.ContactRecord.Email).RemoveTags().Analyze().Store()
                                                   .Add("contact_phone", contactPart.Record.ContactRecord.Phone).RemoveTags().Analyze().Store()
                                                    );
        }

 

 

Apr 25, 2011 at 7:38 PM

Once I cleaned out the database and start a new Orchard site it works!

Thanks!