Hibernate Spring数据弹性搜索嵌套搜索-jhipster
我正在使用Jhipster项目生成器创建一个基本的CRUD应用程序。应用程序具有使用org.spingframework.data.elasticsearch的搜索功能。我有一个客户域和一个地址域,如下所示Hibernate Spring数据弹性搜索嵌套搜索-jhipster,hibernate,rest,elasticsearch,spring-boot,jhipster,Hibernate,Rest,elasticsearch,Spring Boot,Jhipster,我正在使用Jhipster项目生成器创建一个基本的CRUD应用程序。应用程序具有使用org.spingframework.data.elasticsearch的搜索功能。我有一个客户域和一个地址域,如下所示 /** * A Customer. */ @Entity @Table(name = "CUSTOMER") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Document(indexName="custom
/**
* A Customer.
*/
@Entity
@Table(name = "CUSTOMER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName="customer")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "forename")
private String forename;
@Column(name = "surname")
private String surname;
@ManyToOne
@Field(type = FieldType.Nested)
private Address address;
}
/**
* An Address.
*/
@Entity
@Table(name = "ADDRESS")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName="address")
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Min(value = 1)
@Max(value = 9999)
@Column(name = "number")
private Long number;
@NotNull
@Size(min = 5, max = 50)
@Column(name = "line1", length = 50, nullable = false)
private String line1;
@Size(min = 5, max = 50)
@Column(name = "line2", length = 50)
private String line2;
@NotNull
@Size(min = 2, max = 50)
@Column(name = "city", length = 50, nullable = false)
private String city;
@Size(min = 2, max = 50)
@Column(name = "county", length = 50)
private String county;
@Size(min = 2, max = 50)
@Column(name = "country", length = 50)
private String country;
@NotNull
@Size(min = 7, max = 8)
@Column(name = "postcode", length = 8, nullable = false)
private String postcode;
}
每个客户都有一个地址。我有一个多对一的映射,所以每个地址可以有许多客户
现在,在我的资源中,我有以下搜索查询
/**
* SEARCH /_search/customers/:query -> search for the customer corresponding
* to the query.
*/
@RequestMapping(value = "/_search/customers/{query}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Customer> searchCustomers(@PathVariable String query) {
return StreamSupport
.stream(customerSearchRepository.search(queryString(query)).spliterator(), false)
.collect(Collectors.toList());
}
/**
*搜索/\u搜索/customers/:查询->搜索对应的客户
*对查询进行修改。
*/
@RequestMapping(value=“/\u search/customers/{query}”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值)
@定时
公共列表searchCustomers(@PathVariable字符串查询){
返回流支持
.stream(customerSearchRepository.search(queryString(query)).spliterator(),false)
.collect(Collectors.toList());
}
目前,这将根据我输入的任何标准返回客户列表,就像使用搜索引擎一样。但是,搜索将与客户域中的地址字段不匹配。理想情况下,我想在某个邮政编码处搜索某个客户的姓氏,但目前它只会与非地址的客户字段匹配。如果我修改搜索查询以使用findByPostcode或类似的东西,那么我将失去按客户名称/详细信息进行搜索的能力
简单地说,我如何在不取消数据库规范化的情况下搜索客户详细信息字段和客户地址字段?地址上的文档注释不是必需的,我的意思是:
`@ManyToOne
@Field(type = FieldType.Nested)
private Address address;`
应该是:
`@ManyToOne
private Address address;`
谢谢你的建议。我创建了一个新的测试项目来重新讨论这个问题,我已经让它按照我想要的方式工作了。不确定是否是因为这次我没有使用DTO。