I'm writing a Spring Boot API endpoint and I need to return a list of Airlines ordered by a String (description) of 3 or more chars, the List must be sorted with the results that match the String at the beginning of the word at the top and the rest by alphabetical order
Airline class:
@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "airline")
public class Airline implements Serializable {
private static final long serialVersionUID = -5962418077659300886L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column
private String code;
@Column
private String description;
}
I get the list that matches the String but I cannot get the order right
AirlineRepository
public interface AirlineRepository extends JpaRepository<Airline, Integer> {
Airline getById(Integer id);
List<Airline> findByDescriptionContainingIgnoreCase(String description, Pageable pageable);
List<Airline> findByDescriptionStartsWithIgnoreCase(String description, Pageable pageable);
}
If I search the keyword 'ala' all airlines whose name starts with 'ala' should come first.
This is what I get
{
"status": "SUCCESS",
"message": "Execution Successful",
"totalCount": 10,
"content": [
{
"id": 962,
"code": "6T",
"description": "Air Mandalay"
},
{
"id": 46,
"code": "NZ",
"description": "Air New Zealand"
},
{
"id": 18,
"code": "AS",
"description": "Alaska Airlines"
},
{
"id": 519,
"code": "KO",
"description": "Alaska Central Express"
},
{
"id": 477,
"code": "J5",
"description": "Alaska Seaplane"
},
{
"id": 30,
"code": "YZ",
"description": "ALAS URUGUAY"
},
{
"id": 324,
"code": "C9",
"description": "Alphaland Aviation"
},
{
"id": 286,
"code": "A7",
"description": "Calafia Airlines"
},
{
"id": 330,
"code": "CE",
"description": "Chalair"
},
{
"id": 521,
"code": "L3",
"description": "DHL de Guatemala"
}
],
"timestamp": null
}````
CodePudding user response:
you can use something like :
@Query("select * from Airline a order by substring(a.description, 0,search_text_length)")
List<Airline> findAirline();
You can get an idea here
CodePudding user response:
Passed description
as a sorting coloumn and create pageRequest
as follows. and description parameter and pageRequest send to findByDescriptionStartsWith
method in AirlineRepository
PageRequest pageRequest = PageRequest
.of((int) Math.ceil((float) (start / pageDataSize)), pageDataSize, Sort.by('description').ascending());
List<Airline> findByDescriptionStartsWith(String description, Pageable pageable);
CodePudding user response:
i'm not quite sure if i understood your requirement completly.
To fetch all airlines in an ordered way i would add the following method to the AirlineRepository
List<Airline> findByDescriptionStartsWithIgnoreCaseOrderByDescriptionAsc(String description, Pageable pageable);
If you also need all other airlines i would add this method, too.
List<Airline> findByDescriptionNotLikeIgnoreCaseOrderByDescriptionAsc(String description, Pageable pageable);
Add an '%' to the description and combine both lists.