Like to apply sorting on given search query param

I have product id list in terms query. I like to get result in same order as provide ids in terms query. Below is my query.

{
    "size": 24,
    "from": 0,
    "sort": [],  
    "query": {
        "bool": {
            "filter": {
                "script": {
                    "script": {
                        "source": "doc['image_src.keyword'].size()!=0",
                        "lang": "painless"
                    }
                }
            },
            "must": [
                {
                    "term": {
                        "product_to_show": {
                            "value": "1"
                        }
                    }
                },
                {
                    "terms": {
                        "product_id": [
                            66421,
                            66418,
                            66410,
                            66404
                        ]
                    }
                },
                {
                    "terms": {
                        "is_plus": [
                            0,
                            2
                        ]
                    }
                },
                {
                    "script": {
                        "script": "doc['gender.keyword'].value == 'f'"
                    }
                }
            ]
        }
    }
}

should return result in same order 66421,66418,66410,66404 but query return in following order 66418, 66421, 66404, 66410.

thanks is Advance

Any suggestion or thought on my above question.

thanks

The only way I can imagine would be by passing the same list of values to a Sort Script and use the position in the table to define the boost value for each doc.

Not sure how you can do that and write the painless script for that.

BTW, I'd put all the must clauses within the filter part as I believe they are just filtering the resultset and not contributing to the score.

Also, I'd avoid doing things like:

            {
                "script": {
                    "script": "doc['gender.keyword'].value == 'f'"
                }
            }

Instead, I'd just use a term query on field gender.keyword with value f. That will be much more efficient.

For this one:

            "script": {
                "script": {
                    "source": "doc['image_src.keyword'].size()!=0",
                    "lang": "painless"
                }
            }

I'd rather compute at index time the image_src.keyword size and add a image_src_empty boolean field so filtering by the size would be much faster.

thanks for the tips. It really helps but I am still looking for a solution for my query.

Thanks

any one I need help on this topic.

I am still looking for a solution

If David’s suggested solution with the sort script did not work it may not be possible unless you sortvthe results after they are retrieved in your application.

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