您的位置:首页 > 其它

MSDN Visual系列:MOSS企业级搜索之二——在高级搜索页面中创建并暴露托管属性

2007-06-10 23:58 567 查看
原文:http://msdn2.microsoft.com/en-us/library/bb428648.aspx

SharePoint站点中的列表和文档库都具有其特定专有的属性。当爬网器索引这些容器中的内容时,这些定制的元数据都会被搜集起来。管理员可以把这些自定义的元数据暴露给用户,以便可以在搜索中心中用于检索。在高级搜索页面中有一个属性选取器,可以选取这些托管属性。
我们可以在高级搜索页面的属性选取器中添加一个托管属性,只需要扩展一下附加在选取属性的高级搜索框上的XML即可。PropertyDef元素用于注册托管属性,PropertyRef元素作为ResultType元素的子节点用于使该属性可见。

用于添加一个托管属性到属性选取器的XML

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<LangDefs>
<LangDef DisplayName="Arabic" LangID="1"/>


</LangDefs>
<Languages>
<Language LangRef="12"/>

</Languages>
<PropertyDefs>
<PropertyDef Name="Path" DataType="text" DisplayName="URL"/>

<PropertyDef Name="ProjectCode" DataType="text"
DisplayName="Project Code"/>
</PropertyDefs>
<ResultTypes>
<ResultType DisplayName="All Results" Name="default">
<Query/>
<PropertyRef Name="Author" />

<PropertyRef Name="ProjectCode" />
</ResultType>
<ResultType DisplayName="Documents" Name="documents">
<Query>IsDocument=1</Query>
<PropertyRef Name="Author" />

</ResultType>
<ResultType DisplayName="Word Documents" Name="worddocuments">
<Query>FileExtension='doc' Or FileExtension='docx'
Or FileExtension='dot'</Query>
<PropertyRef Name="Author" />
<PropertyRef Name="Company"/>

</ResultType>

</ResultTypes>
</root>
用于托管属性编程的引用和命名空间

我们必须使下面的样例代码运行在装有MOSS的服务器上。该代码需要引用到Microsoft.SharePoint.dll,Microsoft.Office.Server.dll,Microsoft.Office.Server.Search.dll和System.Web.dll。添加下列的namespace:

using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;
连接到共享服务SSP(Shared Services Provider)和搜索上下文

在编程访问受管理属性前,我们必须先引用SSP和搜索上下文。下面的代码展示了该过程。本例假设共享服务的名称为“SharedServices1”。

private ServerContext serverctx = null;
private SearchContext searchctx = null;
private void Form1_Load(object sender, EventArgs e)
{
serverctx = ServerContext.GetContext("SharedServices1");
searchctx = SearchContext.GetContext(serverctx);
}
获取列表的托管属性

托管属性是Schema类的实例 。该类包含一个名为AllManagedProperties的属性,可以通过循环读取并显示所有独立的ManagedProperty实例。GetMappings方法调用后会得到每个已爬网属性对应的托管属性。

Schema schema = new Schema(this.searchContext);
foreach (ManagedProperty prop in schema.AllManagedProperties)
{
TreeNode node = treeViewManagedProperties.Nodes.Add(prop.Name);
node.Tag = prop;
foreach (Mapping mapping in prop.GetMappings())
{
node.Nodes.Add(mapping.CrawledPropertyName);
}
}
获得列表的爬网属性

我们可以通过Schema类的方法QueryCrawledProperties,得到所有的已爬网属性。由于这将是一个巨大的列表,因此这里也提供了参数用于过滤和约束由QueryCrawledProperties返回的属性的数量。

listBoxCrawledProperties.Items.Clear();
listBoxCrawledProperties.DisplayMember = "Name";
foreach (CrawledProperty cprop in
schema.QueryCrawledProperties(string.Empty, 1000, Guid.NewGuid(),
string.Empty, true))
{
listBoxCrawledProperties.Items.Add(cprop);
}
创建托管属性

通过调用ManagedPropertyCollection的Create方法,我们可以创建一个托管属性。参数是托管属性名称和将要存储的信息类型。

Schema schema = new Schema(this.searchContext);
schema.AllManagedProperties.Create
(textBoxManagedPropertyName.Text, ManagedDataType.Text);
映射爬网属性到一个托管属性

类MappingCollection的实例可以使映射对象添加到搜索中。每一个都表现为一个从已爬网属性到托管属性的映射。

ManagedProperty prop = (ManagedProperty)
treeViewManagedProperties.SelectedNode.Tag;
MappingCollection mappings = new MappingCollection();
foreach (CrawledProperty cprop in
listBoxCrawledProperties.SelectedItems)
{
mappings.Add(new Mapping
(cprop.Propset, cprop.Name, cprop.VariantType, prop.PID));
}
prop.SetMappings(mappings);
当对SharePoint网站内容进行爬网时,元数据通过爬网器被收集整理并存在属性商店(Store)中(由SSP控制的数据库之一)。在SSP管理站点中,管理员可以将一个或多个已爬网属性映射到一个托管属性。一个托管属性可以被暴露到高级搜索页面或其他我们定义的允许共享的搜索范围中的属性选取器内。

创建一个托管属性

在SSP管理网站的搜索设置中,点“元数据属性映射”
在左侧快速启动区有两个链接,一个是托管属性,用于显示托管属性的列表。还有一个是已爬网属性,用于显示已爬网属性的列表。
在托管属性的列表页面,点新建托管属性来新建一个。我们可以指定一个标题(不允许有空格),一个描述信息,和一个所存储信息的类型。
到已爬网属性的映射,可以通过点“添加映射”,打开已爬网属性选择网页对话框来进行映射。
有一个选项可以允许此属性在多个搜索范围中使用。

图一,用于编程访问和维护托管属性的类



查看视频
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐