MongoDB & Spring 数据 - 通过 firstname/lastname 搜索用户

MongoDB & Spring Data - Searching for a user by firstname/lastname

我有以下合集:

User {
   firstname,
   lastname
}

我想按名字 and/or 姓氏(firstname/lastname/firstname+姓氏)搜索用户。

我有:

 Criteria criteria = new Criteria();
 criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(searchQuery));

上面的问题是我可以按名字或姓氏搜索,但不能同时搜索(例如,“'John Smith'”不可能,而“'John'”或“[=46=” ]' 是可能的。 这是因为 orOperator 而发生的,我看不到任何 andOr 或类似的东西。

例如如果我的数据库中有 "John Smith",我可以通过查询 "John" 、 "Smith" 、 "John Smith" (就像在 facebook 中一样)找到他

编辑:

String queryx[] = searchQuery.split(" ");
for(String q : queryx) {
    criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(q));
}

显示结果,但在我的控制台中出现以下错误:

  Due to limitations of the com.mongodb.BasicDBObject, you can't add a second '$or' expression specified as '$or : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "smith"}}]'. Criteria already contains '$or : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "john"}}]'.

编辑2:

String queryx[] = searchQuery.split(" ");
   ArrayList<Criteria> orOperators = new ArrayList<Criteria>();
for(String q : queryx) {
    Criteria criteria = new Criteria();
    criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(q));
    orOperators.add(criteria);
}

Criteria [] orArray = orOperators.toArray(new Criteria[orOperators.size()]);
Criteria c = Criteria.where("firstname").regex(searchQuery);
c.orOperator(orArray);

Query query = new Query(c).limit(limit);
return mongoOperations.find(query, User.class);

编辑 3:

String queryx[] = searchQuery.split(" ");
ArrayList<Criteria> cr = new ArrayList<Criteria>();
for (String q : queryx) {
    Criteria criteria = new Criteria();
    criteria.orOperator(Criteria.where("firstname").regex(q),
            Criteria.where("lastname").regex(q));
    cr.add(criteria);
}

Query query = new Query();
for (Criteria cri : cr) {
    query.addCriteria(cri); //this is the problem
}

return mongoOperations.find(query, User.class);

错误:

 Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'null' criteria. Query already contains '{ "$or" : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "john"}}]}'.

要搜索名字和姓氏,您可以轻松地执行以下操作:

String queryx[] = searchQuery.split(" ");
ArrayList<Criteria> cr = new ArrayList<Criteria>();
for (String q : queryx) {
    Criteria fCriteria = Criteria.where("firstname").regex(q);
    cr.add(fCriteria);
    Criteria lCriteria = Criteria.where("lastname").regex(q);
    cr.add(lCriteria);
}
Criteria c = cr.remove(0);
Criteria [] orArray = orOperators.toArray(new Criteria[orOperators.size()]);
c.orOperator(orArray);
query.addCriteria(c); //this is the problem
return mongoOperations.find(query, User.class);

如果不使用标准,您只需在实体 'User' 中的 'firstname''lastname' 上放置一个 @TextIndexed 并执行此操作:

@Query("{ $text: { $search: ?0 } }")
List<User> searchUsers(String search);

它适用于所有这些搜索查询:

  • "john"
  • "john doe"
  • "doe john"
  • "doe"

我用了,成功了

List<Criteria> userNameCriteriaList = Lists.newArrayList();

for (String key: keyWords) {
    Criteria criteria = new Criteria();
    criteria.orOperator(
         where(FIRST_NAME).regex(regex, "i"),
         where(LAST_NAME).regex(regex, "i")
    );
    userNameCriteriaList.add(criteria);
}

return new Criteria().andOperator(userNameCriteriaList.toArray(new Criteria[0]));