基本使用
首先,我们必须将Solr文档映射到一个类。 让我们使用Solr发行版附带的默认模式的一个子集:
public class Product {
[SolrUniqueKey("id")]
public string Id { get; set; }
[SolrField("manu_exact")]
public string Manufacturer { get; set; }
[SolrField("cat")]
public ICollection<string> Categories { get; set; }
[SolrField("price")]
public decimal Price { get; set; }
[SolrField("inStock")]
public bool InStock { get; set; }
}
它只是一个具有一些属性的POCO:SolrField
将属性映射到Solr字段,SolrUniqueKey
(可选但推荐)将属性映射到Solr唯一键字段。
现在我们将使用这个映射类编写一些测试。 我们来初始化类库:
[TestFixtureSetUp]
public void FixtureSetup() {
Startup.Init<Product>("http://localhost:8983/solr");
}
我们来添加一个文档(确保你运行这个测试之前有一个运行的Solr实例):
[Test]
public void Add() {
var p = new Product {
Id = "SP2514N",
Manufacturer = "Samsung Electronics Co. Ltd.",
Categories = new[] {
"electronics",
"hard drive",
},
Price = 92,
InStock = true,
};
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Add(p);
solr.Commit();
}
我们来看看文档是否在我们放置的地方:
[Test]
public void Query() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var results = solr.Query(new SolrQueryByField("id", "SP2514N"));
Assert.AreEqual(1, results.Count);
Console.WriteLine(results[0].Price);
}
您的schema.xml中定义的Solr字段必须映射到.NET类中的属性。
SolrNet不会基于.NET代码编写schema.xml。 这取决于您是否保持同步。 请注意,有时候,您可能不希望exactly同步完成,因为某些字段可能在.NET和Solr中有所不同,或者您可能希望在Solr中为其他用例添加其他字段。
在SolrNet中,目前有三种映射字段的内置方式:
属性 (default)
使用此方法,您可以使用SolrField
和SolrUniqueKey
属性来装饰要映射的属性。 attribute
参数表示相应的Solr字段名称。
例:
public class Product {
[SolrUniqueKey("id")]
public string Id { get; set; }
[SolrField("manu_exact")]
public string Manufacturer { get; set; }
[SolrField("cat")] // cat is a multiValued field
public ICollection<string> Categories { get; set; }
[SolrField("price")]
public decimal Price { get; set; }
[SolrField("inStock")]
public bool InStock { get; set; }
[SolrField("timestamp")]
public DateTime Timestamp { get; set; }
[SolrField("weight")]
public double? Weight { get; set;} // nullable property, it might not be defined on all documents.
}
这种映射方式由AttributesMappingManager
类实现。
index-time 字段 提升
您还可以使用映射属性在升级时将引用应用于特定字段
[SolrField("inStock", Boost = 10.5)]
public bool InStock { get; set; }
每次文档索引时,这将为InStock字段增加10.5。
All-properties
这将类的每个属性映射到与属性完全相同的名称的字段(请注意,Solr字段名称区分大小写)。 它由AllPropertiesMappingManager
类实现。 请注意,唯一键不能被推断,因此必须被明确地映射。 与以上相同的映射可以这样完成:
public class Product {
public string id { get; set; }
public string manu_exact { get; set; }
public ICollection<string> cat { get; set; }
public decimal price { get; set; }
public bool inStock { get; set; }
public DateTime timestamp { get; set; }
public double? weight { get; set; }
}
然后添加唯一键:
var mapper = new AllPropertiesMappingManager();
mapper.SetUniqueKey(typeof(Product).GetProperty("id"));
手动映射
这允许您以编程方式定义每个属性的字段:
public class Product {
public string Id { get; set; }
public string Manufacturer { get; set; }
public ICollection<string> Categories { get; set; }
public decimal Price { get; set; }
public bool InStock { get; set; }
public DateTime Timestamp { get; set; }
public double? Weight { get; set; }
}
var mgr = new MappingManager();
var property = typeof (Product).GetProperty("Id");
mgr.Add(property, "id");
mgr.SetUniqueKey(property);
mgr.Add(typeof(Product).GetProperty("Manufacturer"), "manu_exact");
mgr.Add(typeof(Product).GetProperty("Categories"), "cat_exact");
mgr.Add(typeof(Product).GetProperty("Price"), "price");
mgr.Add(typeof(Product).GetProperty("InStock"), "inStock");
mgr.Add(typeof(Product).GetProperty("Timestamp"), "timestamp");
mgr.Add(typeof(Product).GetProperty("Weight"), "weight");
字典映射和动态字段
Solr dynamicFields可以根据用例不同地映射。 它们可以被“静态地”映射,例如,给定:
<dynamicField name="price_*" type="integer" indexed="true" stored="true"/>
一个特定的dynamicField实例可以映射为:
[SolrField("price_i")]
public decimal? Price {get;set;}
然而,通常需要有更多的灵活性。 您还可以将dynamicFields映射为字典,并使用字段名前缀:
[SolrField("price_")]
public IDictionary<string, decimal> Price {get;set;}
在这种情况下,price_用作实际Solr字段名称的前缀,例如。 使用此映射,Price [“regular”]映射到名为price_regular的Solr字段。
另一个,甚至更灵活的映射:
[SolrField("*")]
public IDictionary<string, object> OtherFields {get;set;}
对于任何其他未映射的字段,它充当全部容器。 例如。 OtherFields [“price_i”]映射到名为price_i的Solr字段。
完全松散的映射
通过使用Dictionary
Startup.Init<Dictionary<string, object>>(serverUrl);
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Dictionary<string, object>>>();
solr.Add(new Dictionary<string, object> {
{"field1", 1},
{"field2", "something else"},
{"field3", new DateTime(2010, 5, 5, 12, 23, 34)},
{"field4", new[] {1,2,3}},
});
当获取作为Dictionary
ISolrOperations<Dictionary<string, object>> solr = ...
ICollection<Dictionary<string, object>> results = solr.Query(SolrQuery.All);
bool inStock = (bool) results[0]["inStock"];
自定义映射
您可以通过实现IReadOnlyMappingManager
接口对自己的映射机制进行编码。