Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 212
Shashi Avatar asked Sep 12 '13 11:09

Shashi


2 Answers

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.

like image 124
akmozo Avatar answered Nov 11 '22 06:11

akmozo


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.

like image 34
ardrian Avatar answered Nov 11 '22 04:11

ardrian