How to Optimize $sort Operator Performace?

I’ve sample dataset around 20K document. It contains the following fields:

{
 "id": "29dffljd",
 "title": "This is title",
 "price": 12000,
}

Now, I’m running a $search Operator only on field title.

The following aggregation looks like this:

{
    '$search': {
        'compound': {
            'must': [
                {
                    'text': {
                        'query': 'camel',
                        'path': 'title'
                    }
                }
            ]
        }
    }
}

=> Response Time: 80-100 ms


Problem:

When I add a $sort after that stage. I’ve got my result in 500-637 ms, which is too Big. I’m looking for a solution to reduce this to 100ms.

The $sort aggegration:

{
    '$search': {
        'compound': {
            'must': [
                {
                    'text': {
                        'query': 'camel',
                        'path': 'title'
                    }
                }
            ]
        }
    }
}, { '$sort': { 'price': 1 } }

=> Response Time: 500-637 ms.

How do I reduce this time? :thinking:

Hey there! We have a project in motion to improve Search with Sort. Feel free to vote on it here to get updated on progress. In the meantime, I’d suggest using near. Let me know what you think!