And now there's a requirement to get a certain timespan, say DeliveryTime
, which is calculated as follows:
DeliveryTime = (order.DeliveredOn - order.DispatchedOn) ?? (DateTime.UtcNow - order.DispatchedOn)
The DeliveryTime
is not stored on the document.
Since we cannot use DateTime.UtcNow
in the static index definition, we just save what we can, e.g.:
class OrderIndex\npublic DateTime DispatchedOn {get; set;}\npublic DateTime? DeliveredOn {get; set;}\npublic TimeSpan? DispatchedUntilDelivered {get; set;} // (DeliveredOn - DispatchedOn)\n
Now there's a requirement to filter by DeliveryTime
, which is not on the index.
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
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
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?
I would need my order logic to look something like:
\n=> index.DispatchedUntilDelivered == null ? (DateTime.UtcNow - index.DispatchedOn).TotalDays : index.DispatchedUntilDelivered.TotalDays\n
Is the only way to achieve this by using Custom Sorters? Or is there some other OrderBy
API I could use?
Thank you for your time & have a beautiful day :)
\nSven
Having said that....
\nHere 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:
\nDeliveryTime = (order.DeliveredOn - order.DispatchedOn) ?? (LoadDocument<Configuration>(\"configuration/current-day\").Today - order.DispatchedOn)\n
What does this do?
\nWhen the package was delivered, you use the actual time.
\nIf it isn't, you pull the current date from a document using LoadDocument
What does this matter?
\nconfiguration/current-day
document - all the documents that haven't been delivered will be updatedThe 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
\nAs a result, you can now easily query on DeliveryTime
and sort on it.
-
Hello everybody :) I have a bit of a problem and wanted to ask you guys opinion (essentially if there's a easier way to achieve my goal). DocumentSo what I have is the following document, say it's an
And now there's a requirement to get a certain timespan, say
The IndexSince we cannot use
FilteringNow there's a requirement to filter by For filtering this should be fine, given the APIs we should be able to solve this problem. would be something like
SortingNow there's a requirement to sort by I would need my order logic to look something like:
Is the only way to achieve this by using Custom Sorters? Or is there some other Thank you for your time & have a beautiful day :) |
Beta Was this translation helpful? Give feedback.
-
What does this mean order by delivery time? Given: What should be the order? Order (1) arrives first, but was 90 days in transit How would you sort those? |
Beta Was this translation helpful? Give feedback.
-
yes exactly, I want to sort by the time span, more specifically by the amount of days it took to deliver.
so, in your example it would be
it's really just about the time span, unfortunately not about the actual delivery date, that would be easy to do 😅 |
Beta Was this translation helpful? Give feedback.
-
Good morning, Thank you for all of the help! We concluded that we will be using the trick/workaround mentioned in #19327 (reply in thread), that does the trick for us :) TLDR: we now have a current-date clock in RavenDB that we reference in our static indexes. Thank you very much for your support on this manner, very appreciated. Best, |
Beta Was this translation helpful? Give feedback.
-
For reference, I wrote a blog post on this: |
Beta Was this translation helpful? Give feedback.
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:
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?
configuration/current-day
document - all the documents that haven't been delivered will be updatedThe idea is that hopefully you …