Developer forum

Forum » CMS - Standard features » Repositories index query with datetime

Repositories index query with datetime

Thomas Larsen
Reply

How do I build an index query that uses DateTime?

An example:

I wants to build an query that only includes products where the current date is between two custom fields on the product (ProductActiveFrom and ProductActiveTo)


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

When Dynamicweb indexes dates, they are added to the index as the tick represenation of the data. The same happens if you add a criteria as a parameter of type DateTime. See Dump#1.

So when making a query with that setup, I just have to add a date in a format that .net understands and in a way so it does not misunderstand.

So I can search with dates as you can see in Dump #2. That results in a Lucene query that looks like this:

+Active:True +LanguageID:LANG1 +Updated:{636108768000000000 TO *]

So - you just add 2 citerias to your query in a group - one bigger than the active from date field and one less than the active to field - see dump#3.

Now it is possible to do the search - the only issue is to get the current DateTime.Now as a expression value in your query. For that you have to create a Macro (see dump#4)

And some mockup code for the macro (Sorry about the VB):

Imports System.Collections.Generic
Imports Dynamicweb.Extensibility.Macros

Public Class MyDateTimeMacro
    Inherits Macro

    Private Shared ReadOnly SupportedActionsInternal As New Dictionary(Of String, Func(Of Object)) From
        {
            {"DateTime.Now", Function() GetDateTimeNow()}
        }

    Private Shared ReadOnly LockObject As New Object()

    Public Overrides Function Evaluate(action As String) As Object
        'Prepare return value
        Dim value As Object = Nothing

        SyncLock LockObject
            'Make sure action is supported
            If SupportedActionsInternal.ContainsKey(action) Then
                Try
                    'Get value from action
                    value = SupportedActionsInternal(action)()
                Catch ex As Exception
                    'Maybe log this?
                End Try
            End If
        End SyncLock

        'Return the value if one was found, otherwise Nothing
        Return value
    End Function

    Public Overrides ReadOnly Property Name As String
        Get
            Return "Dynamicweb.MyDateTimeMacro"
        End Get
    End Property

    Public Overrides ReadOnly Property SupportedActions As IEnumerable(Of String)
        Get
            Dim result As IEnumerable(Of String)
            SyncLock LockObject
                result = SupportedActionsInternal.Keys.ToList()
            End SyncLock
            Return result
        End Get
    End Property

    Private Shared Function GetDateTimeNow() As Object
        Return DateTime.Now
    End Function
End Class

 

Capture.PNG Capture1.PNG Capture2.PNG Capture3.PNG
Votes for this answer: 1

 

You must be logged in to post in the forum