65.9K
CodeProject 正在变化。 阅读更多。
Home

在 SharePoint 2013 搜索的内容处理管道中使用内容丰富功能调用 WCF

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2013年3月14日

CPOL

3分钟阅读

viewsIcon

17263

如何在内容丰富管道中,使用外部 SOAP 服务调用来处理抓取的数据。

引言

SharePoint 2013 (RTM) 搜索相比于其前身 SharePoint 2010 搜索进行了大量的改进。在 SharePoint 2013 中,我们可以发现“快速搜索”已集成到“SharePoint 2010”搜索平台中,从而提供出色的搜索体验,且问题更少。它拥有 SharePoint 2010 搜索的基础,以及快速搜索的魅力。

背景

几天前,我遇到一个需求,需要在索引发生之前操作抓取的数据。在 SharePoint 2013 搜索中,我们找到了一个“内容处理”管道,其中提供了一种调用外部 SOAP 服务(在我的例子中,我使用了 WCF)来操作原始抓取数据,并将其放回内容处理管道进行处理的方式,按照定义的步骤进行。

使用代码

我的需求是考虑那些“位置”属性标记为“加尔各答”的记录,并在有人针对“加尔加塔”进行查询时,使其可搜索。因此,我的目标是为那些“位置”值设置为“加尔各答”的记录(项目)的“位置”字段添加“加尔加塔”的值。我将在下面给出从内容处理管道调用外部 WCF 的核心要求。请按照下图了解可以在哪里调用 WCF(SOAP Web 服务)。

开发步骤如下

  1. 从 VS 2012 创建一个 WCF 应用程序,并添加对 Microsoft.office.server.search.contentprocessingenrichment.dll 的引用,该文件可以在 C:\\program files\Microsoft office servers\15.\Search\Application\External 中找到。
  2. 删除默认接口(例如,IService1)。
  3. 将以下引用添加到 Service1.svc.cs 文件
    • Microsoft.office.server.search.contentprocessingenrichment
    • Microsoft.office.server.search.contentprocessingenrichment.PropertyTypes
  4. Service1.svc.cs 文件中继承 Icontentporcessingenrichmentservice
  5. 实现 ProcessItem 方法。这是您获取每个项目所需属性的方法。
  6. 以下是实现 ProcessItem 的示例代码。
  7. private const string LocationProperty = "Location";
           
    // Defines the error code for managed properties with an unexpected type.
    private const int UnexpectedType = 1;
    // Defines the error code for encountering unexpected exceptions.
    private const int UnexpectedError = 2;
    private readonly ProcessedItem processedItemHolder = new ProcessedItem
    {
        ItemProperties = new List<AbstractProperty>()
    };  
     public ProcessedItem ProcessItem(Item item)
    {
       processedItemHolder.ErrorCode = 0;
       processedItemHolder.ItemProperties.Clear();
       var LocationProperty = item.ItemProperties.Where(p => p.Name == "Location").FirstOrDefault();
       Property<List<string>> LocProp = LocationProperty as Property<List<string>>;
       // WriteLog("previous step");
       if (LocProp != null && LocProp.Value.Count > 0)
       {
            //WriteLog("second step");
            string[] propValues = LocProp.Value.First().Split(';');
            if (propValues.Length > 0)
            {
    
                strLocation = propValues[0];
            }
            // WriteLog("zipcode is " + strzipcode);
            string  locname = strLocation.Trim();
            if(locname.ToUpper() == "CALCUTTA")
            {
                LocProp.Value.Add("KOLKATA");
                processedItemHolder.ItemProperties.Add(LocProp);
            }
       } 
    }
  8. web.config 文件中的 <system.servicemodel> 中添加以下内容
  9. <bindings>
       <basicHttpBinding>
       <!-- The service will accept a maximum blob of 8 MB. -->
          <binding maxReceivedMessageSize = "8388608"> 
             <readerQuotas maxDepth="32"
              maxStringContentLength="2147483647"
              maxArrayLength="2147483647"   
              maxBytesPerRead="2147483647"   
              maxNameTableCharCount="2147483647" />  
                 <security mode="None" />
          </binding>
       </basicHttpBinding>
    </bindings>
  10. 将此 ECF 托管到 IIS(创建一个虚拟目录。将此映射到 WCF 应用程序的物理路径。右键单击虚拟目录,然后单击“转换为应用程序”)。
  11. 浏览并获取托管 .svc 文件的 URL。
  12. 执行以下 PowerShell 脚本,将“内容丰富”映射到托管的自定义 WCF。
  13. $ssa = Get-SPEnterpriseSearchServiceApplication
    $config = New-SPEnterpriseSearchContentEnrichmentConfiguration
    $config.Endpoint = http://Site_URL/<service name>.svc
    $config.InputProperties = "Location"
    $config.OutputProperties = "Location"
    $config.SendRawData = $True
    $config.MaxRawDataSize = 8192
    Set-SPEnterpriseSearchContentEnrichmentConfiguration –SearchApplication
    $ssa –ContentEnrichmentConfiguration $config
  14. 对内容源执行完全抓取。

关注点

我在这里考虑了 Location 属性,它是 SharePoint 搜索中的默认托管属性,但是如果您想处理一个非常独特的属性名称,则需要先抓取内容源,在抓取属性部分中找到您的属性名称,它可能是“ows_<您的自定义字段名称>”,然后创建您的字段名称的托管属性,并将此托管属性映射到“ows_<您的自定义字段名称>”属性,以便在内容处理管道中使用。

这是内容丰富调用的一部分,我将在后续文章中发布更高级的内容。

© . All rights reserved.