Duaa Abu Gharbieh
Duaa Abu Gharbieh

Reputation: 41

Sitecore PowerShell fast Search in Script

I used Sitecore PowerShell Extensions module to create script, that copy the relation from one item to the related item.

I use Get-ChildItem to get all the items which will have relation on specific fields

Get-ChildItem -Recurse . | Where-Object {
    $_.TemplateName -match $'myTemplate' -and
    $_.Fields[$fromfield].Value -ne $null -and
    $_.Fields[$fromfield].Value -ne ""
} | ForEach-Object {
}

I took about 1 min to fetch all the items because the data is big.

So I tried to use Find-Item to make the search process faster

Find-Item -Index 'sitecore_mastre_index' -Where 'TemplateName = @0' -WhereValues 'myTemplate'

It gave me below warning, note that I use Sitecore Version 7.2

WARNING: The parameter Where is not supported on this version of Sitecore due to platform limitations.

This parameter is supported starting from Sitecore Version 7.5

Is there a way to retrieve the data using PowerShell in way faster than using Get-ChildItem?

Note: if I use Get-Item . the query returns only the first 100 items. I have many more items.

Upvotes: 3

Views: 3074

Answers (1)

Coding101
Coding101

Reputation: 551

There are a few things to consider.

Example: Get-ChildItem

# Essentially touches all of the items in the database. 
# It's one of the most common ways to query the items, 
# but should be a narrow path.

Get-ChildItem -Path "master:\" -Recurse

Example: Find-Item

# This example is what you need to query the index.
# You can chain together multiple Criteria by making a comma separated list of hashtables.
# Piping to the Initialize-Item command will convert SearchResultItem to Item.
# PS master:\> help Find-Item -Examples 

Find-Item -Index sitecore_master_index -Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"} | Initialize-Item

Example: Get-Item with fast query

# This example takes advantage of the fast query. Substitute for your use case.

$query = "fast:/sitecore//*[@@templatename='Sample Item']"
Get-Item -Path "master:" -Query $query

The book we've put together may also prove beneficial. https://www.gitbook.com/book/sitecorepowershell/sitecore-powershell-extensions/details

Upvotes: 5

Related Questions