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

Open
michaellwest opened this issue Jul 5, 2019 · 6 comments

Comments

3 participants
@michaellwest
Copy link
Member

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

This comment has been minimized.

Copy link
Member Author

commented Jul 8, 2019

Supported scenarios:

  • Criteria
  • Predicate
  • Scope Query
  • Where Values
@AlexKasaku

This comment has been minimized.

Copy link
Contributor

commented Jul 8, 2019

This is really nice!

@michaellwest

This comment has been minimized.

Copy link
Member Author

commented Jul 8, 2019

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

@peplau

This comment has been minimized.

@michaellwest

This comment has been minimized.

Copy link
Member Author

commented Jul 12, 2019

@peplau I got the predicate working!

@peplau

This comment has been minimized.

Copy link

commented Jul 12, 2019

@michaellwest michaellwest added this to In progress in Road to Symposium Jul 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.