Can't Retrive the Filter results with $compound Operator


Sample DATA:

{
  "_id": 1,
  "title": "Redmi 6 Pro - Red",
  "ram": "4 GB"
},
{
  "_id": 2,
  "title": "Redmi large One",
  "ram": "1 GB"
},
{
  "_id": 3,
  "title": "Redmi Note 8",
  "ram": "4 GB"
},
{
  "_id": 4,
  "title": "Redmi diamond colour",
  "ram": "2 GB"
}

I’ve created an index with dynamic Mapping ON.

My aggregation:

[{
    '$search': {
      'index': 'default', 
      'compound': {
        'must': [
          {
            'text': {
              'query': 'redmi',  // searching redmi on field "title"
              'path': 'title'
            }
          }
        ], 
        'filter': {
          'text': {
            'query': '4 GB',  // docs which have "4 GB" ram only
            'path': 'ram'
          }
        }
      }
    }
  }]

I wanted those documents which have only “4 GB” ram


Like this (Expected Output): :point_down:

{
  "_id": 1,
  "title": "Redmi 6 Pro - Red",
  "ram": "4 GB"
},
{
  "_id": 3,
  "title": "Redmi Note 8",
  "ram": "4 GB"
}

But, I’m getting all my documents returned.
Like this:

{
  "_id": 1,
  "title": "Redmi 6 Pro - Red",
  "ram": "4 GB"
},
{
  "_id": 2,
  "title": "Redmi large One",
  "ram": "1 GB"  
},
{
  "_id": 3,
  "title": "Redmi Note 8",
  "ram": "4 GB"
},
{
  "_id": 4,
  "title": "Redmi diamond colour",
  "ram": "2 GB"    
}

Please Make Sure:

ram:"4GB" :x:
ram:"4 GB" :heavy_check_mark:

How to fix this ?
Thanks :wink:

If you want exact match, you should index the ram field with lucene.keyword.

1 Like

I’ve almost 40-60 fields of a total of 10 different categories. I want to get an exact match on it. Do I need to index all of those fields? Any else solutions?