10

I am using Parse.com and access it using Javascript. I want to search data in such a manner that it will not search for case sensitive like if i search abc then it will give abc, Abc, ABC,aBc etc all. but currently it gives only abc.

JavaScript:

var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);

query.contains("name",search_data); 
query.find({
     success: function(results) {
        console.log("Total Product Found : "+results.length);
        printSearchProducts(results,query);  //custom method to print product detail
     },
     error: function(error) {
         console.log("Error: " + error.code + " " + error.message);
     }
 });

Actual Result: Temp1,Temp2,Temp3,ProductTemp3,Temp4Product etc.

Required Result: Temp1,Temp2,Temp3,ProductTemp3,Temp4Product,temp5,producttemp6,TEMP7,tEMp8 etc.

4 Answers 4

42

For that you can use Parse.Query's matches() function, which even it's not indicated in its documentation page, but you can use it like this :

query.matches(key, value, 'i');

Hope that can help.

Sign up to request clarification or add additional context in comments.

5 Comments

this is the right answer, but how would this work in JSON, REST
How do you query on multiple fields? If I add matches one after the other I get no results instead even with an empty query.
@Chrillewoodz I think for that you can use compound queries ...
This together with mentioning compound queries is the right answer!
5

At this point in time you aren't able to perform case insensitive searches via a query.

A very easy workaround for this however, is to store a lower case version of the field you need to do this query on.

Creating The Item

var Product = Parse.Object.extend("product");
var newProduct = new Product();
var productName = "Temp4Product";
newProduct.set("name",productName);
newProduct.set("name_lowercase",productName.toLowerCase());
newProduct.save(); 

Performing the query

var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);

query.contains("name_lowercase",search_data.toLowerCase()); 
query.find({
     success: function(results) {
        console.log("Total Product Found : "+results.length);
        printSearchProducts(results,query);  //custom method to print product detail
     },
     error: function(error) {
         console.log("Error: " + error.code + " " + error.message);
     }
 });

Note in the above example I've used the string.toLowerCase() function, however there may be a more appropriate function for you to use. Basically you want to find a way to "simplify" your data so that you can perform the appropriate query.

8 Comments

thanks @ahar083 for your reply but i don't want to change the case in my product class on parse.com.
@Shashi I'm not suggesting you change only store the lower case value. Note in my example above, I'm saving out "name" (which retains case), as well as "name_lowercase". The only downside of this solution is you are storing slightly more data in your table, this should not be a big problem. At this point there is no alternative, as Parse does not support case insensitive queries at this point in time. They may add it in the future, however it has back-end performance implications.
@Shashi - I still don't have enough rep to reply to your proposed answer below. While your answer does work, it is very inefficient! You are pulling down 500 items from Parse.com to your server (or the client's machine, if this is javascript running in the client browser). You might be pulling down say 1MB worth of data, rather than 2KB, so the use could be waiting say 1 minute for a response, rather than 1 second. (These numbers are arbitrary, but just to give you an idea).
I have only 400 products and according to requirement, we can't add an extra column in product class which stores name in lower case that is why below method is best suited for me i know it is not optimum solution because each time it fetch all product data which may decrease performance as you said.
@Shashi - Fair enough, there are always trade-offs, as long as you are aware of the increased latency and larger download involves. If it is a desktop application, this might be no problem at all.
|
0

The solutions that suggest an extra stored field with the lowercase value have the disadvantage of requiring additional storage. The matches method works, but has a minor performance cost.

A better workaround that avoids both of these issues is to use an aggregate with the $toLower expression in a projection.

1 Comment

Hi! I don't understand your solution, could you please add code? Or point me to what "use an aggregate with the $toLower expression in a projection" means?
-1

I have solved this issue. I remove constraint query.contains("name",search_data); and apply manual search using java script inside printSearchProducts method like:

var search_data="Temp";
var product = Parse.Object.extend("product");
var query = new Parse.Query(product);
query.limit(500);
query.find({
    success: function(results) {
         printSearchProducts(results,query,search_data); 
    }, 
    error: function(error) {
         console.log("Error: " + error.code + " " + error.message);
   } 
});

printSearchProducts Method Where search manually via Java Script.

function printSearchProducts(results,query,search_data) {
    var counter = results.length; 
    var productName = '',i=0; 
    search_data = search_data.toLowerCase(); 
    if(counter){
        while(i<counter){
            var found = -1;
            productName = results[i].get("name");
            var lower_name = productName.replace(/[^a-zA-Z]/g,'_').toLowerCase();
            found = lower_name.indexOf(search_data);
            if(found>=0) {
                  //Access or print the required result here 
            } 
            i++;
        }
    }  
}

2 Comments

I would strongly recommend against this method, you will always be pulling down all products every time you do a search, and you'll have issues if you end up with more than 500 products (or 1000 if you increase the limit to the max value). If you need all products for other parts of the page this may be acceptable, but it is not ideal.
@TimothyWalters: I have only 400 products and according to requirement, we can't add an extra column in product class which stores name in lower case that is why above method is best suited for me i know it is not optimum solution because each time it fetch all product data which may decrease performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.