|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Configuration |
| 4 | +permalink: /configuration |
| 5 | +uid: configuration |
| 6 | +order: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +Configuration |
| 10 | +=== |
| 11 | + |
| 12 | +An index can be configured in many ways including different configurations per field such as how those values are analyzed, indexed, tokenized ... basically how the data is stored and retrieved. This is done via Examine ["Value Types"](#value-types). |
| 13 | + |
| 14 | +_**Note**: This documentation refers to using Lucene based indexes in Examine (the default index type shipped in Examine)._ |
| 15 | + |
| 16 | +## Field definitions |
| 17 | + |
| 18 | +A Field Definition is a mapping of a field name to a ["Value Types"](#value-types). By default all fields are mapped to the default Value Type: [`FieldDefinitionTypes.FullText`](xref:Examine.FieldDefinitionTypes#Examine_FieldDefinitionTypes_FullText). |
| 19 | + |
| 20 | +You can map a field to any value type when configuring the index. |
| 21 | + |
| 22 | +### IConfigureNamedOptions |
| 23 | + |
| 24 | +Configuration of Examine indexes is done with [.NET's Options pattern](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0). For Examine, this is done with named options: [`IConfigureNamedOptions`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.iconfigurenamedoptions-1). |
| 25 | + |
| 26 | +There are several options that can be configured, the most common ones are: |
| 27 | + |
| 28 | +* __FieldDefinitions__ _[`FieldDefinitionCollection`](xref:Examine.FieldDefinitionCollection)_ - Manages the mappings between a field name and it's index value type |
| 29 | +* __Analyzer__ _`Analyzer`_ - The default Lucene Analyzer to use for each field (default = [`StandardAnalyzer`](https://lucenenet.apache.org/docs/4.8.0-beta00016/api/analysis-common/Lucene.Net.Analysis.Standard.StandardAnalyzer.html)) |
| 30 | +* __Validator__ _[`IValueSetValidator`]([`IValueSetValidator`](xref:Examine.IValueSetValidator))_ - Used to validate a value set to be indexed, if validation fails it will not be indexed |
| 31 | +* __[IndexValueTypesFactory](xref:Examine.Lucene.IFieldValueTypeFactory)__ _`IReadOnlyDictionary<string, IFieldValueTypeFactory>`_ - Allows you to define custom Value Types |
| 32 | + |
| 33 | +```cs |
| 34 | +/// <summary> |
| 35 | +/// Configure Examine indexes using .NET IOptions |
| 36 | +/// </summary> |
| 37 | +public sealed class ConfigureIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions> |
| 38 | +{ |
| 39 | + public void Configure(string name, LuceneDirectoryIndexOptions options) |
| 40 | + { |
| 41 | + switch (name) |
| 42 | + { |
| 43 | + case "MyIndex": |
| 44 | + // Set the "Price" field to map to the 'Double' value type. |
| 45 | + options.FieldDefinitions.AddOrUpdate( |
| 46 | + new FieldDefinition("Price", FieldDefinitionTypes.Double)); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void Configure(LuceneDirectoryIndexOptions options) |
| 52 | + => Configure(string.Empty, options); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### After construction |
| 57 | + |
| 58 | +You can modify the field definitions [FieldDefinitionCollection](xref:Examine.FieldDefinitionCollection) for an index after it is constructed by using any of the following methods: |
| 59 | + |
| 60 | +* `myIndex.FieldDefinitionCollection.TryAdd` |
| 61 | +* `myIndex.FieldDefinitionCollection.AddOrUpdate` |
| 62 | +* `myIndex.FieldDefinitionCollection.GetOrAdd` |
| 63 | + |
| 64 | +These modifications __must__ be done before any indexing or searching is executed. |
| 65 | + |
| 66 | +### Add a field value type after construction |
| 67 | + |
| 68 | +It is possible to add custom field value types after the construction of the index, but this must be done before the index is used. Some people may prefer this method of adding custom field value types. Generally, these should be modified directly after the construction of the index. |
| 69 | + |
| 70 | +```cs |
| 71 | +// Create the index with all of the defaults |
| 72 | +var myIndex = new LuceneIndex( |
| 73 | + "MyIndex", |
| 74 | + new SimpleFSDirectory(new DirectoryInfo("C:\\TestIndexes"))); |
| 75 | + |
| 76 | +// Add a custom field value type |
| 77 | +myIndex.FieldValueTypeCollection.ValueTypeFactories |
| 78 | + .TryAdd( |
| 79 | + "phonenumber", |
| 80 | + name => new GenericAnalyzerFieldValueType( |
| 81 | + name, |
| 82 | + new PhoneNumberAnalyzer())); |
| 83 | + |
| 84 | +// Map a field to use the custom field value type |
| 85 | +myIndex.FieldDefinitionCollection.TryAdd( |
| 86 | + new FieldDefinition("Phone", "phonenumber")); |
| 87 | +``` |
| 88 | + |
| 89 | +## Value types |
| 90 | + |
| 91 | +Value types are responsible for: |
| 92 | + |
| 93 | +* Defining a field name and if the field should be sortable, the field to store the sortable data |
| 94 | +* Adding a field value to an index document |
| 95 | +* Configuring how the value will be stored in the index |
| 96 | +* Configuring the analyzer for the field |
| 97 | +* Generating the Query for the field |
| 98 | + |
| 99 | +These are the default field value types provided with Examine. Each value type can be resolved from the static class [`Examine.FieldDefinitionTypes`](xref:Examine.FieldDefinitionTypes) (i.e. [`Examine.FieldDefinitionTypes.FullText`](xref:Examine.FieldDefinitionTypes#Examine_FieldDefinitionTypes_FullText)). |
| 100 | + |
| 101 | +| Value Type | Description | Sortable | |
| 102 | +|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------| |
| 103 | +| FullText | __Default__.<br />The field will be indexed with the index's <br />default Analyzer without any sortability. <br />Generally this is fine for normal text searching. | ❌ | |
| 104 | +| FullTextSortable | Will be indexed with FullText but also <br />enable sorting on this field for search results. <br />_FullText sortability adds additional overhead <br />since it requires an additional index field._ | ✅ | |
| 105 | +| Integer | Stored as a numerical structure. | ✅ | |
| 106 | +| Float | Stored as a numerical structure. | ✅ | |
| 107 | +| Double | Stored as a numerical structure. | ✅ | |
| 108 | +| Long | Stored as a numerical structure. | ✅ | |
| 109 | +| DateTime | Stored as a DateTime, <br />represented by a numerical structure. | ✅ | |
| 110 | +| DateYear | Just like DateTime but with <br />precision only to the year. | ✅ | |
| 111 | +| DateMonth | Just like DateTime but with <br />precision only to the month. | ✅ | |
| 112 | +| DateDay | Just like DateTime but with <br />precision only to the day. | ✅ | |
| 113 | +| DateHour | Just like DateTime but with <br />precision only to the hour. | ✅ | |
| 114 | +| DateMinute | Just like DateTime but with <br />precision only to the minute. | ✅ | |
| 115 | +| EmailAddress | Uses custom analyzers for dealing <br />with email address searching. | ❌ | |
| 116 | +| InvariantCultureIgnoreCase | Uses custom analyzers for dealing with text so it<br /> can be searched on regardless of the culture/casing. | ❌ | |
| 117 | +| Raw | Will be indexed without analysis, searching will<br /> only match with an exact value. | ❌ | |
| 118 | + |
| 119 | +### Custom field value types |
| 120 | + |
| 121 | +A field value type is defined by [`IIndexFieldValueType`](xref:Examine.Lucene.Indexing.IIndexFieldValueType) |
| 122 | + |
| 123 | +_**Tip**: There are many implementations of IIndexFieldValueType in the source code to use as examples/reference._ |
| 124 | + |
| 125 | +A common base class that can be used for field value types is: [`IndexFieldValueTypeBase`](xref:Examine.Lucene.Indexing.IndexFieldValueTypeBase). |
| 126 | + |
| 127 | +A common implementation that can be used for field value types for custom Analyzers is: [`GenericAnalyzerFieldValueType`](xref:Examine.Lucene.Indexing.GenericAnalyzerFieldValueType). |
| 128 | + |
| 129 | +#### Example - Phone Number |
| 130 | + |
| 131 | +A phone number stored in Lucene could require a custom analyzer to index and search it properly. So the best way to set this up in Examine would be to have a custom field value type for it. Since this field value type doesn't need to do anything more fancy than to provide a custom analyzer, we can create it with the [`GenericAnalyzerFieldValueType`](xref:Examine.Lucene.Indexing.GenericAnalyzerFieldValueType). |
| 132 | + |
| 133 | +```cs |
| 134 | +/// <summary> |
| 135 | +/// Configure Examine indexes using .NET IOptions |
| 136 | +/// </summary> |
| 137 | +public sealed class ConfigureIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions> |
| 138 | +{ |
| 139 | + private readonly ILoggerFactory _loggerFactory; |
| 140 | + |
| 141 | + public ConfigureIndexOptions(ILoggerFactory loggerFactory) |
| 142 | + => _loggerFactory = loggerFactory; |
| 143 | + |
| 144 | + public void Configure(string name, LuceneDirectoryIndexOptions options) |
| 145 | + { |
| 146 | + switch (name) |
| 147 | + { |
| 148 | + case "MyIndex": |
| 149 | + // Create a dictionary for custom value types. |
| 150 | + // They keys are the value type names. |
| 151 | + options.IndexValueTypesFactory = new Dictionary<string, IFieldValueTypeFactory> |
| 152 | + { |
| 153 | + // Create a phone number value type using the GenericAnalyzerFieldValueType |
| 154 | + // to pass in a custom analyzer. As an example, it could use Examine's |
| 155 | + // PatternAnalyzer to pass in a phone number pattern to match. |
| 156 | + ["phone"] = new DelegateFieldValueTypeFactory(name => |
| 157 | + new GenericAnalyzerFieldValueType( |
| 158 | + name, |
| 159 | + _loggerFactory, |
| 160 | + new PatternAnalyzer(@"\d{3}\s\d{3}\s\d{4}", 0))) |
| 161 | + }; |
| 162 | + |
| 163 | + // Add the field definition for a field called "phone" which maps |
| 164 | + // to a Value Type called "phone" defined above. |
| 165 | + options.FieldDefinitions.AddOrUpdate(new FieldDefinition("phone", "phone")); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public void Configure(LuceneDirectoryIndexOptions options) |
| 171 | + => throw new NotImplementedException("This is never called and is just part of the interface"); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +The above creates a custom field value type using a custom analyzer and maps the "Phone" field to use this value type. |
| 176 | + |
| 177 | +## ValueSet validators |
| 178 | + |
| 179 | +An [`IValueSetValidator`](xref:Examine.IValueSetValidator) is a simple interface: |
| 180 | + |
| 181 | +```cs |
| 182 | +public interface IValueSetValidator |
| 183 | +{ |
| 184 | + ValueSetValidationResult Validate(ValueSet valueSet); |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +That returns an result [`ValueSetValidationResult`](xref:Examine.ValueSetValidationResult) with an enum of [`ValueSetValidationStatus`](xref:Examine.ValueSetValidationStatus) values: |
| 189 | + |
| 190 | +* `Valid` - The ValueSet is valid and will be indexed |
| 191 | +* `Failed` - The ValueSet was invalid and will not be indexed |
| 192 | +* `Filtered` - The ValueSet has been filtered/modified by the validator and will be indexed |
| 193 | + |
| 194 | +Examine only has one implementation: [`ValueSetValidatorDelegate`](xref:Examine.Lucene.Providers.ValueSetValidatorDelegate) which can be used by developers as a simple way to create a validator based on a callback, else developers can implement this interface if required. By default, no ValueSet validation is done with Examine. |
0 commit comments