For all documents satisfying a given criteria, I would like to patch update a field in those documents. For this, I wrote a query in LINQ Pad that gets me the subset of documents needed.
That is :
Session.Query<Movie>()
.Where(x => x.Status == "Released" && x.IsDeleted == false && x.ReleaseDate.Date < new DateTime(2012, 4, 3))
.Dump();
Now I have the list of documents. But I need to modify a field. For this I wrote an index and patch command.. I need help completing this task.
Patch code (incomplete) :
store.DatabaseCommands.UpdateByIndex("Movies/NewIndexName",
new IndexQuery { Query = "IsDeleted:false" },
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Status",
Value = "Testing"
}
}, allowStale: false);
Index is
from doc in docs.Movies
select new { ReleaseDate = doc.ReleaseDate.Date, IsDeleted = doc.IsDeleted, Status =
doc.Status }
Specifically my question is.. is my approach correct.. and if it is, how do I add multiple criteria in the condition.. where it says Query = “IsDeleted:false” can I add more comparison checks to the query (like in the LINQ query above)?
EDIT: (USING DANIEL LANG’S SOLUTION)
This gives format exception.. Input string not in correct format.
store.DatabaseCommands.UpdateByIndex("Movies/NewIndexName",
new IndexQuery
{
Query =
string.Format("Status:Released AND IsDeleted:false AND ReleaseDate:{* TO {0}}",
DateTools.DateToString(new DateTime(2012, 4, 3),
DateTools.Resolution.MILLISECOND))
},
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Status",
Value = "TestingReleased"
}
}, allowStale: false);
Yes, you can have multiple conditions and combine them using standard boolean operations. You’re actually building a lucene query here, so let’s take a look at this website to learn more about how you can write them: http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html#Boolean%20operators
The thing about dates is, that raven stores them (as everything else) as a string and the format it uses is specifically chosen to enable range queries and that kind of stuff, so it might not be what you expect…
So in order to query for the date, you need to format it in a special way. There is a class DateTools inside Raven.Abstractions.Linq that has a DateToString method that helps.
You can construct the same query from above like this:
(EDITED 4/27 to add an extra {} in the string)