I have an two tables:-

Countries Customers [have a number of column- ike name, address, city, zipcode, email, etc] Customer table have an column country [Pointer of Countries].

Now what I want: I have a search form and if some put "aus" in a search box and click on "Search" button I want to display all matching records, I want to apply search on "Name, email, address, city, and the Country name [pointer]"

So if someone has name Austin or Country name "Australia" will be the output of search, currently, I'm using "contains" on the name, email and it is working fine.

I tried to apply search on country but not successes, could someone please help to apply this.

Here is my current code that is working [Without Country search], I am using cloud functions.

`var customerName = new Parse.Query("customer");
  var customerEmail = new Parse.Query("customer");
  var customerAddress = new Parse.Query("customer");
  customerName.contains('name','aus');
  customerEmail.contains('email','aus');
  customerAddress.contains('address','aus');

 var serviceQuery = new Parse.Query.or(
      customerName,
      customerEmail,
      customerAddress
      // country
  );

.............` Thanks

2 Answers

1
Davi Macêdo On Best Solutions

Try something like this:

var customerName = new Parse.Query('customer');
var customerEmail = new Parse.Query('customer');
var customerAddress = new Parse.Query('customer');
customerName.contains('name','aus');
customerEmail.contains('email','aus');
customerAddress.contains('address','aus');

var countryQuery = new Parse.Query('country');
countryQuery.contains('name','aus');
var customerCountry = new Parse.Query('customer');
customerCountry.matchesQuery('country', countryQuery);

var serviceQuery = new Parse.Query.or(
  customerName,
  customerEmail,
  customerAddress,
  customerCountry
);

Instead of searching for each of customers' fields, you can use full text search: https://docs.parseplatform.org/js/guide/#full-text-search

0
Julien Quere On

A solution would be to compute a fullTextSearch field on cloud beforeSave of your objects. The best way is to store this string in lowercase and without diacritics. It will give better results if you do the same when searching (so that André will match andre or AnDrÉ).

Here is the helper I used to do so:

  /**
   * Generates fulltextsearch for any Parse.Object. It's the concatenation of the value
   * of all fields in propertiesToAdd, separated by a whitespace and lowercased.
   * Often used in beforeSave :)
   * @param propertiesToAdd the list of the object properties names that we want to handle in fulltextsearch
   * @param newObject the new version of the object
   */
  static generateFulltextSearch(propertiesToAdd, newObject): string {
    let result = '';
    propertiesToAdd.forEach(property => {
      let value = newObject.get(property);
      if (value) {
        result += DiacriticRemove(value) + ' ';
      }
    });

    return result.trim().toLocaleLowerCase();
  }

DiacriticRemove is simply a call to Diacritics package.

In your beforeSave (in cloud code), you just have to call:

  myCustomer("myFullTextField", generateFulltextSearch(["name", "email", "address", "country", "anyotherField"], myCustomer))

Then, when you're searching:

var customer = new Parse.Query("customer");
// Don't forget to lowercase and remove diacritics from your searched string.
customer.contains('myFullTextField','aus');

And voilà :)