Skip to content

Instantly share code, notes, and snippets.

@orenovadia
Created June 2, 2020 17:11
Show Gist options
  • Save orenovadia/8ea4f4be434a8f2127ca7e7c03b4344f to your computer and use it in GitHub Desktop.
Save orenovadia/8ea4f4be434a8f2127ca7e7c03b4344f to your computer and use it in GitHub Desktop.

Atlas Search Compound Operator

Two common mistakes we see when folks are writing compound operator.

Duplicate keys under compound

For instance, two must or should keys under compound.

Example:

{
  "search": {
    "compound": {
      "must": {
        "text": {
          "path": "title",
          "query": "bistro"
        }
      },
      "must": {  // this "must" overwrites on the first
        "text": {
          "path": "description",
          "query": "bistro"
        }
      }
    }
  }
}

Duplicate clauses

For instance, two query operators like text under must or under should. Example:

{
  "search": {
    "compound": {
      "must": {
        "text": {
          "path": "title",
          "query": "bistro"
        },
        "text": { // this "text" overwrites the first
          "path": "description",
          "query": "bistro"
        }
      }
    }
  }
}

Proper Syntax

Object keys must be unique always. To specify multiple clauses, they must appear as objects under an array.

Example:

{
  "search": {
    "compound": {
      "must": [
        {
          "text": {
            "path": "title",
            "query": "bistro"
          }
        },
        {
          "text": {
            "path": "description",
            "query": "bistro"
          }
        }
      ]
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment