Atlas Search Autocomplete on an array of object

Hi

I have an array of objects like

services: [

{
  name: "repair tire"
},
{
   name: "oil change"
}

]

I set a MongoDB atlas full text search with autocomplete on “services.name” but when i search using autocomplete it doest not show anything.

4 Likes

Did you find a solution for this yet? I am experiencing the same kind of problem…

Hello!

Thanks so much for your questions about Atlas Search. Can you please share the sample code of your search query and your index, so I can have a deeper look?

Karen

Hello Karen, we are facing similar kind of issue. Please find details below:

Sample collection data:

{ "name" : "Phani", "phones" : [ { "phoneNumber" : "123456789" } ] }
{ "name" : "Yuva", "phones" : [ { "phoneNumber" : "987654321" } ] }

Autocomplete search index on phoneNumber:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "phones": {
        "fields": {
          "phoneNumber": {
            "tokenization": "edgeGram",
            "type": "autocomplete"
          }
        },
        "type": "document"
      }
    }
  }
}

Query used:

{ $search: {“autocomplete”: {“query”: “1234”,“path”: “phones.phoneNumber”}} }

=> returns no result

I’m facing the same issue. It works on a simple nested document, but not for an array of nested documents. It works with “text” instead of “autocomplete” but that requires the exact string which is not really possible.

Hello everybody, same issue here :frowning: Somebody find a solution ? Please help, because this is very impactful. Thanks in advance.

Please Karen, help us with this topic :pray:

Hello, everyone. Thank you so much for the code and index definitions. You are correct that this does not currently work, but this is a particular use case we are actively working on right now. @Shadoweb_EB, you could include a fuzzy object into the text- but you would have to get close to the end of the string for this to work.
"fuzzy": { "maxEdits": 2 }
So this is not exactly what you need.
Hopefully, we will have a resolution soon.

3 Likes

How much time until it will be supported?

1 Like

Hello Karen, Thanks for your answer. Do you think it’s going to take a long time ? Do you have approximatively an idea on how much time ? Many of us are waiting for a solution to this problem. Also, could you posted here once a solution has been found ? Thank you in advance for your help.

Hello. It should be done this quarter. Please vote on the issue on feedback.mongodb.com, you should be notified when it is worked out. Allow autocomplete search on multiple fields using a wildcard path or by specifying multiple fields in the path – MongoDB Feedback Engine

Karen

6 Likes

Hi, I voted on the linked enhancement request. There aren’t any updates on that ticket, though. Can you provide an update on progress? I would love this feature. Thanks!

1 Like

Yes, and it’d be great if this limitation were mentioned in the docs. I losta bunch of time on this today for nothing. Thanks.

Actually I was wrong it is in the docs, kind of. According to the index definition page (https://docs.atlas.mongodb.com/atlas-search/index-definitions/#array), you can’t even use autocomplete for an array of strings, let alone an array of docs:

“You can’t use the autocomplete type to index fields whose value is an array of strings.”

FWIW, I’ve got an array of ‘tags’ for my use case. Attempting to refactor it as a string that can be searched using autocomplete instead.

Hi, when is it planned to be possible to use autocomplete on array of objects?
Thanks in advance

Hi.
When do you plan to implement autocomplete on arrays?
Thank you.

Hi @Alexander_Wieland and @V_C1 - Welcome to the community.

At this point in time as per the autocomplete documentation:

  • You can’t use the autocomplete type to index fields whose value is an array of strings.
  • You can use the autocomplete type to index string fields inside an array of documents indexed as the embeddedDocuments type

Alex, I believe the second dot point is related to your question. V_C1, if this is also the case for you too (array of documents), then please refer to the docs link. If not, I would create a new topic and advise your use case details including sample documents.

Regards,
Jason

2 Likes

Hi Jason.
What is the point to index something if one cannot search for it?
Are there any plans to implement such a search?
Thank you.

Hi Jason, thanks for your response. Since I had a hard time making it, here is an implementation of this use case that works for me (it might help others):

The search index JSON on mongo Atlas

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "autocomplete"
      },
      "roles": {
        "dynamic": true,
        "type": "embeddedDocuments",
        "fields": {
          "externalId": {
            "type": "autocomplete"
          },
          "phone": {
            "type": "autocomplete"
          }
        }
      }
    }
  }
}

The query

db.entities.aggregate([
  {
    "$search": {
      "compound": {
        "should": [
          {
            "autocomplete": {
              "query": "search query input",
              "path": "name"
            }
          },
          {
            "embeddedDocument": {
              "path": "roles",
              "operator": {
                "compound": {
                  "should": [
                    {
                      "autocomplete": {
                        "path": "roles.externalId",
                        "query": "search query input"
                      }
                    },
                    {
                      "autocomplete": {
                        "path": "roles.phone",
                        "query": "search query input"
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    }
  },
  {
    $addFields: {
      "score": { $meta: "searchScore" }
    }
  }])

3 Likes

Thank you Robin.
This the solution.