Today I’m going to talk to you about the search function of small programs. I use the background database here is a small program cloud development of the cloud database. So when we search, we need to use cloud development to achieve.

A, demand

For example, I have the following data

We want to implement the following search requirements

  • 1. Search for data whose title contains’ little rock ‘
  • 2, search for data with title or description desc containing ‘little rock’
  • 3, search for data that contains’ little rock ‘in title description (desc)

We know that there is a WHERE statement when querying a database, but the WHERE statement is only used to query a certain field that contains all the content you entered, so using only the WHERE statement to do a search is too simple. So today we are going to learn the implementation of fuzzy search. Let’s take the above three requirements as examples to explain one by one.

Second, the realization principle

When we do a fuzzy search, we actually look for a field that contains our search term. And fuzzy search requires a RegExp, so let’s see what a RegExp is.Official documents:Developers.weixin.qq.com/miniprogram…

  • Let’s look at the official example again

May look at the official example will be a little confused, so we will combine the concrete code to explain to you.

Three, fuzzy search code implementation

3-1, fuzzy search for a single field

  • Requirement: Search for data that contains’ little rock ‘in title

The following codeThe query results are as follows:As you can see, we managed to query the title with ‘data for small rocks’.

3-2, fuzzy search for multiple fields (satisfies only one)

  • Requirement: Search for data with title or description desc containing ‘little rock’

Since we are querying multiple fields, we use the OR from the command advanced operatorThe code is as follows:Query result:So let’s look at these two numbers

  • 1. Both the title and description contain ‘little stone’
  • 2. It’s not in the title, but it’s in the description, so it fits.
  • 3, there is no ‘small stone’ in title and desc, so it does not match.

3-3, fuzzy search for multiple fields (to satisfy simultaneously)

  • Required: Search title description (desc) contains’ little rock ‘data

Since we are querying multiple fields, we use the and in the command advanced operator

The code is as follows:

Query result:So let’s look at these two numbers

  • 1. Both the title and description contain ‘little stone’
  • 2, although there is no ‘small stone’ in desc, there is no ‘small stone’ in title, so it does not match.
  • 3, there is no ‘small stone’ in title and desc, so it is also inconsistent.

Four, the source code

As an example for you to use, I put the complete code here, later when you use, directly copy the code here, slightly modified can be.

// I'm going to write the search term dead for simplicity, Let searchKey = 'little rock' let db = wx.cloud.database() let _ = db.command db.collection('news'). Where (_. {// title: db.regexp ({// RegExp: searchKey, options: 'I ', // case - sensitive}),}, {// Description desc: db.RegExp({ regexp: searchKey, options: 'I'})}])). The get (). Then (res = > {the console. The log (' query success 'res)}). The catch (res = > {the console. The log (' query fails, res)})Copy the code

Here is the end of the story, I will be dedicated to cloud development in the introduction of the course as a practical case to record the video to everyone: “small program cloud development introduction video”