title: Dynamic mapping and nested sorting on dates in ElasticSearch/Java
body: <p>I am new to Elastic Search. I am using Java, Elastic Search and Spring. My scenario is that I have a jQuery Table on a page. This table is a list of all the Users. Associated with users is a <code>Set</code> of extra information, we'll call this <code>extraDataValues</code>. This could be anything, the most important thing is that the value stored is a <code>String</code> value, which could be a formatted <code>Date</code> or <code>DateTime</code> String value. Here it is in JSON:</p>
  
  <pre><code>"extraDataValues": [
      {
          "id": 3,
          "extraDataValueObject": {
              "id": 12,
              "label": "Metadata Date",
              "displayable": true
          },
          "value": "01/01/2015 00:01:11"
      },
      {
          "id": 4,
          "extraDataValueObject": {
              "id": 13,
              "label": "Metadata TextBox",
              "displayable": true
          },
          "value": "zzzz"
      }
  ],
  </code></pre>
  
  <p>This brings me to the problem. Given that I have a nested <code>extraDataValues</code>set in Users with <code>extraDataObjects</code>, how can I:</p>
  
  <ul>
  <li>Perform a sort on a specific extraDataObject with either ID 12 or 13 that resides in extraDataValues. Basically, in Java I would get the <code>Set</code> of <code>extraDataValues</code> and within there I would search for all the <code>extraDataValueObjects</code> with ID 12, then I would see if that is a Date Time and sort it as a Date Time</li>
  <li>Perform a sort on a String value if it is a Date or Date Time?</li>
  <li>how can I dynamically change the mapping during runtime so I can perform such a dynamic sorting</li>
  <li>How can I do this in plain Elastic Search and how can I translate this to Java?</li>
  </ul>
  
  <p>I can figure whether the value is going to be a <code>DateTime</code> or a `String' in Java but I have no idea how to translate this to Elastic Search in Java. </p>
  
  <p>User object return from Elastic Search after a simple query:</p>
  
  <pre><code>User:
  {
      "id": 1,
       "extraDataValues": [
          {
              "id": 1,
              "extraDataValueObject": {
                  "id": 10,
                  "label": "Metadata Date",
                  "displayable": true
              },
              "value": "01/01/2015 00:00:00"
          },
          {
              "id": 2,
              "extraDataValueObject": {
                  "id": 11,
                  "label": "Metadata TextBox",
                  "displayable": true
              },
              "value": "aaaa"
          }
      ],
      "username": "johnDoe",
      "firstName": "John",
      "surname": "Doe",
      "email": "john@doe.com",
      "fullName": "John Doe"
  
      "type": {
          "id": 2,
          "name": "Blogger",
          "active": true,
      },
      "club": {
          "id": 2,
          "name": "Photography",
      }
  },
  {
      "id": 2,
       "extraDataValues": [
          {
              "id": 3,
              "extraDataValueObject": {
                  "id": 10,
                  "label": "Metadata Date",
                  "displayable": true
              },
              "value": "01/01/2015 00:01:11"
          },
          {
              "id": 4,
              "extraDataValueObject": {
                  "id": 11,
                  "label": "Metadata TextBox",
                  "displayable": true
              },
              "value": "zzzz"
          }
      ],
      "username": "marySmith",
      "firstName": "Mary",
      "surname": "Mary",
      "email": "mary@smith.com",
      "fullName": "Mary Smith"
  
      "type": {
          "id": 2,
          "name": "Moderator",
          "active": true,
      },
      "club": {
          "id": 2,
          "name": "Yoga",
      }
  },
  {
      "id": 3,
      "extraDataValues": [
          {
              "id": 5,
              "extraDataValueObject": {
                  "id": 10,
                  "label": "Metadata Date",
                  "displayable": true
              },
              "value": "02/02/2015 00:01:11"
          },
          {
              "id": 6,
              "extraDataValueObject": {
                  "id": 11,
                  "label": "Metadata TextBox",
                  "displayable": true
              },
              "value": "bbbb"
          }
      ],
      "username": "joeBloggs",
      "firstName": "Joe",
      "surname": "Bloggs",
      "email": "joe@bloggs.com",
      "fullName": "Joe Bloggs"
  
      "type": {
          "id": 3,
          "name": "Admin",
          "active": true,
      },
      "club": {
          "id": 3,
          "name": "Cycling",
      }
  }
  </code></pre>
  
  <p>User Mapping Document:</p>
  
  <pre><code>    {
      "User": {
         "properties": {
              "type": {
                  "properties": {
                      "id": {
                          "type": "long"
                      },
                      "name": {
                          "type": "string",
                          "index":    "not_analyzed",
                          "fields": {
                              "raw_lower_case": { 
                                  "type":  "string",
                                  "analyzer": "case_insensitive"
                              }
                          }
                      }
                  }
              },
              "fullName": {
                  "type": "string",
                  "index":    "not_analyzed",
                  "fields": {
                      "raw_lower_case": { 
                          "type":  "string",
                          "analyzer": "case_insensitive"
                      }
                  }
              },              
              "username": {
                  "type": "string",
                  "index":    "not_analyzed",
                  "fields": {
                      "raw_lower_case": { 
                          "type":  "string",
                          "analyzer": "case_insensitive"
                      }
                  }
              },
              "email": {
                  "type": "string",
                  "index":    "not_analyzed",
                  "fields": {
                      "raw_lower_case": { 
                          "type":  "string",
                          "analyzer": "case_insensitive"
                      }
                  }
              },
              "firstName": {
                  "type": "string",
                  "index":    "not_analyzed",
                  "fields": {
                      "raw_lower_case": { 
                          "type":  "string",
                          "analyzer": "case_insensitive"
                      }
                  }
              },
              "surname": {
                  "type": "string",
                  "index":    "not_analyzed",
                  "fields": {
                      "raw_lower_case": { 
                          "type":  "string",
                          "analyzer": "case_insensitive"
                      }
                  }
              },
              "id": {
                  "type": "long"
              },
              "metadataFieldValues": {
                  "type": "nested",
                  "properties": {
                      "metadataFieldDefinition": {
                          "properties": {
                              "id": {
                                  "type": "long"
                              },
                              "label": {
                                  "type": "string"
                              },
                              "displayable": {
                                  "type": "boolean"
                              }
                          }
                      },
                      "value": {
                          "type": "string",
                          "index":    "not_analyzed",
                          "fields": {
                              "raw_lower_case": { 
                                  "type":  "string",
                                  "analyzer": "case_insensitive"
                              }
                          }
                      }
                  }
              },
              "club": {
                  "properties": {
                      "id": {
                          "type": "long"
                      },
                      "name": {
                          "type": "string",
                          "index":    "not_analyzed",
                          "fields": {
                              "raw_lower_case": { 
                                  "type":  "string",
                                  "analyzer": "case_insensitive"
                              }
                          }
                      }
                  }
              },      
          }
      }
  }
  </code></pre>
  
  <p>EDIT:::</p>
  
  <p>I took a look at these questions:
  - <a href="https://stackoverflow.com/questions/18405195/elasticseach-sorting-on-dates">ElasticSeach - Sorting on dates</a>
  - <a href="https://stackoverflow.com/questions/10415494/elasticsearch-sort-by-single-nested-document-key-in-array">Elasticsearch sort by single nested document key in array</a></p>
  
  <p>Am I heading in the right direction?</p>