MongoDB Atlas Search - full text

i have a collection project which has several fields but i want to perform full text search in to 3 fields - projectName, projectSummery, title. there is one array which has all keywords to be searched in to 3 fields. if keyword is found in any field it should come in to result. it will be great if fuzzy search can also be included.
i have tried with following aggregation but it is not working. only results for last element in array is returned.

const variations = [water, waste, wastewater]
const aggregation = [
          {
            $search: {
              index: "default",
              compound: {
                should: variations.map(term => ({
                  compound: {
                    should: [
                      {
                        text: {
                          query: term,
                          path: "Type",
                          score: { boost: { value: 10 } }
                        }
                      },
                      {
                        text: {
                          query: term,
                          path: "ProjectName",
                          score: { boost: { value: 8 } }
                        }
                      },
                      {
                        text: {
                          query: term,
                          path: "ProjectSummary"
                        }
                      }
                    ],
                    minimumShouldMatch: 1
                  }
                })),
                minimumShouldMatch: 1
              }
            }
          },
          {
            $limit: 40
          }
        ];

Hi @Clima_Champions and welcome to the community forum!!

Based on the above, I tried to create some sample data as:

Atlas atlas-99o4e9-shard-0 [primary] test> db.arrayTest.find()
[
  {
    _id: ObjectId('66587570ac48209c08560f1f'),
    Type: 'Water Management',
    ProjectName: 'Clean Water Initiative',
    ProjectSummary: 'This project focuses on providing clean and safe drinking water to rural areas.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f20'),
    Type: 'Waste Disposal',
    ProjectName: 'Waste Management Solutions',
    ProjectSummary: 'A comprehensive approach to managing and disposing of waste sustainably.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f21'),
    Type: 'Infrastructure',
    ProjectName: 'Urban Wastewater Treatment',
    ProjectSummary: 'Developing advanced wastewater treatment facilities in urban regions.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f22'),
    Type: 'Environmental',
    ProjectName: 'Air Quality Control',
    ProjectSummary: 'Monitoring and improving air quality in metropolitan areas.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f23'),
    Type: 'Sanitation',
    ProjectName: 'Waste Reduction Program',
    ProjectSummary: 'Implementing strategies to reduce waste generation in cities.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f24'),
    Type: 'Water Supply',
    ProjectName: 'Fresh Water Access',
    ProjectSummary: 'Ensuring access to fresh water in remote communities.'
  }
]

with the index created as:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "ProjectName": {
        "type": "string"
      },
      "ProjectSummary": {
        "type": "string"
      },
      "Type": {
        "type": "string"
      }
    }
  }
}

and the query I used looks like the following:

Atlas atlas-99o4e9-shard-0 [primary] test> const variations = ["water", "waste", "wastewater"];

Atlas atlas-99o4e9-shard-0 [primary] test> db.arrayTest.aggregate([ { $search: { index: "default", compound: { should: variations.map((term) => ({ compound: { should: [ { text: { query: term, path: "Type", fuzzy: { maxEdits: 2 }, score: { boost: { value: 10 } } } }, { text: { query: term, path: "ProjectName", fuzzy: { maxEdits: 2 }, score: { boost: { value: 8 } } } }, { text: { query: term, path: "ProjectSummary", fuzzy: { maxEdits: 2 } } }], minimumShouldMatch: 1 } })), minimumShouldMatch: 1 } } }, { $limit: 40 }] ).pretty();
[
  {
    _id: ObjectId('66587570ac48209c08560f24'),
    Type: 'Water Supply',
    ProjectName: 'Fresh Water Access',
    ProjectSummary: 'Ensuring access to fresh water in remote communities.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f20'),
    Type: 'Waste Disposal',
    ProjectName: 'Waste Management Solutions',
    ProjectSummary: 'A comprehensive approach to managing and disposing of waste sustainably.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f1f'),
    Type: 'Water Management',
    ProjectName: 'Clean Water Initiative',
    ProjectSummary: 'This project focuses on providing clean and safe drinking water to rural areas.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f23'),
    Type: 'Sanitation',
    ProjectName: 'Waste Reduction Program',
    ProjectSummary: 'Implementing strategies to reduce waste generation in cities.'
  },
  {
    _id: ObjectId('66587570ac48209c08560f21'),
    Type: 'Infrastructure',
    ProjectName: 'Urban Wastewater Treatment',
    ProjectSummary: 'Developing advanced wastewater treatment facilities in urban regions.'
  }
]

I hope this answers your question. If not please help me with the sample data that you have in your collection and the index definition you are using.

Please do not hesitate to reach out in case of further queries.

Best regards
Aasawari

A post was split to a new topic: MongoDB Atlas Vector Search introduction