# Sorting not working when update entity

**URL:** <https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429>\
**Category:** Hibernate Search\
**Created:** [June 14, 2021, 8:08am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429 "2021-06-14T08:08:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinod\_Jat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vinod_jat/32/1283_2.png) [@Vinod\_Jat](https://discourse.hibernate.org/u/Vinod_Jat)\
**Post date:** [June 14, 2021, 8:08am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/1 "2021-06-14T08:08:23Z")

</div>

Hi,  
I am using hibernate search 5.11 , i have added base entity below  
@CreationTimestamp  
@Column(updatable = false, name = “created\_date”)  
@Field(analyze = Analyze.NO,store = Store.NO)  
@SortableField  
private LocalDateTime createdDate;

and for sort  
sort = queryBuilder  
.sort()  
.byField(“createdDate”).desc() // Descending order  
.createSort();  
Sorting is working when I add new item but when I update this item sorting not working.  
Note – Other entity extends this base entity.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [June 14, 2021, 8:12am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/2 "2021-06-14T08:12:23Z")

</div>

Hello.

We will need more information.

- Be more specific about what you mean by “not working”: show some example data with the expected order vs. the actual order.
- Be more specific about what you do when you “update items”: show the code that performs the actual update, in particular the properties that are modified.
- Be more specific about the type involved; I understand there are two? Are both those types annotated with `@Indexed`?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [June 14, 2021, 8:14am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/3 "2021-06-14T08:14:07Z")

</div>

… Also, this is a **creation** date, not a “last update” date, and the column in question is marked as “not updateable”, so I’m not sure what you mean by “when I update this item”. You probably confused this property with another one?

---

<div class="post-metadata">

**Author:** ![Vinod\_Jat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vinod_jat/32/1283_2.png) [@Vinod\_Jat](https://discourse.hibernate.org/u/Vinod_Jat)\
**Post date:** [June 14, 2021, 8:22am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/4 "2021-06-14T08:22:21Z")

</div>

Hi

1. Order should be in descending but i got  
{  
“id”:“3”,  
“createdDate”:“2021-06-13 13:24:56”  
} ,  
{  
“id”:“2”,  
“createdDate”:“2021-06-12 13:24:56”  
} ,  
{  
“id”:“5”,  
“createdDate”:“2021-06-14 13:24:56” // this item details updated (createdDate not update)  
}

2. I update title for particular item (createdDate not update)

3 Yes both type are annotated with `@Indexed`

I am using base entity and extends this to other entity.  
when I update I simply get information from db by id and update the title and then persist it.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [June 14, 2021, 8:38am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/5 "2021-06-14T08:38:57Z")

</div>

Okay, I don’t know why you keep mentioning updates and why you keep not showing the code, but regardless the order seems wrong.

After you create the sort, do you [add it to the query](https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#elasticsearch-query-sorting) with `query.setSort(sort);`?

If so, and if it still doesn’t work, please [enable logging of Elasticsearch requests](https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#elasticsearch-logging) by setting the log level of category `org.hibernate.search.elasticsearch.request` to `TRACE`, then paste the log output when executing your search query here.

Please format this properly with ``` before and after the logs, like this:

````auto
```
...my logs...
...some more logs...
```

````

---

<div class="post-metadata">

**Author:** ![Vinod\_Jat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vinod_jat/32/1283_2.png) [@Vinod\_Jat](https://discourse.hibernate.org/u/Vinod_Jat)\
**Post date:** [June 14, 2021, 9:28am UTC](https://discourse.hibernate.org/t/sorting-not-working-when-update-entity/5429/7 "2021-06-14T09:28:20Z")

</div>

Please find details

@indexed(name=“Item”)  
public class Item extends BaseEntity{

```
@Id
Long id

@Field
private String title;

```

}

@indexed(name=“BaseEntity”)  
public class BaseEntity{

@CreationTimestamp  
@Column(updatable = false, name = “created\_date”)  
@Field(analyze = Analyze.NO,store = Store.NO)  
@SortableField  
private LocalDateTime createdDate;  
}

public Item updateItem(Long id,String title){

try{  
Optional item = itemRepository.findById(id);  
if(item.isPresent()) {  
item.get().set(title);  
itemRepository.save(item.get())  
return item;  
}  
else{  
throw new CustomException(“Item with id “+id+” not found”,HttpStatus.NOT\_FOUND);  
}  
}  
catch(Exection e){  
log.error(“Error::{}”,e.getMessage());  
throw new CustomException(“Something went wrong”,HttpStatus.INTERNAL\_SERVER\_ERROR,e.getMessage());  
}

}

I will try to reproduce logs
