问题
我必须按姓名搜索用户,联系任何用户类型,即如果他输入姓名,他只获取姓名,如果他键入联系人,他只会联系而不是我获得所有用户结果。 < / p>
解决方案 -
第一种方法
我已经使用spring数据应用或查询了findByNameOrContactOrLocation等MongoDb.Below是我的代码。
public class Search {
private String searchKey;
private double[] location;
private List<Gender> gender;
private List<String> profession;
private List<INTERESTS> interests;
private int minAge;
private int maxAge;
private double[] height;
@PutMapping(value = "/searchByEndUser")
ApiResponse<List<Employee>> searchEndUserBasedOnKeyWord(@RequestBody Search params) {
return ApiResponse.success().object(employeeService.getByWord(params));
}
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Override
public List<Employee> getByWord(Search params) {
return employeeRepository.findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween(params.getSearchKey(), params.getSearchKey(), params.getLocation(), params.getGender(), params.getProfession(), params.getInterests(), params.getHeight(), params.getMinAge(), params.getMaxAge());
}
public interface EmployeeRepository extends MongoRepository<Employee, String> {
List<Employee> findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween(String searchKey, String searchKey1, double[] location, List<Gender> gender, List<String> profession, List<INTERESTS> interests, double[] height, int minAge, int maxAge);
}
输入提供
{
"searchKey": "98111648642"
}
预期输出
{
"createdBy": "anonymousUser",
"lastModifiedBy": "anonymousUser",
"version": 0,
"createdAt": 1510303944245,
"lastModifiedAt": 1510303944245,
"imageIds": null,
"concerns": null,
"summary": null,
"likings": null,
"occupation": null,
"religion": null,
"education": null,
"height": null,
"location": null,
"interests": null,
"fcmId": null,
"connections": null,
"declined": null,
"pending": null,
"likes": 0,
"dislikes": 0,
"review": null,
"ratings": null,
"favourites": null,
"disConnReason": null,
"employee_status": null,
"employee_role": null,
"age": 0,
"notification": false,
"contact": "98111648642",
"email": null,
"address": "13",
"gender": null,
"imageId": null,
"firstName": null,
"lastName": null,
"middleName": null,
"dob": null,
"username": "jack",
"country": null,
"designation": null,
"aboutMe": null,
"profession": null,
"sos": null,
"secret": null,
"socialProfile": null,
"socialProfileLink": null,
"socialToken": null,
"socialProfilePicture": null,
"referralCode": null,
"refCons": 0,
"referredUserId": null,
"choice": null,
"id": "5a0568c879eb192b872592b3"
}
]
}
下面我只显示2个输出
显示输出
{
"createdBy": "anonymousUser",
"lastModifiedBy": "anonymousUser",
"version": 0,
"createdAt": 1510228467303,
"lastModifiedAt": 1510228467303,
"imageIds": null,
"concerns": null,
"summary": null,
"likings": null,
"occupation": null,
"religion": null,
"education": null,
"height": null,
"location": null,
"interests": null,
"fcmId": null,
"connections": null,
"declined": null,
"pending": null,
"likes": 0,
"dislikes": 0,
"review": null,
"ratings": null,
"favourites": null,
"disConnReason": null,
"employee_status": null,
"employee_role": null,
"age": 0,
"notification": false,
"contact": "9811164864",
"email": null,
"address": "32",
"gender": null,
"imageId": null,
"firstName": null,
"lastName": null,
"middleName": null,
"dob": null,
"username": "jui",
"country": null,
"designation": null,
"aboutMe": null,
"profession": null,
"sos": null,
"secret": null,
"socialProfile": null,
"socialProfileLink": null,
"socialToken": null,
"socialProfilePicture": null,
"referralCode": null,
"refCons": 0,
"referredUserId": null,
"choice": null,
"id": "5a0441f379eb1930d9f961b2"
},
{
"createdBy": "anonymousUser",
"lastModifiedBy": "anonymousUser",
"version": 0,
"createdAt": 1510303944245,
"lastModifiedAt": 1510303944245,
"imageIds": null,
"concerns": null,
"summary": null,
"likings": null,
"occupation": null,
"religion": null,
"education": null,
"height": null,
"location": null,
"interests": null,
"fcmId": null,
"connections": null,
"declined": null,
"pending": null,
"likes": 0,
"dislikes": 0,
"review": null,
"ratings": null,
"favourites": null,
"disConnReason": null,
"employee_status": null,
"employee_role": null,
"age": 0,
"notification": false,
"contact": "98111648642",
"email": null,
"address": "13",
"gender": null,
"imageId": null,
"firstName": null,
"lastName": null,
"middleName": null,
"dob": null,
"username": "jack",
"country": null,
"designation": null,
"aboutMe": null,
"profession": null,
"sos": null,
"secret": null,
"socialProfile": null,
"socialProfileLink": null,
"socialToken": null,
"socialProfilePicture": null,
"referralCode": null,
"refCons": 0,
"referredUserId": null,
"choice": null,
"id": "5a0568c879eb192b872592b3"
}
]
}
**2nd Approach**
1)我可以检查数据是来自用户设置还是设置随机数据。虽然我还没有实现它。我正在寻找替代方法。
任何人都可以指导我做错了什么或任何替代方法吗?
答案 0 :(得分:0)
findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween有效意味着
Get_me_documents_where {
firstname : "98111648642"
or
contact : "98111648642"
or
location : null
or
gender : null
or
......
}
由于您使用OR,它将获取所有记录。因为可能有一些属性对所有记录都是null 正如@Niel建议的那样,拥有如此长的查询语句并不是一个好主意。 spring-data-mongodb可以解决这个问题,但你会失去清晰度。