Adding custom field to the search index

If you're creating advanced search pages, you normally want to have custom fields in the search index for properties in Webnodes, in order to create various filters.

Basic solution

The easiest way to add a custom field is to go to the settings of the property you want to get a custom field in the Lucene search index and check the "Create custom index field" option:

 custom_index_field

 If you want another name than the property name as the field name, you can specify it in the "Custom index field name". 

Advanced solution

Using the property setting as shown above is one way to create custom fields in the search index, but sometimes you want more control over what is indexed in the custom field. The method above uses the standard GetSearchIndex method for the relevant property class to generate the contents of the custom field. One example where this might not be ideal is for relation properties. The default will index the names of the related nodes. But if you're using it for a advanced search page, you usually want to index the node ids of the related nodes.

The easiest way to get full control over the creation of custom fields, is to override the method "BuildSearchIndex" for the content classes you want to alter the default indexing for.

The BuildSearchIndex method has the Lucene document as input. You can use the full Lucene.Net API to add the fields you want. An example:

 public override void BuildSearchIndex(Lucene.Net.Documents.Document doc) {
     base.BuildSearchIndex(doc);            
     if (this.IncidentType.IsSet()) doc.Add(new Field("IncidentType", this.IncidentType.GetId(false, false).ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
     if (!this.AircraftCategory.IsEmpty()) {
          foreach (AviationAircraftCategory ac in this.AircraftCategory.Get()){
               doc.Add(new Field("AircraftCategory", ac.NodeId.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
          }
     }
}