Converting to JSON and accessing metadata
Entities passed to index can be converted to JSON using AsDocument
method, there is also a possibility to access metadata for a specified object using MetadataFor
method.
AsDocument - converting to JSON
public class Products_AllProperties : AbstractIndexCreationTask<Product, Products_AllProperties.Result>
{
public class Result
{
public string Query { get; set; }
}
public Products_AllProperties()
{
Map = products => from product in products
select new
{
// convert product to JSON and select all properties from it
Query = AsDocument(product).Select(x => x.Value)
};
// mark 'Query' field as analyzed which enables full text search operations
Index(x => x.Query, FieldIndexing.Analyzed);
}
}
IList<Product> results = session
.Query<Products_AllProperties.Result, Products_AllProperties>()
.Where(x => x.Query == "Chocolade")
.OfType<Product>()
.ToList();
MetadataFor - accessing metadata
public class Products_WithMetadata : AbstractIndexCreationTask<Product>
{
public class Result
{
public DateTime LastModified { get; set; }
}
public Products_WithMetadata()
{
Map = products => from product in products
let metadata = MetadataFor(product)
select new
{
LastModified = metadata.Value<DateTime>("Last-Modified")
};
}
}
IList<Product> results = session
.Query<Products_WithMetadata.Result, Products_WithMetadata>()
.OrderByDescending(x => x.LastModified)
.OfType<Product>()
.ToList();