I am not getting the accepted result from autocomplete edge gram

Hi,
I am creating a search engine with the help of mongo Atlas search,
This is the mapping I am using for $search in aggregation query,

{
   "analyzer":"lucene.standard",
   "searchAnalyzer":"lucene.standard",
   "mappings":{
      "dynamic":true,
      "fields":{
         "bestProduct":{
            "fields":{
               "productName":[
                  {
                     "analyzer":"lucene.simple",
                     "searchAnalyzer":"lucene.simple",
                     "type":"string"
                  },
                  {
                     "analyzer":"lucene.standard",
                     "maxGrams":3,
                     "minGrams":15,
                     "tokenization":"edgeGram",
                     "type":"autocomplete"
                  }
               ]
            },
            "type":"document"
         },
         "brandName":[
            {
               "analyzer":"lucene.simple",
               "searchAnalyzer":"lucene.simple",
               "type":"string"
            },
            {
               "type":"autocomplete"
            }
         ]
      }
   }
}

and this is the aggregation query

{
   "$search":{
      "compound":{
         "should":[
            {
               "term":{
                  "query":"organic",
                  "path":[
                     "bestProduct.productName"
                  ],
                  "score":{
                     "boost":{
                        "value":10
                     }
                  }
               }
            },
            {
               "autocomplete":{
                  "query":"organic",
                  "path":"bestProduct.productName",
                  "score":{
                     "boost":{
                        "value":50
                     }
                  }
               }
            }
         ]
      }
   }
}

I am getting this as output

{
   "bestProduct":{
      "productName":"Samisha organic"
   }
},
{
   "bestProduct":{
      "productName":"childern organic"
   }
},
{
   "bestProduct":{
      "productName":"organic"
   }
}

I want to know, how can I get the matching word first, and also if the search_key = “organ” still I want products with a name starting with “organ*” shows first, Can anyone help?

Hi @Nikhil_Anand1 - Welcome to the community :wave:

I tried using this index definition and noticed that "minGrams" was larger than "maxGrams". Are these values you entered a typo? I.e. They are supposed to be reversed?

I ran a simple test with the same query and had the document where "bestProduct.productName":"organic" appeared first (with the highest score). Please see the query ran but with an extra projection stage to indicate scoring:

DB> db.organic.aggregate([
{
  '$search': {
    compound: {
      should: [
        {
          term: {
            query: 'organic',
            path: [ 'bestProduct.productName' ],
            score: { boost: { value: 10 } }
          }
        },
        {
          autocomplete: {
            query: 'organic',
            path: 'bestProduct.productName',
            score: { boost: { value: 50 } }
          }
        }
      ]
    }
  }
},
{
  '$project': { bestProduct: 1, _id: 0, score: { '$meta': 'searchScore' } }
}

Output:

[
  { bestProduct: { productName: 'organic' }, score: 9.29643726348877 },
  {
    bestProduct: { productName: 'Samisha organic' },
    score: 8.128897666931152
  },
  {
    bestProduct: { productName: 'childern organic' },
    score: 8.128897666931152
  }
]

To further assist you with this, can you provide the following information:

  1. Clarification on the index details regarding the "minGrams" and "maxGrams" values you have provided as mentioned at the top of this reply.
  2. Status of the Index being used for this query. E.g. “Active”, “Failed”:

image

  1. Output from the aggregation you have used with the addition of { '$project': { bestProduct: 1, _id: 0, score: { '$meta': 'searchScore' } } } at the end (as per my example above).
  2. The index name. I noticed the query you provided did not specify the index name. Could it be that you have another index called "default" which the query is using?
  3. MongoDB version in use

I tried to use the index you provided and was returned with:

Your index could not be built: "mappings.fields.bestProduct.fields.productName[1]" minGrams cannot be greater than maxGrams. You are still using your last valid index.

Note specifically the following: "You are still using your last valid index."

You could be getting the results you’ve specified since the index you provided is actually not in use. I swapped the "minGrams" and "maxGrams" values you had specified in my own test above before running my $search.

Regards,
Jason

1 Like