【发布时间】:2016-01-02 12:01:26
【问题描述】:
我正在使用 Jhipster 项目生成器来创建一个基本的 CRUD 应用程序。该应用程序具有使用 org.spingframework.data.elasticsearch 的搜索功能。我有一个客户域和一个地址域,如下所示。
/**
* 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());
}
目前,这将根据我输入的任何标准返回客户列表,就像使用搜索引擎一样。但是,搜索不会与客户域中的地址字段匹配。理想情况下,我想在某个邮政编码搜索某个客户姓氏,但目前它只会匹配不是地址的客户字段。如果我修改搜索查询以使用 findByPostcode 或类似的内容,那么我将无法按客户名称/详细信息进行搜索。
简而言之,如何在不对数据库进行反规范化的情况下搜索客户详细信息字段和客户地址字段?
【问题讨论】:
标签: hibernate rest elasticsearch spring-boot jhipster