Christian Nilsson
Christian Nilsson

Reputation: 1

How do I get expected results when searching in MongoDB (Atlas)

Issue

I'm looking for a third solution where "Alicex Flicex" will not return "Alice Maria Bobsson" and "Alice Bobsson" will return "Alice Maria Bobsson"

Requirements

Setup

Profile

{
  name: {
    firstName: 'Alice Maria',
    lastName: 'Bobsson',
    fullName: 'Alice Maria Bobsson'
  }
}

Index


Attempt 1

  1. Split search string to separate the names
  2. Search with each name part for a match in firstName OR lastName with fuzzy level 2. All name parts in the search string needs to match.
[
  {
    "$search": {
      "index": "indexName",
      "compound": {
        "must": [
          {
            "compound": {
              "should": [
                {
                  "text": {
                    "query": "Alicex",
                    "path": "name.firstName",
                    "fuzzy": {
                      "maxEdits": 2
                    }
                  }
                },
                {
                  "text": {
                    "query": "Alicex",
                    "path": "name.lastName",
                    "fuzzy": {
                      "maxEdits": 2
                    }
                  }
                }
              ]
            }
          },
          {
            "compound": {
              "should": [
                {
                  "text": {
                    "query": "Flicex",
                    "path": "name.firstName",
                    "fuzzy": {
                      "maxEdits": 2
                    }
                  }
                },
                {
                  "text": {
                    "query": "Flicex",
                    "path": "name.lastName",
                    "fuzzy": {
                      "maxEdits": 2
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
]

Input 1

name: Alicex Flicex

Result 1

Expected result
Not return "Alice Maria Bobsson"

Actual result
Returning "Alice Maria Bobsson"

Reasons


Attempt 2

  1. Search with full search string for a fullName match with fuzzy level 2
[
  {
    "$search": {
      "index": "indexName",
      "compound": {
        "must": [
          {
            "text": {
              // "query": "Alicex Flicex", Name corrected with next line 
              "query": "Alice Bobsson",
              "path": "name.fullName",
              "fuzzy": {
                "maxEdits": 2
              },
              "matchCriteria": "all"
            }
          }
        ]
      }
    }
  }
]

Input 2

name: Alice Bobsson

Result 2

Expected result
Return "Alice Maria Bobsson"

Actual result
Do not return "Alice Maria Bobsson"

Reasons

Upvotes: 0

Views: 56

Answers (0)

Related Questions