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?
Note that I also posted this question on SO here: How can I search in arrays of integers with a compound MongoDB Atlas search query? - Stack Overflow