Search code examples
javascriptparse-platform

Search for case insensitive data from Parse using javascript


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.


Solution

  • 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.