Indexes: Converting to JSON and Accessing Metadata
Entities passed to an index can be converted to JSON using the AsJson
method.
It is also possible to access metadata for a specified object using the MetadataFor
method.
- In this Page:
AsJson - Converting to JSON
class Products_AllProperties_Result
{
public ?string $query = null;
public function getQuery(): ?string
{
return $this->query;
}
public function setQuery(?string $query): void
{
$this->query = $query;
}
}
class Products_AllProperties extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$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", FieldIndexing::search());
}
}
$results = $session->query(Products_AllProperties_Result::class, Products_AllProperties::class)
->whereEquals("Query", "Chocolade")
->ofType(Product::class)
->toList();
MetadataFor - Accessing Metadata
class Products_WithMetadata_Result
{
public ?DateTime $lastModified = null;
public function getLastModified(): ?DateTime
{
return $this->lastModified;
}
public function setLastModified(?DateTime $lastModified): void
{
$this->lastModified = $lastModified;
}
}
class Products_WithMetadata extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.Products.Select(product => new { " .
" Product = Product, " .
" Metadata = this.MetadataFor(product) " .
"}).Select(this0 => new { " .
" LastModified = this0.metadata.Value\<DateTime\>(\"Last-Modified\") " .
"})";
}
}
$results = $session
->query(Products_WithMetadata_Result::class, Products_WithMetadata::class)
->orderByDescending("LastModified")
->ofType(Product::class)
->toList();