Java Spring Mongo排序忽略案例问题 [英] Java Spring Mongo Sort Ignore Case issue

查看:258
本文介绍了Java Spring Mongo排序忽略案例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring-Data-mongodb对MongoDB执行各种请求.

I am using Spring-Data-mongodb performing all kind of requests to MongoDB.

尝试执行分页&时用忽略的情况排序我得到一个例外,

When trying to perform paging & sort with ignore case I get an exception,

这是我的代码:

Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey).ignoreCase();
    Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
    return mongoTemplate.find(query, clazz,collection);

注意在Sort.Order对象上应用的.IgnoreCase()方法.

notice the .IgnoreCase() method applied on the Sort.Order Object.

Query .with方法失败并引发异常:

The Query .with Method fails and throws the exception:

java.lang.IllegalArgumentException:给定的排序包含带有忽略大小写的lastName的订单! MongoDB当前不支持排序忽略大小写! 在org.springframework.data.mongodb.core.query.Query.with(Query.java:179) 在org.springframework.data.mongodb.core.query.Query.with(Query.java:162)

java.lang.IllegalArgumentException: Given sort contained an Order for lastName with ignore case! MongoDB does not support sorting ignoreing case currently! at org.springframework.data.mongodb.core.query.Query.with(Query.java:179) at org.springframework.data.mongodb.core.query.Query.with(Query.java:162)

如果删除.IgnoreCase()方法,即执行以下代码:

If removing the .IgnoreCase() method, i.e executing the following code:

Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey);
Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
return mongoTemplate.find(query, clazz,collection);

一切正常,除了我不会得到不敏感的排序结果.

Everything works fine, except of course I don't get insensitive sorted results.

因此,我可能会得到A B C a1 a2而不是A a1 a2 B C.

hence I might get A B C a1 a2, instead of A a1 a2 B C.

即使该异常提到mongoDB不支持IgnoreCase排序,但我使用的是mongo 3.4,据我所知,它确实支持对页面分页排序的ignoreCase选项(

Even though the exception mentions that mongoDB doesn't support IgnoreCase sort, I am using mongo 3.4 which in my understanding does support ignoreCase option for pageable sort (Here's the official JIRA issue regarding insensitive search feature added), and my spring-data-mongodb package is 1.8.

推荐答案

对不起,如果我的评论不清楚.您必须使用排序查询发送归类.

Sorry if my comments were not clear. You've to send collation with sort query.

主要和次要强度都将提供不区分大小写的排序.确保在排序查询中使用确切的排序规则条件以利用索引.

Strength primary and secondary both will provide case insensitive sort. Make sure you use the exact collation criteria in your sort query to take advantage of index.

Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey);
Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
query.collation(Collation.of("en").strength(Collation.ComparisonLevel.secondary()));
return mongoTemplate.find(query, clazz,collection);

这篇关于Java Spring Mongo排序忽略案例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆