How to query on object stored as hash in $search

Hi all,
Data - Consider there is a User collection having documents as follows:-

{
  _id: 'bson_id',
  name: 'Jon',
  groups: {
    ids: [101, 104, 105]
    names: ['Sales UK', 'Head of Sales', 'UK people'],
    count: 3
  }
}
{
  _id: 'bson_id',
  name: 'James',
  groups: {
    ids: [234, 105, 110]
    names: ['Sales US', 'Head of Marketing', 'US people'],
    count: 3
  }
}

There is User information with the details of groups they are part of. Eg: Jon is part of Sales UK group which has ID - 101 in Groups Collection.

Questions:

  1. I want to get users which belong to 101, 105, 110 groups. I have added dynamic index and want to execute below query
[
  {
    '$search': {
      'compound': {
        'filter': [
          {
            'text': {
              'path': 'groups.ids',
              'query': [101, 105, 110] # not accepted
            }
          }
        ]
      }
    }
  }
]

But it is giving error

Remote error from mongot :: caused by :: "compound.filter[0].text.query[0]" must be a string

If I change this integer array to a string array it is working.
So is it not possible to query on an integer array or is it not supported in $search??

  1. I want to get users belonging to the ‘Sales’ group. but above 2 objects are also returned when there was dynamic indexing as it uses Standard analyzer which is not acceptable as per our requirement.
    I tried creating Static indexing as below:-
{
  "mappings": {
    "dynamic": false,
    "fields": {
      "enrolled_school_teams": {
        "fields": {
          "names": {
            "analyzer": "lucene.keyword",
            "searchAnalyzer": "lucene.keyword",
            "type": "string"
          }
        },
        "type": "document"
      }
    }
  }
}

It is now not showing any data for ‘Sales’. But it is also not showing any results when for ‘Sales UK’ term is searched as well. I suppose as Keyword analyzer will not work with strings having space.
So what could be the possible solution for this
Is there any changes will be required in terms of indexing or in search query?

Hey @Viraj_Chheda,

So you need to find all documents that have all these three group IDs or at least of them? The equals operator might suit your use case. Please check and let us know if this helps or if you’re looking for something else.

Can you please share your query and the output that you are expecting along with it? Also, I noticed that in the index definition plan you shared, it says enrolled_school_teams while in the sample document, the field name is groups. Kindly check if all field names used are correct or not.

Regards,
Satyam

1 Like

Thanks, for the response @Satyam.

Yes, but if there are many ids then that many no.of equals block will be required right?

My bad it is groups and not enrolled_school_teams
It is working as expected.

Hey @Viraj_Chheda,

The value field does not take an array of numbers. You can try using the compound with equals and see if they can help you in your use case. You can read more about these from the documentation:
Compound
Equals

Regards,
Satyam

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.