Examine Search where date field is LessOrEqual to ...
# help-with-umbraco
s
I have an Examine search (Our.Umbraco.Extensions.Search), where I want to limit the output only to show results where the date field is less or equal to now ? Now my query looks like this:
Copy code
IBooleanOperation query = index.Searcher.CreatePublishedQuery().And().NodeTypeAlias(alias.ToLower());
j
Looks like the package doesn't have anything for datetimes, so the standard Examine way of searching should be the go-to: https://shazwazza.github.io/Examine/docs-v1-v2/searching.html?q=date#date-range
s
Cool, but how do I do that on a custom field ?
j
So let's say you have a doctype called "events" with a property called "startTime". Then something like this:
Copy code
csharp
IBooleanOperation query = index
    .Searcher.CreateQuery()
    .NodeTypeAlias("events")
    .And()
    .RangeQuery<DateTime>(["startTime"], DateTime.MinValue, DateTime.Now);
That would query for all documents of the nodeType events and the startTime between DateTime.MinValue and DateTime.Now
s
I think it's almost there. Can it be that I have to make FieldDefinition for the custom field ?
j
Oh yea probably, you can specify its type like this:
Copy code
csharp
public class ConfigureExternalIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
    public void Configure(string? name, LuceneDirectoryIndexOptions options)
    {
        if (name is not Constants.UmbracoIndexes.ExternalIndexName)
            return;

        options.FieldDefinitions.AddOrUpdate(
            new FieldDefinition("startTime", FieldDefinitionTypes.DateTime)
        );
    }

    public void Configure(LuceneDirectoryIndexOptions options)
    {
        Configure(string.Empty, options);
    }
}
And then within a composer add this:
Copy code
csharp
builder.Services.AddTransient<IConfigureOptions<LuceneDirectoryIndexOptions>, ConfigureExternalIndexOptions>();
s
Hmmmm ... my frontend keeps saying
Could not perform a range query on the field pageDate, it's value type is Examine.Lucene.Indexing.FullTextType
. It's like the composer is never picked up. I've attached a screendump of the composer and the IndexOptions. It's an Umbraco V13 if it makesany difference https://cdn.discordapp.com/attachments/1295341227687936064/1295360958780411975/image.png?ex=670e5e67&is=670d0ce7&hm=4fa5c1061215eb9de29a68ca3a94df256d161304a098e2b0251f639bd2b41db8&
j
Line 14 you set the type to fulltext 😉 Should be FieldDefinitionTypes.DateTime
s
Bingo, that was the culprit 🙂 How would you go about multiple languages in this scenario ? As for now I've done like this in the IndexOptions:
Copy code
public void Configure(string? name, LuceneDirectoryIndexOptions options)
    {
        if (name != Constants.UmbracoIndexes.ExternalIndexName) return;

        options.FieldDefinitions.AddOrUpdate(new FieldDefinition("pageDate_da", FieldDefinitionTypes.DateTime));
        options.FieldDefinitions.AddOrUpdate(new FieldDefinition("pageDate_en", FieldDefinitionTypes.DateTime));
    }
j
We do something like this:
Copy code
csharp
var languages = _localizationService.GetAllLanguages().ToList();
foreach (var language in languages)
{
    options.FieldDefinitions.AddOrUpdate(
        new FieldDefinition(
            $"nodeName_{language.CultureInfo}",
            FieldDefinitionTypes.FullTextSortable
        )
    );
10 Views