@Entity
@Table(name = "profiles")
public class Profile extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "profile_given_name")
private String givenName;
@Column(name = "profile_surname")
private String surname;
@Column(name = "profile_is_male")
private Integer isMale;
@Column(name = "profile_height_meters", columnDefinition = "DOUBLE")
private BigDecimal heightMeters;
@Column(name = "profile_weight_kilos", columnDefinition = "DOUBLE")
private BigDecimal weightKilos;
@Column(name = "profile_dob")
private Date dob;
// Getters, setters & ctor down here
}
# Search for women between 1.2 and 1.8 meters tall.
GET /v1/profiles?isMale=0&heightMeters={"gt": 1.2, "lt": 1.8}
# Search for men born after Jan 1, 1990 who weigh less than 100 kg.
GET /v1/profiles?isMale=1&dob={"gt" : "1990-01-01 00:00:00"}&weightKilos={"lt": 100.0}
@RestController
@RequestMapping("/v1/profiles")
public class ProfileResource {
@Autowired
ProfileRepository profileRepository;
@GetMapping
public ResponseEntity<Set<Profile>> searchProfiles(@RequestParam(value = "isMale", required = false) String isMaleVal,
@RequestParam(value = "heightMeters", required = false) String heightMetersVal,
@RequestParam(value = "weightKilos", required = false) String weightKilosVal,
@RequestParam(value = "dob", required = false) String dobVal) {
Integer isMaleVal;
BooleanCriteria isMaleCriteria;
if(isMaleVal != null) {
// Parse the value which could either be "0" for female, "1" for male or something like
// ?isMale={0,1} to indicate
// BooleanCriteria would store which values male, female or both) to include in the search
}
BigDecimal heighMeters;
BigDecimalCriteria heightCriteria;
if(heightMetersVal != null) {
// Parse the value which like in the examples could be something like:
// ?heightMeters={"gt" : "1.0"}
// BigDecimalCriteria stores range information
}
BigDecimal heighMeters;
BigDecimalCriteria weightCriteria;
if(weightKilosVal != null) {
// Parse the value which like in the examples could be something like:
// ?weightKilos={"eq" : "100.5"}
// BigDecimalCriteria stores range information
}
// Ditto for DOB and DateCriteria
// TODO: How to pack all of these "criteria" POJOs into a
// CrudRepository/JPQL query against the "profiles" table?
Set<Profile> profiles = profileRepository.searchProfiles(
isMaleCriteria, heightCriteria, weightCriteria, dobCriteria);
}
}
// Basically it just stores the (validated) search criteria that comes in over the wire
// on the controller method
public class BigDecimalCriteria {
private BigDecimal lowerBound;
private Boolean lowerBoundInclusive;
private BigDecimal upperBound;
private Boolean upperBoundInclusive;
// Getters, setters, ctors, etc.
}
由于所有这些搜索条件都是可选的(因此可以为null
),所以我只能讨论如何在ProfileRepository
中编写JPQL查询:
public interface ProfileRepository extends CrudRepository<Profile,Long> {
@Query("???")
public Set<Profile> searchProfiles();
}
如何实现ProfileRepository#SearchProfiles
的@query(...)
,使其启用我的所有搜索条件(给定要搜索的所有允许范围和条件值),并允许任何条件为空/可选?
当然,如果有什么漂亮的小库或者Spring Boot/JPA已经有了解决方案,我会洗耳恭听的!
您可以通过spring数据中的JPaspecificationExecutor
实现具有规范的复杂查询。存储库接口必须扩展JPaspecificationExecutor
接口,以便我们可以通过创建新的Specification
对象来指定数据库查询的条件。
技巧在于结合JPaspecificationExecutor
使用规范接口。下面是示例:
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "city")
private String city;
@Column(name = "age")
private Integer age;
....
}
然后定义存储库:
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {
}
public class PersonSpecification implements Specification<Person> {
private Person filter;
public PersonSpecification(Person filter) {
super();
this.filter = filter;
}
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq,
CriteriaBuilder cb) {
Predicate p = cb.disjunction();
if (filter.getName() != null) {
p.getExpressions()
.add(cb.equal(root.get("name"), filter.getName()));
}
if (filter.getSurname() != null && filter.getAge() != null) {
p.getExpressions().add(
cb.and(cb.equal(root.get("surname"), filter.getSurname()),
cb.equal(root.get("age"), filter.getAge())));
}
return p;
}
}
...
Person filter = new Person();
filter.setName("Mario");
filter.setSurname("Verdi");
filter.setAge(25);
Specification<Person> spec = new PersonSpecification(filter);
List<Person> result = repository.findAll(spec);
以下是github中的完整示例
您还可以使用规范创建任何复杂的查询
问题内容: 我正在尝试将范围查询与elasticsearch一起使用 但是弹性返回没有结果。我发现系统存在字符串包含或问题 这是该字段的映射: 问题答案:
我想在Spring-Boot后端创建一个多字段搜索。如何使用实现这一点? 环境 前端的UI是Jquery DataTable。每列允许应用单个字符串搜索项。跨多个列的搜索词由联接。 看来QueryDSL是解决这个问题的更简单、更好的方法。我在用Gradle。我需要改变我的构造吗?
本文向大家介绍solr范围搜索,包括了solr范围搜索的使用技巧和注意事项,需要的朋友参考一下 示例 age:[50 TO 60] 匹配年龄在50和60之间(包括50和60)的文档 age:{50 TO 60} 匹配年龄在50到60之间(不包括50到60)的文档 age:[* TO 60] 匹配年龄小于或等于60的文档 age:[50 TO *] 匹配年龄大于或等于50的文档 age:{50 to
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the
我使用Lucene 6.3,但我无法找出以下非常基本的搜索查询有什么问题。它只是添加到具有单个日期范围的文档中,然后尝试在更大的范围内搜索,应该可以找到两个文档。什么是错的? 有内联注释应该使示例非常不言自明。如果有什么不清楚的地方,请告诉我。 请注意,我的主要要求是能够执行日期范围查询以及其他字段查询,例如 这是在观看了Lucene空间深潜视频介绍之后,介绍了DateRangePrefix Tr
版本字符串有一个支持java类(版本),它实现了Comparable。 我的分析器是一个分析器包装器,它是一个小写和空格分析器,类似于内置的分析器。我使用经典的查询解析器进行搜索。在确切的条件下搜索工作良好。 我想做的是: 我试图在索引之前将版本字符串转换为int,但查询输入需要以某种方式转换,以便在搜索之前将版本字符串转换为int。 看起来我必须为version字段实现一个自定义分析器,但是我在