I have a collection of manufacturers, each of them makes several products. Manufacturers can have string tags, and I would like to search the products by their manufacturer tag (such that tags are inherited from the manufacturer by the product).

One way of doing that is to create an index which, in the TransformResults part, adds the manufacturer's tags to each product.

from product in results
select new {
    ...,
    Tags = Database.Load("manufacturers/"+ product.ManufacturerId).Tags
}

But I cannot seem to be able to query it:

this.Query<T>("TaggedProducts").Where(s => s.Tags.Any(tag => tag=="reliable"));

because Tags is not indexed. I am using RavenDB Studio.

This is the map:

from product in docs.Products
select new {
    product.Id,
    product.ManufacturerId
}

It is unclear to me whether I need to define a Field, I couldn't find any documentation explaining that.

These are the classes:

class Manufacturer
{
    public int Id {get; set;}
    public List<string> Tags {get; set;}
}

class Product
{
    public int ManufacturerId {get; set;}
    public int Id {get; set;}
}

Note that I am trying to avoid adding a Tags field to the Product class, if that's possible, because that would give the false impression that tags can be set in stories.

有帮助吗?

解决方案

What you put in the Map is what is being indexed and what you put in TransformResults is what you get back from a successful query. Therefore you can never query the tags unless you add them in the map.

What you could do is to use LoadDocument in the map. It is a fairly new feature that was made available in 2.0. The docs are here: http://ravendb.net/docs/2.0/client-api/querying/static-indexes/indexing-related-documents

This is what the map could look like in your case perhaps:

from product in docs.Products
select new {
    product.Id,
    product.ManufacturerId,
    Tags = LoadDocument("manufacturers/"+ product.ManufacturerId).Tags
}

The TransformResults you are using could remain the same.

A tip is to use the management studio and look what is indexed. You can do so in Indexes -> [your index] -> Query and then check the Index entries checkbox from the Query Options drop down.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top