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 {\n" +
            // convert product to JSON and select all properties from it
            "    Query = this.AsJson(product).Select(x => x.Value)\n" +
            "})";

        // 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 {\n" +
            "    Product = product,\n" +
            "    Metadata = this.MetadataFor(product)\n" +
            "}).Select(this_0 => new {\n" +
            "       LastModified = this_0.Metadata.Value<DateTime>(\'Last-Modified'\)\n"+
            "   })";
    }
}

const results = await session
    .query({ indexName: "Products/WithMetadata" })
    .orderByDescending("LastModified")
    .ofType(Product)
    .all();