不太清楚您所说的“标签应该单独查询以便在 GUI 中显示它们”是什么意思。但是每个图像的文档以及直接存储的每个标签的属性将是一个起点。
例如
public class Image
{
public string Id { get; set; }
public string Name { get; set; }
public Dictionary<string, Dictionary<string, object>> LabelProperties { get; set; }
}
然后,您可以按如下方式填充:
var img1 = new Image
{
Name = "Image1",
LabelProperties = new Dictionary<string, Dictionary<string, object>>
{
{
"Car",
new Dictionary<string, object>
{
{ "Brand", "GM"},
{ "Color", "Blue"},
{ "Engine type", "Big"},
{ "Total doors", 4}
}
},
{
"House",
new Dictionary<string, object>
{
{ "Location", "Downtown"},
{ "Color", "Blue"},
{ "Size", 240}
}
}
}
};
然后,这最终在数据库中看起来非常漂亮的结构 JSON:
{
"Name": "Image1",
"LabelProperties": {
"Car": {
"Brand": "GM",
"Color": "Blue",
"Engine type": "Big",
"Total doors": 4
},
"House": {
"Location": "Downtown",
"Color": "Blue",
"Size": 240
}
}
}
然后,您可以使用动态索引进行查询。例如查找所有包含蓝色房子的图像:
var blueHouses = session.Query<Image>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x => Equals(x.LabelProperties["House"]["Color"], "Blue"));
Console.WriteLine("--- All Images Containing a House of Color Blue");
foreach (var item in blueHouses)
{
Console.WriteLine("{0} | {1}", item.Id, item.Name);
}
如果您想查询标签本身,则可能需要索引。有关完整示例,请参阅要点。