Friday, February 28, 2014

PowerShell script for creating and mapping managed properties in Search service application in Sharepoint

If you develop search-driven site for Sharepoint 2013 then you often need to configure managed properties in Search service application which then can be used in query conditions. Managed property should be mapped to crawled property created by Sharepoint automatically during the crawl. When create new managed property you may specify its settings:

Property name Description
Type Type of information that is stored in this property. Supported types are:
- Text
- Integer
- Decimal
- Date and Time
- Yes/No
- Double precision float
- Binary
Searchable Enables querying against the content of the managed property.  The content of this managed property is included in the full-text index. For example, if the property is "author", a simple query for "Smith" returns items containing the word "Smith" and items whose author property contains "Smith".
Advanced Searchable Settings Enables viewing and changing the full-text index and weight of the managed property.
Queryable Enables querying against the specific managed property. The managed property field name must be included in the query, either specified in the query itself or included in the query programmatically. If the managed property is "author", the query must contain "author:Smith".
Retrievable Enables the content of this managed property to be returned in search results. Enable this setting for managed properties that are relevant to present in search results.
Allow multiple values Allow multiple values of the same type in this managed property. For example, if this is the "author" managed property, and a document has multiple authors, each author name will be stored as a separate value in this managed property.
Refinable

Yes - active: Enables using the property as a refiner for search results in the front end. You must manually configure the refiner in the web part.
Yes - latent: Enables switching refinable to active later, without having to do a full re-crawl when you switch.
Both options require a full crawl to take effect.

Sortable

Yes - active: Enables sorting the result set based on the property before the result set is returned. Use for example for large result sets that cannot be sorted and retrieved at the same time.
Yes - latent: Enables switching sortable to active later, without having to do a full re-crawl when you switch.
Both options require a full crawl to take effect.

Safe for Anonymous Enables this managed property to be returned for queries executed by anonymous users. Enable this setting for managed properties that do not contain sensitive information and are appropriate for anonymous users to view.
Alias Define an alias for a managed property if you want to use the alias instead of the managed property name in queries and in search results. Use the original managed property and not the alias to map to a crawled property. Use an alias if you don't want to or don't have permission to create a new managed property.
Token Normalization Enable to return results independent of letter casing and diacritics(for example accented characters) used in the query.
Complete Matching Queries will only be matched against the exact content of the property. For example, if you have a managed property "ID" that contains the string "1-23-456#7", complete matching only returns results on the query ID:"1-23-456#7", and not on the queries ID:"1-23" or  ID:"1 23 456 7".
Mappings to crawled properties The list shows all the crawled properties that are mapped to this managed property. A managed property can get its content from one or more crawled properties.
Company name extraction

Enables the system to extract company name entities from the managed property when crawling new or updated items. Afterwards, the extracted entities can be used to set up refiners in the web part.

There is a pre-populated dictionary for company name extraction. The system saves the original managed property content unchanged in the index and, in addition, copies the extracted entities to the managed property "companies". The "companies" managed property is configured to be searchable, queryable, retrievable, sortable and refinable.

Custom entity extraction

Enables one or more custom entity extractors to be associated with this managed property. This enables the system to extract entities from the managed property when crawling new or updated items. Afterwards, the extracted entities can be used to set up refiners in the web part.

There are four types of custom extraction dictionaries. You create your own, separate custom entity extraction dictionaries that you deploy using the PowerShell cmdlet Import-SPEnterpriseSearchCustomExtractionDictionary.

The system saves the original managed property content unchanged in the index and, in addition, copies the extracted entities to the managed properties  "WordCustomRefiner1" through 5, "WordPartCustomRefiner1" through 5, "WordExactCustomRefiner" and/or "WordPartExactCustomRefiner" respectively.

These managed properties are configured to be searchable, queryable, retrievable, sortable and refinable.

It is possible to specify all these settings manually during each installation, but much quicker way will be doing it automatically via PowerShell script. Here is the script which creates new managed properties and maps them to crawled properties (crawled properties should already exist, i.e. you should run search crawler before to run it):

   1:  $category = "SharePoint"
   2:   
   3:  function SetCrawledAndManagedProperties($propertyMappings)
   4:  {
   5:      $propertyMappings | % { 
   6:          Write-Host "Setting crawled property:" $_[0]
   7:          # Check if crawled property exists. If not, create it.
   8:          $crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty
   9:  -Name $_[0] -SearchApplication $searchServiceApplication
  10:          if (!$crawledproperty)
  11:          {
  12:              $message = " - Mapping " + $_[0] + " to " + $_[1]
  13:              Write-Host $message -ForegroundColor Green
  14:              $crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty
  15:  -SearchApplication $searchServiceApplication -Category $category -VariantType $_[4]
  16:  -PropSet $_[2] -Name $_[0] -IsNameEnum $false
  17:              if ([bool]$_[7] -eq $true)
  18:              {
  19:                  $crawledproperty.IsMappedToContents = $false
  20:                  $crawledproperty.update()
  21:              }
  22:          }
  23:          else {
  24:  Write-Host " - Crawled property" $_[0] "already exists, create managed property only."
  25:  -ForegroundColor Yellow
  26:          }
  27:          Write-Host "Setting managed property:" $_[1]
  28:          $managedproperty = Get-SPEnterpriseSearchMetadataManagedProperty
  29:  -Identity $_[1] -SearchApplication $searchServiceApplication -ErrorAction:SilentlyContinue
  30:          if ($managedproperty -eq $null)
  31:          {
  32:              Write-Host " - Creating managed property" $_[1] -ForegroundColor Green
  33:              if ([bool]$_[7] -eq $true)
  34:              {
  35:                  $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty
  36:  -Name $_[1] -SearchApplication $searchServiceApplication -Type $_[3]
  37:  -FullTextQueriable $false -Queryable $false -Retrievable $false -EnabledForScoping $true
  38:  -NameNormalized $true -Safe $true
  39:  -NoWordBreaker $_[5]
  40:              }
  41:              else
  42:              {
  43:                  $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty
  44:  -Name $_[1] -SearchApplication $searchServiceApplication -Type $_[3]
  45:  -FullTextQueriable $true -Queryable $true -Retrievable $true -EnabledForScoping $true
  46:  -NameNormalized $true -Safe $true -NoWordBreaker $_[5]
  47:              }
  48:              $mapping = New-SPEnterpriseSearchMetadataMapping
  49:  -SearchApplication $searchServiceApplication -ManagedProperty $managedproperty
  50:  -CrawledProperty $crawledproperty
  51:              $managedproperty.Refinable = $true
  52:              $managedproperty.Sortable = $true
  53:              $managedproperty.HasMultipleValues = $_[6]
  54:              $managedproperty.update()
  55:          }
  56:          else
  57:          {
  58:              Write-Host " - Managed property" $_[1] "already exists. Skipping."
  59:  -ForegroundColor Yellow
  60:          }
  61:          
  62:          Write-Host ""
  63:      };
  64:   
  65:  }
  66:   
  67:  $stringPropertySetId = "00130329-0000-0130-c000-000000131346"
  68:  $imagePropertySetId = "fea84df6-a0fc-492c-9aa7-d28b8dcb08b3"
  69:   
  70:  # 0= Crawled property name | 1= Managed property name | 2= Propertyset id |
  71:  # 3= Type id | 4= variant type | 5= Do not use word breaker | 6= Has multiple values |
  72:  # 7= ExcludeFromSearch
  73:  $propertyMappings = @(
  74:      ,("ows_FooText", "FooText", $stringPropertySetId, 1, 31, $false, $false, $false)
  75:      ,("ows_FooDate", "FooDate", $stringPropertySetId, 4, 64, $false, $false, $false)
  76:      ,("ows_FooImage", "FooImage", $imagePropertySetId, 1, 31, $false, $false, $false)
  77:      ,("ows_FooMM", "FooMM", $stringPropertySetId, 1, 31, $true, $true, $false)
  78:  )
  79:   
  80:  SetCrawledAndManagedProperties $propertyMappings

This script creates 4 managed properties of 4 different types:

  • FooText – text property
  • FooDate – date time
  • FooImage – image
  • FooMM – managed metadata

Also it maps them to appropriate crawled properties. If crawled properties don’t exist, they will be created too. But as I wrote above the better approach is to make full crawl first and run script after that. In this case Sharepoint will create crawled properties by itself.

You may extend the script for you needs and configure other settings for each property. Hope it will help in your work.

2 comments:

  1. Thanks for the post, nice work! When I tried using it to create a managed property for a choice field, using the "HasMultipleValues" switch gave me this error - "Property 'HasMutipleValues' cannot be found on this object; make sure it exists and is settable."

    Here's the snippet I used:

    $crawledproperty=Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $cat -Name "ows_DocumentType"
    $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name "DocumentType" -Type 1
    New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
    $managedproperty.HasMutipleValues = $true
    $managedproperty.Sortable = $true
    $managedproperty.Update()


    Thoughts?

    ReplyDelete
  2. Ajay,
    try to list all properties of $managedproperty object by using Get-Member cmdlet (http://technet.microsoft.com/en-us/library/ee176854.aspx) and see whether it is called exactly HasMutipleValues.

    ReplyDelete