I am trying to setup my mapping using Attribute based mapping
I need to set routing, making it required and set to a particular property on my object that I am indexing.
Is this possible? Has anyone accomplished this?
I am trying to setup my mapping using Attribute based mapping
I need to set routing, making it required and set to a particular property on my object that I am indexing.
Is this possible? Has anyone accomplished this?
First, you need to make the routing required when you are creating your index, like this:
client.CreateIndex("my-index",
d => d
.Mappings(mapping => mapping
.Map<MyObject>(map => map
.RoutingField(routing => routing
.Required(true))
.AutoMap()
)
));
Second, you need to add the routing value when you index your document, like this:
var result = client.Index<MyObject>(
myObject,
selector => selector
.Id(myObject.ObjectId)/*to avoid "random" ids*/
.Routing(routingValue)); //or in your case, myObject.MySpecialProperty
Finally, you need to specify the routing value(s), when you are doing a search.
client.Search<MyObject>(query => query.Query(q => q.MatchAll()).Routing(routingValue));
Using NEST v2.4
You can use the IdProperty
property of the ElasticType
attribute:
[ElasticType(Name = "mydocument", IdProperty = "docDate")]
public class MyDocument
{
[ElasticProperty(Name = docDate)]
public DateTime DocDate { get; set; }
...
This sets the value stored against the _id
field in elasticsearch, which is used for routing.