user2439903
user2439903

Reputation: 1307

Nest and elastic search- update mapping for nested object?

I am trying to add a new property to an existing nested document. My document looks like:

"mappings": {
  "test": {
    "_routing": {
      "required": true,
      "path": "tId"
    },
    "properties": {

      "series": {
        "type": "nested",
        "properties": {

          "iType": {
            "type": "string",
            "index": "not_analyzed",
            "doc_values": true
          },
          "isValid": {
            "type": "boolean"
          },

        },
      },
    }
  }

The property i want to insert to nested document "series" is "iType". How can i use the NEST put mapping API to update the existing mapping?

Any help will be appreciated.

Thanks in advance.

#####*****UPDATED*****##########

i would need to update the mapping for nested element with attributes :

"iType": {
  "type": "string",
  "fields": {
    "raw": {
      "type": "string",
      "index": "not_analyzed",
      "doc_values": true,
      "fielddata": {
        "loading": "eager_global_ordinals"
      }
    }
  }
},

How can i do this with NEST ?

My query looks like:

var response2 = elasticClient.Map < Test > (e => e
  .Properties(props => props
    .NestedObject < series > (s => s
      .Properties(sprops => sprops
        .String(n => n.Name(name => "iType"))))));

i get an exception: Could not get field name for nested sobject mapping Any corrections to be made in query?

Upvotes: 2

Views: 7064

Answers (1)

Rob
Rob

Reputation: 9979

You can update mapping for nested object by

var response = client.Map<YourType>(m => m
    .Properties(p => p
        .NestedObject<YourNestedType>(n => n
            .Name(name => name.NestedObject)
            .Properties(pp => pp
                .String(s => s.Name(name => name.NewProp))
            ))));

update

This is how you can update your index mapping with multi fields:

var response = client.Map<Test>(m => m
    .Properties(p => p
        .NestedObject<Series>(nested => nested
            .Name(name => name.Series)
            .Properties(pp => pp
                .MultiField(mf => mf
                    .Name(name => name.iType)
                    .Fields(f => f 
                        .String(s => s.Name(n => n.iType))
                        .String(s => s
                            .Name(n => n.iType.Suffix("raw"))
                            .Index(FieldIndexOption.NotAnalyzed)
                            .DocValues()
                            .FieldData(fd => fd
                                .Loading(FieldDataLoading.EagerGlobalOrdinals))))))
            ))); 

You got the exception, because you didn't specify name for nested object, take a look one more time at my mapping definition: enter image description here

Hope it helps.

Upvotes: 4

Related Questions