Share via


How to: Change the Weight Setting for a Managed Property

You can change the weight applied to a managed property by using the ManagedProperty class in the Enterprise Search Administration object model. This setting is applied when the content is indexed.

To change the weight for a managed property from a console application

  1. In your application, set references to the following DLLs:

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. In your console application's class file, add the following using statements near the top of the code with the other namespace directives.

    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    
  3. Create a function to write out usage information to the console window.

    static void Usage()
    {
      Console.WriteLine("Change Property Weight");
      Console.WriteLine("Usage: PropertyWeightSample.exe ManagedPropertyName <WeightValue>");
      Console.WriteLine("<WeightValue>: Must be formatted as a float.");
    }
    
  4. In the Main() function of the console application, add code to check the number of items in the args[] parameter; it must equal 2. If it does not, then call the Usage() function defined in Step 3.

    if (args.Length != 2)
    {
    Usage();
    return;
    }
    
  5. Retrieve the values specified in the args[] parameter that will be used to specify the name of the managed property to change the weight setting for, and the new weight setting value.

    string strPropertyName = args[0].ToLower();
    float newWeight = Convert.ToSingle(args[1]);
    
  6. To retrieve the Schema object for the search context of the Shared Services Provider (SSP), add the following code. For more information about ways to retrieve the search context, see How to: Return the Search Context for the Search Service Provider.

    /*
    Replace <SiteName> with the name of a site using the SSP
    */
    string strURL = "http://<SiteName>";
    Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
    
  7. Retrieve the collection of managed properties by using the following code.

    ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
    
  8. Determine if the collection of managed properties includes the managed property that you want to change the weight for by checking the value returned for the Contains method. If false, the property does not exist, so write out a message to the console indicating this.

    if (!properties.Contains(strPropertyName))
    {
       Console.WriteLine strPropertyName + " property does not exist.");
       return;
    }
    
  9. If the Contains method returns true, loop through the managed properties, and for each one, check to see if the Name property matches the value of strPropertyName, and if it does, change the Weight property to the specified value.

    foreach (ManagedProperty property in properties)
    {
        if (property.Name.ToLower() == strPropertyName)
        {
            property.Weight = newWeight;
            Console.WriteLine("Weight value changed for " + strPropertyName + " property.");
            Console.WriteLine("NAME: " + property.Name + "  WEIGHT: " + property.Weight.ToString());
        }
    }
    

Example

Following is the complete code for the sample console application.

Prerequisites

  • Ensure a Shared Services Provider is already created.

Project References

Add the following Project References in your console application code project before running this sample:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.Search

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;

namespace PropertyWeightSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 2)
                {
                    Usage();
                    return;
                }

                /*
                Replace <SiteName> with the name of a site using the SSP
                */
                string strURL = "http://<SiteName>";
                Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
                ManagedPropertyCollection properties = sspSchema.AllManagedProperties;

                string strPropertyName = args[0].ToLower();
                float newWeight = Convert.ToSingle(args[1]);

                if (!properties.Contains(strPropertyName))
                {
                    Console.WriteLine(strPropertyName + " property does not exist.");
                    return;
                }

                foreach (ManagedProperty property in properties)
                {
                    if (property.Name.ToLower() == strPropertyName)
                    {
                        property.Weight = newWeight;
                        Console.WriteLine("Weight value changed for " + strPropertyName + " property.");
                        Console.WriteLine("NAME: " + property.Name + "  WEIGHT: " + property.Weight.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Usage();
            }
        }

        private static void Usage()
        {
            Console.WriteLine("Change Property Weight");
            Console.WriteLine("Usage: PropertyWeightSample.exe ManagedPropertyName <WeightValue>");
            Console.WriteLine("<WeightValue>: Must be formatted as a float.");
        }
    }
}

See Also

Reference

Microsoft.Office.Server.Search.Administration.Schema

Concepts

Enterprise Search Relevance Architecture Overview
Getting Started with the Enterprise Search Administration Object Model
Improving Relevance

Other Resources

Programmatically Administering Enterprise Search