How to index and search an array in Atlas fulltext search?

I am trying to search in the array, with the range date but it is not working. if I try searching alone in the array it is not working too. though I check I have data according to the search query.

this is my sample data -


{
  "_id" : ObjectId("6401bf4640e29af1625c10a9"),
  "articleid" : "165097155",
  "headline" : "iOS vs. Android: Android Must Consider these iOS Features"
  "article_type" : "online",
  "pubdateRange" : ISODate("2023-03-03T14:55:01.000+0000"),
  "clientidArray" : [ "M0036", "Y0010", "D0382"]
}

The index name is - fulltext, where the headline is indexed as a string, pubdateRange as Date, and clientidArray as a string too

The query I am trying to execute is -

db.getCollection("article_beta").aggregate([
    {
        "$search":{
            "index":"fulltext",
            "compound":{
                "must":[
                    {
                        "range":{
                            "path":"pubdateRange",
                            "gte":"ISODate(""2023-01-01T00:00:00.000Z"")",
                            "lte":"ISODate(""2023-03-15T00:00:00.000Z"")"
                        }
                    },
                    {
                        "text":{
                            "query":"D0382",
                            "path":[
                                "clientidArray"
                            ]
                        }
                    }
                ]
            }
        }
    }
])

Any help is much appreciated.

Hey @Utsav_Upadhyay2,

Is your range search working? Range requires a date or a numeric field as an input while I see you’re giving a string value. I tried to reproduce your problem on my end to check this. Created documents from the sample you provided and the index definition is (with lucene.standard):

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "clientidArray": {
        "type": "string"
      },
      "headline": {
        "type": "string"
      },
      "pubdateRange": {
        "type": "date"
      }
    }
  }
}

When I executed the text search alone, it worked as expected. This is my search:

{
  index: 'default',
  text: {
    query: 'M0036',
    path: 'clientidArray'
  }
}

But when I executed the query you provided, it gave me an error:

Reason: PlanExecutor error during aggregation :: caused by :: 
Remote error from mongot :: caused by :: 
"compound.must[0].range.lte" must be a date, number, or geoPoint

ie. the range operator is unable to identify the dates provided since we are providing string values to it instead of dates. I changed the query to provide date to the range operator:

{
  index: 'default',
  "compound": {
        "must": [{
          "range":{
                            "path":"pubdateRange",
                            "gte":ISODate('2023-03-03T14:55:01.000+00:00'),
                            "lte":ISODate('2023-03-03T14:55:01.000+00:00')
                        }
        }],
        "must":[{
          text: {
           query: 'M0036',
           path: 'clientidArray'
  }
        }]
}
}

and it worked as expected.

Kindly try and see if the above query works for you or not. If this still doesn’t work for you, please check the types of your fields pubdateRange and clientidArray. They should be date and array respectively or please post your index definition as well as any error you may be getting while executing the search.

Regards,
Satyam

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.