你可以使用 MongoRepository 来按分数的顺序排序吗?

huangapple go评论57阅读模式
英文:

Can I use MongoRepository to sort in the order of the score?

问题

I'm going to sort the search results in the database in the order of the score.

However, I could not find a way to sort using mongoRepository, so I asked a question.

Is there a way to sort by score using MongoRepository?

This is my Entity:

@Data
@Document(collection = "cad")
public class Cad {
    @Id
    private String id;
    private String author;
    private String mainCategory;
    private String subCategory;
    private String title;
    private String index;
    private String s3Url;
    private String createdAt;

    public Cad(String author, String mainCategory, String subCategory, String title, String index, String s3Url, String createdAt) {
        this.author = author;
        this.mainCategory = mainCategory;
        this.subCategory = subCategory;
        this title = title;
        this.index = index;
        this.s3Url = s3Url;
        this.createdAt = createdAt;
    }
}

And This is my Repository:

public interface CadRepository extends MongoRepository<Cad, String> {
    List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4);
}

And This is my Service:

@RequiredArgsConstructor
@Service
@Component
public class CadService {
    private final CadRepository cadRepository;

    public List<Cad> searchCadFile(String searchText) {
        try {
            List<Cad> list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I tried @Aggregate Annotation, but I didn't get the result I wanted.

Any hint would be appreciated. Thank you 你可以使用 MongoRepository 来按分数的顺序排序吗?

英文:

I'm going to sort the search results in the database in the order of the score.

However, I could not find a way to sort using mongoRepository, so I asked a question.

Is there a way to sort by socre using MongoRepository?

This is my Entity

    @Data
    @Document(collection = &quot;cad&quot;)
    public class Cad {
        @Id
        private String id;
        private String author;
        private String mainCategory;
        private String subCategory;
        private String title;
        private String index;
        private String s3Url;
        private String createdAt;
    
    
        public Cad(String author, String mainCategory, String subCategory, String title, String index, String s3Url, String createdAt) {
            this.author = author;
            this.mainCategory = mainCategory;
            this.subCategory = subCategory;
            this.title = title;
            this.index = index;
            this.s3Url = s3Url;
            this.createdAt = createdAt;
        }
    }

And This is my Repository

    public interface CadRepository extends MongoRepository&lt;Cad, String&gt; {
        List&lt;Cad&gt; findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4);
    }

And This is my Service

@RequiredArgsConstructor
@Service
@Component
public class CadService {
    private final CadRepository cadRepository;

    public List&lt;Cad&gt; searchCadFile(String searchText) {
        try {
            List&lt;Cad&gt; list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I tried @Aggregate Annotation, but I didn't get the result I wanted.

Any hint would be appreciated. Thank you 你可以使用 MongoRepository 来按分数的顺序排序吗?

答案1

得分: 1

你可以在你的存储库方法中添加一个 org.springframework.data.domain.Sort 参数:

List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4, Sort sort);

当调用你的存储库方法时,你可以添加要排序的属性:

List<Cad> list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText, Sort.by("index"));

或者,你可以将属性添加到方法名称中:

List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContainsOrderByIndexAsc(String text1, String text2, String text3, String text4);

更详细的解释可以在这里找到:https://www.baeldung.com/spring-data-sorting

英文:

You can add a org.springframework.data.domain.Sort parameter to your repository method:

List&lt;Cad&gt; findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4, Sort sort);

And when calling your repository method you can add the property / properties you want to sort on:

List&lt;Cad&gt; list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText, Sort.by(&quot;index&quot;));

Or you can add the property to the method name:

List&lt;Cad&gt; findAllByTitleOrIndexOrMainCategoryOrSubCategoryContainsOrderByIndexAsc(String text1, String text2, String text3, String text4);

Things are more explained in detail here: https://www.baeldung.com/spring-data-sorting

huangapple
  • 本文由 发表于 2023年2月13日 23:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定