Indexes: Converting to JSON and Accessing Metadata
Entities passed to an index can be converted to JSON using AsJson
method. There is also a possibility to access metadata for a specified object using MetadataFor
method.
AsJson - Converting to JSON
class Products_AllProperties extends AbstractIndexCreationTask {
constructor() {
super();
this.map = "docs.Products.Select(product => new { " +
// convert product to JSON and select all properties from it
" Query = this.AsJson(product).Select(x => x.Value) " +
"})";
// mark 'query' field as analyzed which enables full text search operations
this.index("Query", "Search");
}
}
const results = await session
.query({ indexName: "Products/AllProperties" })
.whereEquals("Query", "Chocolade")
.ofType(Product)
.all();
MetadataFor - Accessing Metadata
class Products_WithMetadata extends AbstractIndexCreationTask {
constructor() {
super();
this.map = "docs.Products.Select(product => new { " +
" Product = product, " +
" Metadata = this.MetadataFor(product) " +
"}).Select(this0 => new { " +
" LastModified = this0.Metadata.Value<DateTime>(\"Last-Modified\") " +
"})";
}
}
const results = await session
.query({ indexName: "Products/WithMetadata" })
.orderByDescending("LastModified")
.ofType(Product)
.all();