Skip to main content

Sitecore

Sitecore Powershell 4: Using ContentSearch API with the Help of Find-Item

How natural language search helps deliver personalization

In previous posts of this blog series, we have seen how we can use Get-Item and Get-ChildItem to fetch items from our Sitecore instance. Get-ChildItem command is very slow if we have a large number of items to fetch. This is the same when we try to fetch a large number of items by using the Sitecore Item API. The use of Sitecore ContentSearch API is highly recommended for this scenario.

The same is correct for Sitecore Powershell. We can use features provided by Sitecore’s ContentSearch API in our Powershell Scripts.

 

Find-Item

Find-Item cmdlet is used to fetch items using ContentSearch API, where we provide the name of the index and filtering conditions before getting the results. The results that are returned are of type SearchResultItem; however, we can leverage the use of another cmdlet that lets us include our custom fields in the result set.

Following are a few parameters that we can use with Find-Item:

  1. -Index: Where we specify the name of the index to use.
  2. -Criteria: Where we specify the Search Filter
  3. -First: Where we can specify the number of results to return
  4. -Skip: Where we can specify the number of results to be skipped before returning the result
  5. -Where: Where we can specify Dynamic Linq to filter results
  6. -WhereValue: Where we can specify Array of values that will be used by -Where Linq statement

Let’s execute one example script to see how Find-Item works:

 

$FilterCriteria = @(

    @{Filter = "Equals"; Field = "_templatename"; Value = "Event Details"},

    @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/Events" }

)




Find-Item -Index "sitecore_master_index" -Criteria $FilterCriteria

 

See that we have specified “sitecore_master_index” for –Index parameter. We have created a Grouping expression and assigned it to $FilterCriteria variable.

Then, we created different Filters of objects inside our Criteria expression. Every Filter has three imp parts: Field, Filter, and Value.

  • Filter: Where we specify the operator for evaluating the condition. Ex- Equals, StartsWith, Contains, EndsWith, LessThan, GreaterThan etc.
  • Field: Where we can specify the name of the indexed field that we want to use in filtering condition.
  • Value: Value of the field for satisfying the condition.

 

Output of the above Script:

Finditeam

 

Above, the script will get the result which has the TemplateName property value as Event Details and their path starts with “/sitecore/content/events” as shown in the image.

 

Fetching Custom Fields

Now, as I have mentioned, the result set will contain an item of type SearchResultItem. We will not get our custom fields if we try to access them.

 

See the below image where I have written a script to display Custom Field DifficultyLevel. It does not show anything.

Fetchingcustomfield1

 

Here, we can use Initialize-Item after Pipe operator in our statement to get custom fields in our results.

Using the following script displayed our custom field in result output:

 

$FilterCriteria = @(

    @{Filter = "Equals"; Field = "_templatename"; Value = "Event Details"},

    @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/Events" }

)




Find-Item -Index "sitecore_master_index" $FilterCriteria | Initialize-Item | Select-Object -Property DifficultyLevel

 

 

Output of above:

Fetchingcustomfield2

Here, we can now see the values for our custom fields. Initialize-Item transforms SearchResultItem type items in the result set to Sitecore Items with all the fields included.

 

Using -First

We can use -First operator to fetch a given number of result items from the result set. Following is the statement:

$FilterCriteria = @(

    @{Filter = "Equals"; Field = "_templatename"; Value = "Event Details"},

    @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/Events" }

)




Find-Item -Index "sitecore_master_index" -Criteria $FilterCriteria -First 2

 

This will only fetch 2 items from the result set:

Firstentry

 

 

Using -Skip Operator

This will skip the number of items based on the value we pass.

$FilterCriteria = @(

    @{Filter = "Equals"; Field = "_templatename"; Value = "Event Details"},

    @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/Events" }

)




Find-Item -Index "sitecore_master_index" -Criteria $FilterCriteria -Skip 2

 

 

Output:

Skipentry

 

 

Using Where and WhereValues

We can use Where and WhereValues to filter our search instead of Criteria. Here we can use proper objects as opposed to Criteria operator that we saw previously. –Where is used to specify Dynamic Linq query and –WhereValues contains array of values.

Following, the script will fetch items based on Templates that have “Event” in their name and which are under “/sitecore/content/events” node.

 

$WhereCondition = 'TemplateName.Contains(@0) And Path.Contains(@2)'

$WhereValues = @("Event", "en","/sitecore/content/Events")

Find-Item -Index "sitecore_master_index" -Where $WhereCondition -WhereValues $WhereValues

 

 

Output of Above Script:

Wherefinditem

 

 

In this blog post, we have seen how we can query Sitecore indexes using ContentSearch API in Sitecore Powershell. This is a fast approach and we can use filters easily for our result.

So far we have seen how to work with Sitecore Items. In the next post, we will learn about how we can work with Sitecore Renderings using different Sitecore Powershell commands.

Thoughts on “Sitecore Powershell 4: Using ContentSearch API with the Help of Find-Item”

  1. Ramakant Chandel Post author

    Thanks Michael for this information. I was just exploring this great tool. I will surely explore new features added in SPE 6.1.1 and will add a blog in this series dedicated to new facet Search.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ramakant Chandel

Ramakant Chandel is working as a Sitecore Professional. He likes to explore challenging and new technical concepts in Sitecore. He is passionate about sharing his knowledge with everyone.

More from this Author

Categories
Follow Us