How to perform search with multiple operators?

How can I build a query to search with multiple operators like if I need to search -

  1. Honda and car or mobile and iPhone

  2. MongoDB Atlas and fulltext or elasticsearch and fulltext

Based on the above 2 queries, and need to search in must and or needs to search in should but how will these relate with or between them? I mean if it finds Honda and car or mobile and iPhone then it needs to show both results right?

How will I build a query to search for the above-mentioned situation?
I have an index name - fulltext, where fields are headline, fulltext, and subtitle as I need to search keywords within these fields.
document structure -

{
    "_id":{
        "$oid":"6368ca3fcb0c042cbc5b198a"
    },
    "articleid":"159447148",
    "headline":"News of the day!",
    "subtitle":"",
    "fulltext":"sony will partnership with Honda in making cars"
}

Hi @Utsav_Upadhyay2 , can you share your index definition? Have you tried using an index with dynamic mapping and searching with a wildcard query path?
Index definition:

  "mappings": {
    "dynamic": true
  }
}

Query:

[
  {
    "$search": {
      "index": "default",
      "text": {
        "query": "string",
        "path": {
          "wildcard": "*"
        }
      }
    }
  }
]

I already have dynamic indexing name - fulltext, my question was - how will you search with this combination below

  1. Honda and car or mobile and iPhone

This means either a document has text containing- Honda and car together or mobile and iPhone.

Please see the example below which I believe matches what you have described above. 5 sample documents in which 4 match the above description:

[
  {
    headline: 'News of the day!',
    subtitle: '',
    fulltext: 'sony will partnership with Honda in making cars'
  },
  {
    headline: 'News of the day!',
    subtitle: 'mobile iPhones test',
    fulltext: 'this is just random text '
  },
/// the one document below this contains only honda and mobile
  {
    headline: 'nothing',
    subtitle: 'mobile only',
    fulltext: 'honda only'
  },
  {
    headline: 'cars here',
    subtitle: 'honda here',
    fulltext: 'test only'
  },
  {
    headline: 'iphones here',
    subtitle: 'mobile here',
    fulltext: 'test 2 only'
  }
]

The $search stage used:

  {
    '$search': {
      index: 'compoundindex',
      compound: {
        should: [
          {
            compound: {
              must: [
                { text: { query: 'honda', path: { wildcard: '*' } } },
                { text: { query: 'cars', path: { wildcard: '*' } } }
              ]
            }
          },
          {
            compound: {
              must: [
                { text: { query: 'iphones', path: { wildcard: '*' } } },
                { text: { query: 'mobile', path: { wildcard: '*' } } }
              ]
            }
          }
        ]
      }
    }
  }

Output:

[
/// The document contains "car" AND "honda"
  {
    headline: 'cars here',
    subtitle: 'honda here',
    fulltext: 'test only'
  },
/// The document contains "iphone" AND "mobile"
  {
    headline: 'iphones here',
    subtitle: 'mobile here',
    fulltext: 'test 2 only'
  },
/// The document contains "car" AND "honda"
  {
    headline: 'News of the day!',
    subtitle: '',
    fulltext: 'sony will partnership with Honda in making cars'
  },
/// The document contains "iphone" AND "mobile"
  {
    headline: 'News of the day!',
    subtitle: 'mobile iPhones test',
    fulltext: 'this is just random text '
  }
]

I have only tested this on the 5 sample documents mentioned above and if you believe it suits your use case please test thoroughly and and adjust the search query accordingly to verify it meets your requirements.

You can refer to the nested example as well for the compound operator.

Regards,
Jason

2 Likes