Aggregate search query not working on index type "number"

Hello, I need some help with a query I’m trying to do, I hope you can find what’s wrong here :crossed_fingers:. For info, we are using Mongo Atlas Search

What I had : A collection with an index on multiple string fields, with a working aggregate query on it.
What I want : To include in this index a “number” type field (int32) so I can use the same search bar to search an item by number, instead of name or description.

I looked at the documentation and updated the index, but my query never returns anything. The query works if I search for something in others fields as in Name.fr-CA or Description.fr-CA, but not in Number.

An example of data, my collection contains many items as this one :

{
“_id” : ObjectId(“010000000000000000000003”),
“Description” : {
“fr-CA” : “Un lot de test”,
“en-CA” : “A test item”
},
“Name” : {
“fr-CA” : “Lot de test”,
“en-CA” : “Test item”
},
“Number” : 345,
“Partners” : ,
}

The default index of the collection :

{
“mappings”: {
“dynamic”: false,
“fields”: {
“Description”: {
“fields”: {
“en-CA”: {
“analyzer”: “lucene.english”,
“searchAnalyzer”: “lucene.english”,
“type”: “string”
},
“fr-CA”: {
“analyzer”: “lucene.french”,
“searchAnalyzer”: “lucene.french”,
“type”: “string”
}
},
“type”: “document”
},
“Name”: {
“fields”: {
“en-CA”: {
“analyzer”: “lucene.english”,
“searchAnalyzer”: “lucene.english”,
“type”: “string”
},
“fr-CA”: {
“analyzer”: “lucene.french”,
“searchAnalyzer”: “lucene.french”,
“type”: “string”
}
},
“type”: “document”
},
“Number”: [
{
“representation”: “int64”,
“type”: “number”
}
],
“Partners”: {
“fields”: {
“Name”: {
“type”: “string”
}
},
“type”: “document”
}
}
}
}

And finally the query I’m trying to do. I’ll need to generate this in C#, but for now I’m trying directly with mongoShell
db.[myDB].aggregate([{ $search: { "index": "default", "text": { "query": "345", "path": ["Number"]}}}])

Does anybody sees what I’m missing ? Hope you can help ! Thanks :slight_smile:

1 Like

When you put a number in quotes, it is interpreted as a string rather than a number.

1 Like

Yes I know that, but is there a way to make it work ? “query” only accepts string and it’s ok because I can research by any keyword, but I would like to be able to search a number too. I tried to index the “Number” field as string, but if the field in the document is still an int32 it does not work. The only way it worked is with a field “NumberString” and an index type “string” on this field.

Did you found a solution to your problem ?

To avoid using a $match with a $search, I’am filtering with a $range gte=lte=345.

Not very elegant but it works. Is there a better way ?

I think I found it guys. This seems to be both the problem and solution: https://www.mongodb.com/docs/atlas/atlas-search/tutorial/query-date-number-fields/

Did you do like?

{
        range: {
            path,
            gte: keyword,
            lte: keyword
        }
}