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
public static class Products_AllProperties extends AbstractIndexCreationTask {
public static class Result {
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
public Products_AllProperties() {
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
index("Query", FieldIndexing.SEARCH);;
}
}
List<Product> results = session
.query(Products_AllProperties.Result.class, Products_AllProperties.class)
.whereEquals("Query", "Chocolade")
.ofType(Product.class)
.toList();
MetadataFor - Accessing Metadata
public static class Products_WithMetadata extends AbstractIndexCreationTask {
public static class Result {
private Date lastModified;
public Date getLastModified() {
return lastModified;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
}
public Products_WithMetadata() {
map = "docs.Products.Select(product => new { " +
" Product = Product, " +
" Metadata = this.MetadataFor(product) " +
"}).Select(this0 => new { " +
" LastModified = this0.metadata.Value<DateTime>(\"Last-Modified\") " +
"})";
}
}
List<Product> results = session
.query(Products_WithMetadata.Result.class, Products_WithMetadata.class)
.orderByDescending("LastModified")
.ofType(Product.class)
.toList();