# Sorting for date field

**URL:** <https://discuss.elastic.co/t/sorting-for-date-field/226205>\
**Category:** Elasticsearch\
**Created:** [April 2, 2020, 11:51am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205 "2020-04-02T11:51:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)\
**Post date:** [April 2, 2020, 11:51am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/1 "2020-04-02T11:51:41Z")

</div>

Hello,

I am using last\_modified\_date field from my index.  
I am sorting my results using this same field.  
field data is enabled on this column. but i am not getting correct results for sorting.  
I want to convert this field to date. so that sorting results can be perfect. How i can achieve this?

Regards,  
Priyanka

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2020, 12:55pm UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/2 "2020-04-02T12:55:35Z")

</div>

What is the mapping?  
What does a document look like?  
What is your query?

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)\
**Post date:** [April 3, 2020, 7:02am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/3 "2020-04-03T07:02:13Z")

</div>

Hello @dadoonet,

My query is as follows:

```
GET /master_db/_search
{ 
  "from":"1", "size":"10",
  "aggs":{ 
    "content_type":
    { 
      "terms":
      { 
        "field":"content_type.keyword", 
        "size":50 
        } 
      
    }, 
    "content_subtype":
    { 
      "terms":
      { 
        "field":"content_subtype.keyword", 
        "size":100
        } 
      
    }, 
    "helpdeskname":
    { 
      "terms":
      { 
        "field":"helpdeskname.keyword", 
      "size":20 
      } 
    }, 
    "keywords":
    {
      "terms":
      { 
        "field":"keywords",
        "size":10
        } 
    }, 
    "segmentname":
    { 
      "terms":
      { 
        "field":"segmentname.keyword", 
        "size":10 
      } 
	  }, 
	  "category":
	  { 
	  "terms":
	  { 
	  "field":"category_paths.keyword", 
	  "size":10 
	  } 
	  }, 
	  "template_field":
	  { 
	  "terms":
	  { "field":"template_field.keyword", 
	  "size":100 
	  } 
	  }, 
	  "relatedto":
	  { "terms":
	  { "field":"relatedto.keyword", 
	  "size":100 
	  } 
	  }, 
	  "scope":
	  { "terms":
	  { "field":"scope.keyword", 
	  "size":10
	  } 
	  }, 
	  "lastmodifiedyear":
	  { "terms":
	  { "field":"lastmodified_year",
	  "size":20, 
	  "order": {"_key":"asc"} 
	  } 
	  }, 
	  "submittedyear":
	  { "terms":
	  { "field":"submitted_year", 
	  "size":30, 
	  "order":{"_key":"asc"} 
	  } 
	  } 
	  },
	  "query":
	  { "bool":
	  { "must":
	  [ { "terms":
	  { "event":["update","new"] 
	  }
	  }, 
	  { "terms":
	  { "business_line":["3"] 
	  } 
	  }, 
	  { "terms":
	  { "status":["12"] 
	  } 
	  } ,
	  { "multi_match":
	  { "query":"mrpo", 
	  "fields":["contentid","title","object_summary","keywords","content_type","content_subtype","ref_page_data","primary_mnemonic","secondary_mnemonic","quest_ids","template_field","submitted_for_name","content_authors","action_by","category_paths"] } } ,{ "terms":{ "segmentid":["18252"] 
	  } 
	  } 
	  ] ,
	  "must_not":
	  { "match":
	  { "scope":"Local" } 
	  } 
	  } 
	  } ,
	  "sort":
	  { 
	  "last_modified_date":{ "order":"asc" } 
	  } 
	  }

```

And last\_modified\_date is having as string date type in my index.  
So sorting is getting affected in this case. It is not sorting on the basis of date.  
Now i want this string to be converted into date.  
How i can achieve this?

Regards,  
Priyanka

---

<div class="post-metadata">

**Author:** ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)\
**Post date:** [April 3, 2020, 7:07am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/4 "2020-04-03T07:07:21Z")

</div>

Hello,

Mapping of my index:

```
{
  "master_db" : {
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "access_groups" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "act_helpdeskid" : {
          "type" : "long"
        },
        "assigned_individual" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "business_line" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "category" : {
          "type" : "text",
          "fielddata" : true
        },
        "category_paths" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "content_authors" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "content_subtype" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "content_subtype_id" : {
          "type" : "long"
        },
        "content_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "content_type_boost" : {
          "type" : "long"
        },
        "contentid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "deny_groups" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "event" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "helpdeskid" : {
          "type" : "long"
        },
        "helpdeskname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "ind_outstanding" : {
          "type" : "long"
        },
        "ind_publicuser" : {
          "type" : "long"
        },
        "ind_whatsnew" : {
          "type" : "long"
        },
        "keywords" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "last_modified_date" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastdatevalid_month" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastmodified_day" : {
          "type" : "long"
        },
        "lastmodified_month" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastmodified_year" : {
          "type" : "long"
        },
        "lastmodifieddatevalid_day" : {
          "type" : "long"
        },
        "lastmodifieddatevalid_year" : {
          "type" : "long"
        },
        "media_type_id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "num_file_attachments" : {
          "type" : "long"
        },
        "object_summary" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "page_views" : {
          "type" : "long"
        },
        "parent_helpdesk_id" : {
          "type" : "long"
        },
        "primary_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "primary_mnemonic" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "quest_ids" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "ref_page" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "ref_page_data" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "relatedtechalertcount" : {
          "type" : "long"
        },
        "relatedto" : {
          "type" : "long"
        },
        "scope" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "secondary_mnemonic" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "segmentid" : {
          "type" : "long"
        },
        "segmentname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "sophia_restricted" : {
          "type" : "long"
        },
        "status" : {
          "type" : "long"
        },
        "status_label" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_date" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_day" : {
          "type" : "long"
        },
        "submitted_for_area" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_for_country" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_for_geomarket" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_for_ldap" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_for_location" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_for_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_month" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "submitted_year" : {
          "type" : "long"
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "template_field" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "typeid" : {
          "type" : "long"
        },
        "version" : {
          "type" : "long"
        },
        "votesignal" : {
          "type" : "long"
        }
      }
    }
  }
}

```

Regards,  
Priyanka

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 3, 2020, 10:26am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/5 "2020-04-03T10:26:55Z")

</div>

In your mapping:

```
    "last_modified_date" : {
      "type" : "text",

```

This is a `text` not a `date`.  
You should make this field a `date` field then you'll be able to properly sort using this field.

---

<div class="post-metadata">

**Author:** ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)\
**Post date:** [April 3, 2020, 10:51am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/6 "2020-04-03T10:51:12Z")

</div>

Hello @dadoonet,

Sorting is showing wrong values in my case for this text field.  
So i wanted to convert this into Date.

Regards,  
Priyanka

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 3, 2020, 11:41am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/7 "2020-04-03T11:41:47Z")

</div>

Yes. That's what I said as well.

---

<div class="post-metadata">

**Author:** ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)\
**Post date:** [April 3, 2020, 11:42am UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/8 "2020-04-03T11:42:33Z")

</div>

Yes but how?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 3, 2020, 3:14pm UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/9 "2020-04-03T15:14:37Z")

</div>

By creating a proper mapping using a date data type for your field.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 1, 2020, 3:14pm UTC](https://discuss.elastic.co/t/sorting-for-date-field/226205/10 "2020-05-01T15:14:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
