Add Sort in Liferay search

Gohel Manoj, modified 6 Years ago. New Member Posts: 18 Join Date: 1/29/16 Recent Posts
Hello All,
I want to add Sort functionality into Default Liferay search can any one suggest me how i can achieve this.?(same as like Let the User Choose scope is given into configuration).

I already use SearchBar fragment and SearchResult fragment, In SearchBar fragment add two select box one is  orderColumn and another is for orderType(asc, desc).

Liferay liferay-dxp-7.1.10.2-sp2

Elasticsearch 6.50.

Appreciate your help!
thumbnail
Russell Bohl, modified 6 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts
First suggestion: move to 7.2, where sorting results is a built-in capability: https://portal.liferay.dev/docs/7-2/user/-/knowledge_base/u/sorting-search-results-with-the-sort-widget
On 7.1, this appears to be the general pattern fo building a sort and adding it to the search context:
SortFactory sortFactory = new SortFactoryImpl();
Sort sort = sortFactory.create(String fieldName, int type, boolean reverse);
searchContext.setSorts(sort));

If that doesn't push you toward a solution, you might have to be more specific with your question. I don't know if I can help you, but it makes it easier for a real expert to help if it's clear what you're trying to do, what you've tried, and how it failed.
Good luck!
Gohel Manoj, modified 6 Years ago. New Member Posts: 18 Join Date: 1/29/16 Recent Posts
Thanks for your help, but it is still not working.

Same as like 7.2 i want to implement into 7.1,  i tried many time but still not working.
I write one IndexerPostProcessor and below is a code snipest.    
@Override
    public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter booleanFilter,
            SearchContext searchContext) throws Exception {
        searchContext.setSorts(new Sort(Field.PUBLISH_DATE, Sort.SCORE_TYPE, false));
    }
Samuel Colin, modified 4 Years ago. New Member Posts: 3 Join Date: 3/15/21 Recent Posts

Hy ,

Don't forget to add your sorted column on this property (portal-ext.properties) : 

index.sortable.text.fields=...