Hi,
I have been playing around with indexing and tried this:
I created a document products/1
{
"Name": "Awesome Book"
}
I created this Index (which doesn’t make much sense but I tried to understand the problem):
Map:
from product in docs.products
select new {
Id = product.Id,
Name = product.Name + " " + product.Id }
Transform:
from result in results
select new {
Name = result.Name,
Id = result.Id
}
The Name property of the transformation is always “Awesome Book” and not “Awesome Book products/1”
When I hit the “Terms” button in RavenDb Studio Name is “awesome book products/1”
What am I doing wrong?
Alex
Let's see your query
Oh yes. I think you would get the behavior you're expecting if you had a reduce step.
Thanks, this now works as expected:
Map:
from product in docs.products
select new {
Id = product.Id,
Name = product.Name }
Transform:
from result in results
select new {
Name = result.Name + " " + result.Id,
Id = result.Id
}
Btw: “Fields” (that can be stored etc.) always refer to properties of the map function, correct?
Alex
Thanks again.
Enhancing the transform function as follows (similar to http://ravendb.net/docs/client-api/querying/static-indexes/live-projections) results in an exception when trying to store the index:
from result in results
let data = database.Load<Product>(result.Id)
where data != null
select new {
Name = result.Name + " " + data.Name,
Id = result.Id
}
Exception:
c:\RavenDb\RavenDB.Server.1.2.2082-Unstable\tools\Databases\ProductTests\IndexDefinitions\TemporaryIndexDefinitionsAsSource\ougmjb2o.0.cs(33,15) : error CS0103: The name 'database' does not exist in the current context
c:\RavenDb\RavenDB.Server.1.2.2082-Unstable\tools\Databases\ProductTests\IndexDefinitions\TemporaryIndexDefinitionsAsSource\ougmjb2o.0.cs(33,29) : error CS0246: The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?)
I’m editing the index definition inside RavenDb studio – how can I invoke the database.Load<Product>(result.Id) call from there?
Works, thanks.
I guess, I found a bug:
This transform definition gets saved inside Studio without any exceptions:
from result in results
let data = Database.Load("/1) <-- missing closing quotation mark
where data != null
select new {
Criteria = data.SpecificCriterionInstances,
Id = result.Id
}
When executing it, it returns empty documents.
Can I also add a parameter here?
Transform:
from result in results
let data = Database.Load(param) <-- param possible here?
where data != null
select new {
Name = data.Name,
Id = result.Id
class Product {
public List<Criterion> Criteria {get;set;}
}
class Criterion {
public List<Role> Roles {get;set;}
}
That’s what I’m trying to do.
I’have been playing around a bit and it looks promising with Linq to object but I’m not done yet.
Anyhow: I’m using let in the Linq queries to solve that problem.
According to http://ravendb.net/docs/client-api/querying/using-linq-to-query-ravendb, let is not supported in RavenDb yet.
Is this valid for 1.2 also?
Any workarounds?
Here is the gist for the linq to object impl.:
https://gist.github.com/3854809
When using it with RavenDb I get an exception for the let assignment (or it’s usage) stating that RavenDb does not allow computation inside the query and I should use an index.
How can I replace the let using an index?
Client = client of our Web API, not database client.
Ok, that’s what I have thought about from the beginning but I thought if I could get the data already from the database in the required format without huge effort it would be better.
So, I’ll stick with the first approach.
Thanks,