Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find-Item : Add support for custom SearchResultItem #1120

Closed
michaellwest opened this issue Jul 5, 2019 · 7 comments
Closed

Find-Item : Add support for custom SearchResultItem #1120

michaellwest opened this issue Jul 5, 2019 · 7 comments
Labels
-release-highlight Exciting change that should be highlighted in the release notes and celebrated by SPE fans. area-commands Involves functions and cmdlets. improvement Extra sugar and spice.
Milestone

Comments

@michaellwest
Copy link
Member

michaellwest commented Jul 5, 2019

In the current version, only SearchResultItem can be used. Would like to have a way to provide a custom class with custom properties.

Example of what should work:

Dynamic Query

class TitleSearchResultItem : SearchResultItem
{
   [Sitecore.ContentSearch.IndexField("title")]
   [string]$Title
}

$props = @{
    Index = "sitecore_master_index"
    Where = "Title.Contains(@0)"
    WhereValues = "great"
    QueryType = [TitleSearchResultItem]
}

Find-Item @props

Dynamic Query

class TemplatesSearchResultItem : SearchResultItem
{
   [Sitecore.ContentSearch.IndexField("_templates")]
   [System.Collections.Generic.List[ID]]$TemplateIds
}

$props = @{
    Index = "sitecore_master_index"
    Where = "Paths.Contains(@0) And TemplateIds.Contains(@1)"
    WhereValues = [ID]::Parse("{371EEE15-B6F3-423A-BB25-0B5CED860EEA}"), [ID]::Parse("{B0B6FB08-6BBE-43F2-8E36-FCE228325B63}")
    QueryType = [TemplatesSearchResultItem]
}

Find-Item @props

Scope Query

class TitleSearchResultItem : SearchResultItem
{
   [Sitecore.ContentSearch.IndexField("title")]
   [string]$Title
}

$props = @{
    Index = "sitecore_master_index"
    ScopeQuery = "location:{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9};custom:title|Sitecore"
    QueryType = [TitleSearchResultItem]
}

Find-Item @props | Select-Object -Property Title

Criteria

class TitleSearchResultItem : SearchResultItem
{
   [Sitecore.ContentSearch.IndexField("title")]
   [string]$Title
   [Sitecore.ContentSearch.IndexField("_creator")]
   [string]$Creator
}

$criteria = @(
    @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"}, 
    @{Filter = "Contains"; Field = "Title"; Value = "Sitecore"},
    @{Filter = "Contains"; Field = "_creator"; Value = "admin"}
)
$props = @{
    Index = "sitecore_master_index"
    Criteria = $criteria
    QueryType = [TitleSearchResultItem]
}

Find-Item @props

Predicate

class TitleSearchResultItem : SearchResultItem
{
   [Sitecore.ContentSearch.IndexField("title")]
   [string]$Title
}

$criteriaTemplate = @{Filter = "Equals"; Field = "_templatename"; Value = "Template Field"; }, @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"; Boost=25; }, @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"; }
$predicateTemplate = New-SearchPredicate -Operation Or -Criteria $criteriaTemplate -QueryType ([TitleSearchResultItem])

$criteriaContent = @{Filter = "Contains"; Field = "Title"; Value = 'Sitecore'}
$predicateTitle = New-SearchPredicate -Criteria $criteriaContent -QueryType ([TitleSearchResultItem])

$predicateTemplateAndTitle = New-SearchPredicate -First $predicateTemplate -Second $predicateTitle -Operation And -QueryType ([TitleSearchResultItem])

$root = Get-Item -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"
$criteriaRoot = @{Filter = "DescendantOf"; Value = $root }
$predicateRoot = New-SearchPredicate -Criteria $criteriaRoot -QueryType ([TitleSearchResultItem])

$predicate = New-SearchPredicate -First $predicateRoot -Second $predicateTemplateAndTitle -Operation And -QueryType ([TitleSearchResultItem])

$props = @{
    Index = "sitecore_master_index"
    Predicate = $predicate
    QueryType = [TitleSearchResultItem]
}

Find-Item @props
@michaellwest
Copy link
Member Author

michaellwest commented Jul 8, 2019

Supported scenarios:

  • Criteria
  • Predicate
  • Scope Query
  • Where Values

@AlexKasaku
Copy link
Contributor

This is really nice!

@michaellwest
Copy link
Member Author

Working with Predicate has proven more challenging. Will put that on the backburner for now.

@peplau
Copy link

peplau commented Jul 8, 2019

@michaellwest michaellwest added this to the 6.0 milestone Jul 11, 2019
@michaellwest michaellwest added area-commands Involves functions and cmdlets. improvement Extra sugar and spice. labels Jul 11, 2019
@michaellwest
Copy link
Member Author

@peplau I got the predicate working!

@peplau
Copy link

peplau commented Jul 12, 2019 via email

@michaellwest michaellwest added this to In progress in Road to Symposium Jul 18, 2019
@michaellwest
Copy link
Member Author

Probably should call out that when using Criteria the field names should be what's in the index. Using Dynamic the field names should match with the custom type.

@michaellwest michaellwest moved this from In progress to Done in Road to Symposium Aug 23, 2019
@michaellwest michaellwest added the -release-highlight Exciting change that should be highlighted in the release notes and celebrated by SPE fans. label Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-release-highlight Exciting change that should be highlighted in the release notes and celebrated by SPE fans. area-commands Involves functions and cmdlets. improvement Extra sugar and spice.
Projects
No open projects
Development

No branches or pull requests

3 participants