3

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?

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Slee
  • 27,498
  • 52
  • 145
  • 243

2 Answers2

2

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

Jaider
  • 14,268
  • 5
  • 75
  • 82
0

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.

Tom Robinson
  • 8,348
  • 9
  • 58
  • 102