Cara menggunakan mongodb atlas fuzzy search

The wildcard operator enables queries which use special characters in the search string that can match any character.

Character

Description

?

Matches any single character.

*

Matches 0 or more characters.

\

Escape character.

wildcard is a term-level operator, meaning that the

"*\\**"
0 field is not analyzed. Term-level operators work well with the , because the
"*\\**"
0 field is treated as a single term, with special characters included. For an example of querying against an analyzed
"*\\**"
0 field vs. a non-analyzed
"*\\**"
0 field, see the

Syntax

wildcard has the following syntax:

{
$search: {
"index": , // optional, defaults to "default"
"wildcard": {
"query": "",
"path": "",
"allowAnalyzedField": ,
"score":
}
}
}

Options

wildcard uses the following terms to construct a query:

Field

Type

Description

Necessity

Default

"*\\**"
0

string or array of strings

String or strings to search for.

yes

"*\\**"
7

string or array of strings

Indexed field or fields to search. You can also specify a to search. See for more information.

yes

"*\\**"
8

boolean

Must be set to

"*\\**"
9 if the query is run against an analyzed field.

no

"*\\\*"
0

"*\\\*"
1

object

Modify the assigned to matching search term results. Options are:

  • "*\\\*"
    2: multiply the result score by the given number.

  • "*\\\*"
    3: replace the result score with the given number.

no

Behavior

wildcard is a term-level operator, meaning that the

"*\\**"
0 field is not analyzed. It is possible to use the wildcard operator to perform searches on a field analyzed during indexing by setting the
"*\\**"
8 option to
"*\\**"
9, but results will reflect that the query text is not analyzed.

Example

Suppose that a field

"*\\\*"
9 is indexed with the . Atlas Search analyzes and indexes the field as
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
0,
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
1 and
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
2. Searching for
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
3 on this field finds nothing, because the wildcard operator treats
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
3 as a single search term with a wildcard at the end. In other words, Atlas Search searches the field for any term that begins with
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
5 but finds nothing, because no term exists.

Example

Searching for

1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
6 on a field indexed with the
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
7 analyzer finds all documents in which the field contains the string
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
8 in any context. Searching for
1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}
6 on a field indexed with the finds nothing, because there is a space between wildcard0 and wildcard1, and the index contains no spaces.

Escape Character Behavior

When using the escape character in or with a driver, you must use a double backslash before the character to be escaped.

Example

To create a wildcard expression which searches for any string containing a literal asterisk in an aggregation pipeline, use the following expression:

"*\\**"

The first and last asterisks act as wildcards which match any characters, and the wildcard3 matches a literal asterisk.

Note

Use the following expression to escape a literal backslash:

"*\\\*"


➤ Use the Select your language drop-down menu to set the language of the examples on this page.


Examples

The following examples use the wildcard4 collection in the wildcard5 database with a custom index definition that uses the . If you have the on your cluster, you can create an Atlas Search index on the wildcard4 collection and run the example queries on your cluster.

Tip

If you've already loaded the , follow the tutorial to create an and run Atlas Search queries.

Index Definition

The following index definition indexes the wildcard7 field in the wildcard4 collection with the

1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}

The following example searches all wildcard7 fields for movie titles that begin with wildcard0, followed by any number of other characters.

The above query returns the following results:

The following example searches all wildcard7 fields for movie titles that begin with the string wildcard2 (where ? may be any single character), followed by a space and then any number of additional characters. The wildcard4 stage limits the results to 5 documents, while the wildcard5 stage limits the results to only the wildcard7 field.

The above query returns the following results:

Escape Character Example

The following example searches for documents in which the wildcard7 field ends with a question mark.

Note

The following example is intended to run in . For more information about using the escape characters with a driver, see

The * character in the

"*\\**"
0 field matches any characters, and the ?1 string matches a literal question mark. The wildcard4 stage limits the results to 5 documents, while the wildcard5 stage limits the results to only the wildcard7 field.