【问题标题】:Elasticsearch Spring repository search using multiple fields使用多个字段的 Elasticsearch Spring 存储库搜索
【发布时间】:2017-01-26 21:26:57
【问题描述】:

我有一个 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"
            }
          }
        ]
      }
    }
  }
]

【问题讨论】:

    标签: java spring elasticsearch spring-boot


    【解决方案1】:

    原来我查询的是页面#1,而不是#0。结果现在显示正确。

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2013-09-12
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2021-12-06
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多