2lach
03/15/2024, 6:37 AMsearchableDateField
, doesn't seem to be indexed in the same manner as Umbraco’s built-in createDate field, despite my efforts to convert searchableDateField
to the DateTime format ticks, which I believe is also used for createDate
.
When I execute the following query:
query.And(q => q.RangeQuery(new[] { "searchableDateField" }, startDate, endDate, minInclusive: true, maxInclusive: false));
I receive this error message from Examine:
"An error occurred during the search: Could not perform a range query on the field searchableDateField, its value type is Examine.Lucene.Indexing.FullTextType"
Interestingly, when I replace searchableDateField
with createDate
, the query works fine and returns the expected results (the documents with a `createDate`between DateTime startDate
and DateTime endDate
query.And(q => q.RangeQuery(new[] { "createDate" }, startDate, endDate, minInclusive: true, maxInclusive: false));
I currently save the value of searchableDateField
in a label in Umbraco as a DateTime (DateTime format Ticks).
I have also tried saving the searchableDateField
in various DateTime formats (both in dateTime labels and fields) and in the compatible types of labels, such as string, BigInt, etc. i could think of and then casting them as DateTime values.
However, my results remains the same as described above.
The main difference I can think of is that createDate
exists on all my DocumentTypes, whereas searchableDateField
does not always exist on all DocumentTypes.
If anyone has experience working with custom date fields in Examine and Umbraco and can offer guidance or suggest a solution (even a creative one, as I really want to solve this), I would be very grateful for any tips or advice.
ThanksJemayn
03/15/2024, 8:09 AMcsharp
public class ExternalIndexOptionsOverwrite : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
public void Configure(LuceneDirectoryIndexOptions options) => Configure(string.Empty, options);
public void Configure(string? name, LuceneDirectoryIndexOptions options)
{
if (name?.Equals(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName) is not true) return;
options.FieldDefinitions.AddOrUpdate(new FieldDefinition("searchableDateField", FieldDefinitionTypes.DateTime));
}
}
Then register it in a composer:
builder.Services.AddSingleton<IConfigureOptions<LuceneDirectoryIndexOptions>, ExternalIndexOptionsOverwrite>();
2lach
03/15/2024, 9:22 AM2lach
03/15/2024, 10:44 AM// ExternalIndexOptionsOverwrite.cs
using Examine;
using Examine.Lucene;
using Microsoft.Extensions.Options;
public class ExternalIndexOptionsOverwrite : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
public void Configure(LuceneDirectoryIndexOptions options) => Configure(string.Empty, options);
public void Configure(string? name, LuceneDirectoryIndexOptions options)
{
if (!name?.Equals(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName) ?? true) return;
options.FieldDefinitions.AddOrUpdate(new Examine.FieldDefinition("searchableDateField", FieldDefinitionTypes.DateTime));
}
}
I've registered the service in my Program.cs:
// Examine config service
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IConfigureOptions<LuceneDirectoryIndexOptions>, ExternalIndexOptionsOverwrite>()
And here is how I'm querying using the field in my SearchApi.cs file:
DateTime? startDate = searchRequest.StartDate;
DateTime? endDate = searchRequest.EndDate;
query.And(q => q.RangeQuery(new[] { "searchableDateField" }, startDate, endDate, minInclusive: true, maxInclusive: true));
Despite this, I'm still receiving the same error when attempting to search date ranges with the above query:
"An error occurred during the search: Could not perform a range query on the field searchableDateField, its value type is Examine.Lucene.Indexing.FullTextType"
After manually rebuilding Examine indexes
If you could help me pinpoint what I might be doing wrong or what I'm missing? It would be greatly appreciated.Mike Chambers
03/15/2024, 10:54 AMMike Chambers
03/15/2024, 10:55 AMJemayn
03/15/2024, 10:59 AMJemayn
03/15/2024, 10:59 AMMike Chambers
03/15/2024, 11:00 AMMike Chambers
03/15/2024, 11:01 AMMike Chambers
03/15/2024, 11:04 AMcsharp
public class SearchComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
// Custom Examine configuration
builder.Services.ConfigureOptions<ConfigureExamineIndexOptions>();
}
}
Mike Chambers
03/15/2024, 11:06 AM2lach
03/15/2024, 11:17 AMsearchableDateField
exists Examines external index, but the error still persisted.
As a note, I've also added fields such as searchableGeoGraphicalLocations, searchableLabels, and searchableSubjectAreas, which all worked perfectly from the outset without any need for special indexing configurations, so i think it might be related to how examine uses the RangeQuery or perhaps how i use it.
Next, I plan to try using a composer to see if it makes any difference. I'm hoping this might provide a different approach to resolve the ongoing issue with date range searches.
https://cdn.discordapp.com/attachments/1218085550926331914/1218156128937054329/Screenshot_2024-03-15_at_12.09.52.JPG?ex=6606a34d&is=65f42e4d&hm=e0e489974a674dab98baca5eab90bb1c544d2fd513cda09b88577bd69fc6cca7&2lach
03/15/2024, 12:12 PMMike Chambers
03/15/2024, 4:33 PMquery.And().RangeQuery<DateTime>(new[] { expiresAlias }, min: DateTime.Now, max: null);
Mike Chambers
03/15/2024, 4:36 PMMike Chambers
03/15/2024, 4:37 PMMike Chambers
03/15/2024, 4:42 PMMike Chambers
03/15/2024, 4:44 PMMike Chambers
03/15/2024, 4:45 PM2lach
03/15/2024, 5:43 PM