我有一个Spring Boot应用程序连接到包含this sample data的Elasticsearch实例。我目前能够搜索特定字段,但是当我在请求中添加第二个字段时,我没有任何结果(每个字段都可以自行完成)。我怎样才能得到正确的结果?
以下是我的课程:
@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));
}
}
以下是从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"
}
}
]
}
}
}
]
答案 0 :(得分:0)
原来我正在查询第1页,而不是#0。结果现在正确显示。