Elasticsearch collapse not working with search_after with single sort field and PIT

533 Views Asked by At

I have an Elastic query that initially returns results. When I attempt the query again using search_after for paging, I am getting the error: Cannot use [collapse] in conjunction with [search_after] unless the search is sorted on the same field. Multiple sort fields are not allowed. So far as I can tell, I am sorting and collapsing using just a single field per_id. Is my query structured incorrectly or is there something else I need to do to get this query to run?

GET /_search
{
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "pform": "iphone"
                }
            }]
        }
    },
    "collapse": {
        "field": "per_id"
    },
    "pit": {
        "id": "g-ABCDDEFG12345678ABCDDEFG12345678==",
        "keep_alive": "5m"
    },
    "sort": [ 
      {"per_id": "asc"}
    ],
    "search_after" : [
      "ABCDDEFG12345678",
      123456
    ]
}
1

There are 1 best solutions below

0
Reese Allison On

I needed to exclude the tie breaker in my search_after. It shouldn't cause duplicates because I am using a PIT and sorting on the collapse field, meaning duplicates shouldn't exist in the my result set.

"search_after" : [
  "ABCDDEFG12345678"
]

So I needed to remove the tiebreaker returned from the previous result before passing it into the next one