last day (14 days later) » 

12:31 PM
0
A: MongoDB & Spring Data - Searching for a user by firstname/lastname

pinturicTo search for firstname AND lastname you can easily do this: 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 = Crit...

 
Thanks for your comment, but it does not work (I tried it). It looks when a firstname AND lastname match the searchQuery. I have editted my post for clarity.
 
Still it is not clear what you are searching for. Do you want to find that user when searchQuery is "John Smith"? if that is the case you have manually break the string into two parts and use only an orOperator.
 
Exactly. Just as in Facebook, whether you search for the firstname OR lastname OR firstname+lastname, it retrieves a user. I used th orOperator in the original post, but I don't know how to combine firstname+lastname.
 
So my proposal is split searchQuery in case it has spaces like in the updated post
 
thanks for your comment. I firstly received an array out of bounds error when I enter only 1 name (since there is not an queries[1] value). But even if I enter "John Smith", I receive the following error: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second '$or' expression specified as '$or : [ { "firsname" : { "$regex" : "john"} , "$and" : [ { "lastname" : { "$regex" : "smith"}}]}]'. Criteria already contains '$or : [ { "lastname" : { "$regex" : "john smith"}}]'.] with root cause
 
12:31 PM
Updated the example; however the key is using a cascaded andOperator and orOperators
 
Unfortunately, it still does not work. This time however, when I insert "John Smith", I do not see any result at all. Inserting only John however still gives me an index out of bound and no result. Anyway, I appreciate your time and support. Isn't there really a simple way to combine 2 fields inside the database using Spring Data and just compare them against a query? Rather than splitting the query up?
Hi +pinturic, I updated my post with some code based on your idea. I can now actually search using firstname/lastname/firstname+lastname and see results! However, I see the following error in my console as the previous one, do you know how to fix this?
 
Programmatically speaking you cannot call or operator more than once on the same criteria. Put them in an array and call it only once.
 
Thanks for the tip. I updated the post again. However, now I can only search using the firstname. But I guess I am doing this process wrong?
 
Yes you are still using searchQueary instead of q in the for :D an please rate the answer if you like it.
 
I am unable to do that since q is outside the scope. I updated my answer (edit 3) and still see the results, but now get a null error. Is this fixable? And of course I will accept your answer
 
12:31 PM
You can try what I have added now in the test
 
It does not work. It appears to only work if firstname and lastname are equal.
 
it is really strange; however you can see query.toString() just before executing the query to debug the query
 
Thanks for your comment. Output is:
Query: Query: { "firstname" : { "$regex" : "john"} , "$or" : [ { "lastname" : { "$regex" : "john"}} , { "firstname" : { "$regex" : "smith"}} , { "lastname" : { "$regex" : "smith"}}]}, Fields: null, Sort: null
MY APOLOGIES. it appears to be working!
Except: I can now not search for "John" or "Smith" separately
In other words, if I search "John Smith" then it works! But searching "John" does not. Or "Smith" does not as well.
 
12:48 PM
I fixed this using an if else statement where I check whether there is a " " (space) in the query. If there isn't, I execute my original code. :)
Thanks for your time and help! +1 and accepted.
 
WOW perfect !
 

last day (14 days later) »