Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElasticSearch Sort not active by default #26712

Open
1 task done
swilimo opened this issue Jul 13, 2024 · 6 comments
Open
1 task done

ElasticSearch Sort not active by default #26712

swilimo opened this issue Jul 13, 2024 · 6 comments

Comments

@swilimo
Copy link

swilimo commented Jul 13, 2024

Overview of the feature request

On latest versions of Jhipster, a lot of code was added to prevent sorting while performing search in ElasticSearch.

This use case was motivated by the fact that "all shards fail" when you try to sort on anything else than a keyword. But this is not intuitive, as most of business cases need a sort on elasticsearch, especially when it is used for listing entities.

Tons of code was added to html / Typescript to delete the sort, while it is not the common use case. It's not dirty in the sense of "clean" code but in term of logic, it has no sense.

The solution would be to add a keyvword as inner field on each one, this works for any type of fields.
For instance :

@MultiField( mainField = @Field(type = FieldType.Text), otherFields = { @InnerField(suffix = "verbatim", type = FieldType.Keyword) } ) private String lastName;

this way we just add on HTML :

<th scope="col" SortBy="lastName.verbatim">

And the sort works

Motivation for or Use Case

Most of business cases need a sort on elasticsearch, especially when it is used for listing entities.

Related issues or PR
  • Checking this box is mandatory (this is just to show you read everything)
@OmarHawk
Copy link
Contributor

Newer elasticsearch versions do generate a keyword field automatically. For my own application, I implemented a Blueprint that enhances the application by allowing to sort on more fields than the default allows by automatically rewriting the Pageable to use that autogenerated .keyword field for sorting for additonal fields instead. ;-)

Something like this:

    /**
     * Creates a new Pageable with the sorting support improved.
     *
     * @param originalPageable
     * @param rootClass
     * @return
     */
    private static Pageable getSortingSupportImprovedPageable(Pageable originalPageable, Class<?> rootClass) {
        List<Sort.Order> orders = new ArrayList<>();
        for (Sort.Order order : originalPageable.getSort()) {
            String property = order.getProperty();
            if (supportsKeywordSearch(property, rootClass)) {
                property += ".keyword";
            }

            orders.add(new Sort.Order(order.getDirection(), property));
        }

        return PageRequest.of(originalPageable.getPageNumber(), originalPageable.getPageSize(), Sort.by(orders));
    }

and the supportsKeywordSearch method allows all String and enum fields, type check using reflection based on the given fieldname (property) and the rootClass.

@swilimo
Copy link
Author

swilimo commented Jul 17, 2024

Thanks for your answer, that's what i'm saying basically, weather it's your solution to have a method for that, or just by adding innerfield on each property or if a keyword is generated automatically, just replace the sort on html by "name.keyword"

But putting that much code on client side to prevent elastic from sorting while searching has no sense for me at least

@OmarHawk
Copy link
Contributor

Thanks for your answer, that's what i'm saying basically, weather it's your solution to have a method for that, or just by adding innerfield on each property or if a keyword is generated automatically, just replace the sort on html by "name.keyword"

But putting that much code on client side to prevent elastic from sorting while searching has no sense for me at least

The idea of my post was to give an impression what could be a solution for the generator itself with the stuff that is already there. ;-)

@swilimo
Copy link
Author

swilimo commented Jul 18, 2024

The idea of my post was to give an impression what could be a solution for the generator itself with the stuff that is already there. ;-)

Thanks Omar, i have a question though, couldn't find the answer.

Are there types that you cant sort with your solution ? or either it's sortable or it has automatically generated keyword field ?

When you say newer elasticsearch versions, it is starting 8 ?

I'm asking those questions, because i guess if this is implemented, we need to make sure that it resolves the problem once and for all, either by applying your solution or the innerfield solution. I know the innerfield solution would work with any elasticsearch version (well at least from 6).

Thanks :)

@OmarHawk
Copy link
Contributor

Yes, ES8 works definetely.

Additionally sortable are String and enum types using the .keyword. UUID produces also a String in ES, but that doesn't make sense to sort for...

@mraible
Copy link
Contributor

mraible commented Sep 13, 2024

If y'all have a solution, please submit a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants