你可以建立你的 query 然后将其传递给搜索请求。在构建期间,您可以有条件地将您的语句添加到查询中。它看起来像这样
query
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(termQuery("country", countryName)) .must(termQuery("Region", regionName)) .must(rangeQuery("persons").from(persons)); if(city != null && city.trim().equals("")) { queryBuilder.must(termQuery("City", city)); } .setQuery(queryBuilder); //add query to your search request