Sorting for date field

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

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. 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.

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

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

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.

Hello @dadoonet,

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

Regards,
Priyanka

Yes. That's what I said as well.

Yes but how?

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

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