
When using the GraphQL Search query in Sitecore AI, you might notice that the search results include multiple versions of the same item when querying through the Preview API.
This doesn’t happen in Live since it has only one published version.
Example if a Item has 10 versions , the search query can return all the 10 versions with same Item ID.
This results in duplicate records being processed and sometimes paging needs to be applied even if its not required.
Sitecore provides a way to handle this using _latestversion field in GraphQL serach query.
For Example-
I have 2 items Product 1 and Product 2 with 2 version’s of each item.

Search query to get this list of products in preview-
# Write your query or mutation here
query SearchQuery($pageSize: Int = 10) {
search(
where: {
AND: [
{
name: "_path"
value: "{9B4410A8-9344-4385-AFF8-9D99DAC77557}"
operator: CONTAINS
}
]
}
first: $pageSize
) {
total
results {
id
name
version
url {
path
}
}
}
}
Reuslts- You can see multiple version’s of product 1 and 2 are fetched as highlighted with same id.
{
"data": {
"search": {
"total": 5,
"results": [
{
"id": "CB881FD619AB4F3A9E724EB028562635",
"name": "Product 1",
"version": 2,
"url": {
"path": "/Global/Product-1"
}
},
{
"id": "CFE698891E3841E8BADA71842EBBAD56",
"name": "Product 2",
"version": 2,
"url": {
"path": "/Global/Product-2"
}
},
{
"id": "CFE698891E3841E8BADA71842EBBAD56",
"name": "Product 2",
"version": 1,
"url": {
"path": "/Global/Product-2"
}
},
{
"id": "CB881FD619AB4F3A9E724EB028562635",
"name": "Product 1",
"version": 1,
"url": {
"path": "/Global/Product-1"
}
},..............
This will cause issue processing data in FE or wherever consumed. If you need the latest verison of each item use _latestversion criteria in query as below-
query SearchQuery($pageSize: Int = 10) {
search(
where: {
AND: [
{
name: "_path"
value: "{9B4410A8-9344-4385-AFF8-9D99DAC77557}"
operator: CONTAINS
}
{
name: "_latestversion"
value: "true"
}
]
}
first: $pageSize
) {
total
results {
id
name
version
url {
path
}
}
}
}
Result should show only the latest version of each item in Sitecore-
{
"data": {
"search": {
"total": 3,
"results": [
{
"id": "CB881FD619AB4F3A9E724EB028562635",
"name": "Product 1",
"version": 2,
"url": {
"path": "/Global/Product-1"
}
},
{
"id": "CFE698891E3841E8BADA71842EBBAD56",
"name": "Product 2",
"version": 2,
"url": {
"path": "/Global/Product-2"
}
},
{
"id": "9B4410A893444385AFF89D99DAC77557",
"name": "Global",
"version": 1,
"url": {
"path": "/Global"
}
}
]
}
}
}
Using _latestversion in the GraphQL Search query is a simple way to retrieve only the latest version of an item instead of getting multiple versions of the same item.
This helps avoid duplicate records and reduces the need to implement additional filtering logic in the headless application.
It is particularly useful when building content listings, product searches, and other scenarios where only the latest version of each item is required























































