Configuring Routing in ElasticSearch Using NEST and Attribute Mapping
I am trying to customize my mapping using Attribute Based Display
I need to set up routing, make it mandatory, and set the object that I am indexing to a specific object.
Is it possible? Has anyone done this?
+3
source to share
2 answers
You can use IdProperty
attribute property ElasticType
:
[ElasticType(Name = "mydocument", IdProperty = "docDate")]
public class MyDocument
{
[ElasticProperty(Name = docDate)]
public DateTime DocDate { get; set; }
...
This sets the value stored in a field _id
in elasticsearch that is used for routing .
0
source to share
First you need to do the routing needed when creating the index, for example:
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 indexing your document, for example:
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 provide a routing value when you search.
client.Search<MyObject>(query => query.Query(q => q.MatchAll()).Routing(routingValue));
Using NEST v2.4
+1
source to share