[英]Elasticsearch Spring repository search using multiple fields
I have a Spring Boot application connected to an Elasticsearch instance containing this sample data . 我有一个Spring Boot应用程序连接到包含此示例数据的Elasticsearch实例。 I am currently able to search for a specific field, but the moment I add the second one in the request, I don't have any results (each of the fields are fine on their own). 我目前可以搜索特定的字段,但是当我在请求中添加第二个字段时,我没有任何结果(每个字段都可以单独使用)。 How can I get the correct results? 如何获得正确的结果?
Here are my classes: 这是我的课程:
@Document(indexName = "bank", type = "account", replicas = 0)
public class Account {
@Id
private String id;
private long accountNumber;
private long balance;
private String firstname;
private String lastname;
private long age;
private String gender;
private String address;
private String employer;
private String email;
private String city;
private String state;
}
public interface AccountRepository extends ElasticsearchRepository<Account, String> {
Page<Account> findByGenderAndStateAllIgnoreCase(String gender, String state, Pageable pageable);
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountRepository repository;
@Override
public Account save(Account account) {
return repository.save(account);
}
@Override
public Account findOne(String id) {
return repository.findOne(id);
}
@Override
public Collection<Account> findAll(PageRequest request) {
return repository.findAll(request).getContent();
}
@Override
public Collection<Account> findByGenderAndState(String gender, String state, PageRequest request) {
return repository.findByGenderAndStateAllIgnoreCase(gender, state, request).getContent();
}
}
@Controller
@RequestMapping("/bank")
public class BankController {
@Autowired
private AccountService accountService;
@GetMapping("/accounts")
public
@ResponseBody
Collection<Account> accounts(@RequestParam(name = "gender", required = false, defaultValue = "*") String gender,
@RequestParam(name = "state", required = false, defaultValue = "*") String state,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@RequestParam(name = "size", required = false, defaultValue = "10") int size) {
return accountService.findByGenderAndState(gender, state, new PageRequest(page, size));
}
}
Here is the query that is sent to Elasticsearch from Spring Boot: 这是从Spring Boot发送到Elasticsearch的查询:
[
{
"from": 20,
"size": 20,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "f",
"fields": [
"gender"
],
"default_operator": "and"
}
},
{
"query_string": {
"query": "dc",
"fields": [
"state"
],
"default_operator": "and"
}
}
]
}
}
}
]
Turns out I was querying for the page #1, instead of #0. 原来我在查询页面#1,而不是#0。 The results are showing correctly now. 结果现在正确显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.