Skip to content
\n

And now there's a requirement to get a certain timespan, say DeliveryTime, which is calculated as follows:

\n

DeliveryTime = (order.DeliveredOn - order.DispatchedOn) ?? (DateTime.UtcNow - order.DispatchedOn)

\n

The DeliveryTime is not stored on the document.

\n

Index

\n

Since we cannot use DateTime.UtcNow in the static index definition, we just save what we can, e.g.:

\n
class OrderIndex\npublic DateTime DispatchedOn {get; set;}\npublic DateTime? DeliveredOn {get; set;}\npublic TimeSpan? DispatchedUntilDelivered {get; set;}     // (DeliveredOn - DispatchedOn)\n
\n

Filtering

\n

Now there's a requirement to filter by DeliveryTime, which is not on the index.

\n

For filtering this should be fine, given the APIs we should be able to solve this problem.
\nExample:
\nget me all items where delivery-time is greater than 5 days

\n

would be something like

\n
            .OpenSubclause()\n            .WhereExists(x => x.DispatchedUntilDelivered)\n            .AndAlso()\n            .WhereGreaterThan(x => x.DispatchedUntilDelivered, TimeSpan.FromDays(5))\n            .CloseSubclause()\n            .OrElse()\n            .OpenSubclause()\n            .Not.WhereExists(x => x.DispatchedUntilDelivered)\n            .AndAlso()\n            .WhereLessThan(x => x.DispatchedOn, DateTime.UtcNow.AddDays(-5))\n            .CloseSubclause()\n
\n

Sorting

\n

Now there's a requirement to sort by DeliveryTime, which is still not on the index.
\nHere's where my question is, I don't see much flexibility there in the API. We are only able to sort by one column, or am I missing something?

\n

I would need my order logic to look something like:

\n
=> index.DispatchedUntilDelivered == null ? (DateTime.UtcNow - index.DispatchedOn).TotalDays : index.DispatchedUntilDelivered.TotalDays\n
\n

Is the only way to achieve this by using Custom Sorters? Or is there some other OrderBy API I could use?

\n

Thank you for your time & have a beautiful day :)
\nSven

","upvoteCount":1,"answerCount":4,"acceptedAnswer":{"@type":"Answer","text":"

Having said that....

\n

Here is a trick you can pull, create a document with: configuration/current-day with the current date, update it once a day.

\n

Then, in your index, you can do:

\n
DeliveryTime = (order.DeliveredOn - order.DispatchedOn) ?? (LoadDocument<Configuration>(\"configuration/current-day\").Today  - order.DispatchedOn)\n
\n

What does this do?

\n

When the package was delivered, you use the actual time.
\nIf it isn't, you pull the current date from a document using LoadDocument

\n

What does this matter?

\n\n

The idea is that hopefully you have a lot less of them, and you can schedule this for an off time (midnight, for example), and then the database would re-index those

\n

As a result, you can now easily query on DeliveryTime and sort on it.

","upvoteCount":0,"url":"https://github.com/ravendb/ravendb/discussions/19327#discussioncomment-10749239"}}}

ordering with DateTime.UtcNow #19327

Closed Answered by ayende
sverbach asked this question in Q&A
Sep 24, 2024 · 4 comments · 11 replies
Discussion options

You must be logged in to vote

Having said that....

Here is a trick you can pull, create a document with: configuration/current-day with the current date, update it once a day.

Then, in your index, you can do:

DeliveryTime = (order.DeliveredOn - order.DispatchedOn) ?? (LoadDocument<Configuration>("configuration/current-day").Today  - order.DispatchedOn)

What does this do?

When the package was delivered, you use the actual time.
If it isn't, you pull the current date from a document using LoadDocument

What does this matter?

  • You aren't computing this each and every time
  • When you update the configuration/current-day document - all the documents that haven't been delivered will be updated

The idea is that hopefully you …

Replies: 4 comments 11 replies

Comment options

You must be logged in to vote
1 reply
@sverbach
Comment options

Comment options

You must be logged in to vote
10 replies
@ayende
Comment options

@sverbach
Comment options

@ayende
Comment options

@ayende
Comment options

Answer selected by sverbach
@sverbach
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants