#Examine Search where date field is LessOrEqual to Now ?

1 messages · Page 1 of 1 (latest)

charred patrol
#

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:

IBooleanOperation query = index.Searcher.CreatePublishedQuery().And().NodeTypeAlias(alias.ToLower());
edgy igloo
charred patrol
#

Cool, but how do I do that on a custom field ?

edgy igloo
#

So let's say you have a doctype called "events" with a property called "startTime". Then something like this:

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

charred patrol
#

I think it's almost there. Can it be that I have to make FieldDefinition for the custom field ?

edgy igloo
#

Oh yea probably, you can specify its type like this:

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:

builder.Services.AddTransient<IConfigureOptions<LuceneDirectoryIndexOptions>, ConfigureExternalIndexOptions>();
charred patrol
#

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

edgy igloo
#

Line 14 you set the type to fulltext 😉

Should be FieldDefinitionTypes.DateTime

charred patrol
#

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:

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));
    }