Using OR operator for a ngram field and keyword field

Hello everyone,

I’m new with atlas search and I don’t know how I can build an ‘OR’ operator through multiple fields.

Here is what I did:

Search index configuration:

{
  "searchAnalyzer": "lucene.keyword",
  "mappings": {
    "dynamic": false,
    "fields": {
      "lrn": {
        "analyzer": "lucene.keyword",
        "type": "string"
      },
      "title": {
        "analyzer": "aks_ngram",
        "type": "string"
      }
    }
  },
  "analyzers": [
    {
      "charFilters": [],
      "name": "aks_ngram",
      "tokenFilters": [
        {
          "type": "lowercase"
        }
      ],
      "tokenizer": {
        "maxGram": 10,
        "minGram": 2,
        "type": "nGram"
      }
    }
  ]
}

My search query:

{
    $search: {
      index: "default",
      compound: {
        should: [
          {
            text: {
              query: "558988b6b1661680917206",
              path: "title",
            }
          },
          {
            text: {
              query: "558988b6b1661680917206",
              path: "lrn"
            }
          }
        ]
      }
    }
  }

What do I expect to get:

[
  {
  title: 'WXYZQP',
  lrn: '558988b6b1661680917206'
 }
]

What do I get:

[
 {
  title: '75669860766',
  lrn: '5832748721661680917174'
 },
 {
  title: 'WXYZQP',
  lrn: '558988b6b1661680917206'
 }
]

I want on the “title” field to search only by substrings and “lrn” to be perfect match.

Thanks! :smiley:

Hi @Catalin_Radu and welcome to MongoDB community forums!!

Based on my understanding for the above post, I tried to replicate the issue in my local environment with the same dataset as:

[
  {
    _id: ObjectId("6458ee0dbe43b85019d199e4"),
    title: 'WXYZQP',
    lrn: '558988b6b1661680917206'
  },
  {
    _id: ObjectId("6458ee30be43b85019d199e5"),
    title: '75669860766',
    lrn: '5832748721661680917174'
  }
]

If I understand correctly, you need to create search indexes on title and lrn fields and trying to search the documents using the should operator.

The search index for the above sample document looks like:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "lrn": {
        "type": "string"
      },
      "title": {
        "type": "string"
      }
    }
  }
}

and the search query is:

[
  {
    '$search': {
      'compound': {
        'should': [
          {
            'text': {
              'query': '75669860766', 
              'path': 'title'
            }, 
            'text': {
              'query': '558988b6b1661680917206', 
              'path': 'lrn'
            }
          }
        ]
      }
    }
  }
]

returns the document as:

[
  {
    _id: ObjectId("6458ee0dbe43b85019d199e4"),
    title: 'WXYZQP',
    lrn: '558988b6b1661680917206'
  }
]

If the above query does not work, could you share the sample dataset which would help me reproduce and provide the query in a better way.

For reference, you can start learning about how Atlas search works in MongoDB using the University from MongoDB Courses and Trainings | MongoDB University and Atlas search Docs.

To learn how SHOULD, MUST and NOT works in Atlas search, you can refer to the documentation for compound operators.

Let us know if you have any further queries.

Regards
Aasawari