Home:ALL Converter>How can I search in arrays of integers with a compound MongoDB Atlas search query?

How can I search in arrays of integers with a compound MongoDB Atlas search query?

Ask Time:2020-10-02T22:32:41         Author:Billybobbonnet

Json Formatter

I am working on a function that helps me find similar documents, sorted by score, using the full-text search feature of MongoDB Atlas.

I set my collection index as "dynamic".

I am looking for similarities in text fields, such as "name" or "description", but I also want to look in another field, "thematic", that stores integer values (ids) of thematics.


Example:

Let say that I have a reference document as follows:

{
 name: "test",
 description: "It's a glorious day!",
 thematic: [9, 3, 2, 33]
}

I want my search to match these int in the thematic field and include their weight in the score calculation.

For instance, if I compare my reference document with :

{
 name: "test2",
 description: "It's a glorious night!",
 thematic: [9, 3, 6, 22]
}

I want to increase the score since the thematic field shares the 9 and 3 values with the reference document.


Question:

What search operator should I use to achieve this? I can input array of strings as queries with a text operator but I don't know how to proceed with integers.

Should I go for another approach? Like splitting the array to compare into several compound.should.term queries?


Edit:

After a fair amount of search, I found this here and here:

Atlas Search cannot index numeric or date values if they are part of an array.

Before I consider to change the whole data structure of my objects, I wanted to make sure that there is no workaround.

For instance, could it be done with custom analyzers?

Author:Billybobbonnet,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64173357/how-can-i-search-in-arrays-of-integers-with-a-compound-mongodb-atlas-search-quer
yy