答案 0 :(得分:1)
首先,您需要在创建索引时进行所需的路由,如下所示:
client.CreateIndex("my-index",
d => d
.Mappings(mapping => mapping
.Map<MyObject>(map => map
.RoutingField(routing => routing
.Required(true))
.AutoMap()
)
));
其次,您需要在索引文档时添加路由值,如下所示:
var result = client.Index<MyObject>(
myObject,
selector => selector
.Id(myObject.ObjectId)/*to avoid "random" ids*/
.Routing(routingValue)); //or in your case, myObject.MySpecialProperty
最后,您需要在进行搜索时指定路由值。
client.Search<MyObject>(query => query.Query(q => q.MatchAll()).Routing(routingValue));
使用NEST v2.4
答案 1 :(得分:0)
您可以使用IdProperty
属性的ElasticType
属性:
[ElasticType(Name = "mydocument", IdProperty = "docDate")]
public class MyDocument
{
[ElasticProperty(Name = docDate)]
public DateTime DocDate { get; set; }
...
这会设置存储在elasticsearch中的_id
字段的值,该字段用于routing。