查询托管元数据字段值

上次修改时间: 2015年3月9日

适用范围: SharePoint Server 2010

本文内容
CAML 查询
搜索查询
示例

在 Microsoft SharePoint Server 2010 中,在构造旨在返回结果(这些结果表示用托管元数据列中的值标记的项)的查询时务必小心。SharePoint Server 2010 支持从 TermStore 数据库返回已限定范围的元数据值或不限范围的元数据值,无论您是编写协作应用程序标记语言 (CAML) 查询并使用 API(如 SPQuery 对象)获取特定列表中的项,还是使用 API(如 Query)执行搜索查询。

CAML 查询

在编写协作应用程序标记语言 (CAML) 查询以从用托管元数据列中的特定值标记的 SPList 对象返回项时,必须指定要匹配的词的查找 ID。查找 ID 是网站集特定的;对于使用指定词的每个网站集,该词具有不同的查找 ID。若要获取词的查找 ID,可以使用 GetWssIdsOfKeywordTerm() 方法。或者,如果词是一个关键字(如果词的 IsKeyword 属性设置为 true),则也可以使用 GetWssIdsOfKeywordTerm() 方法。在某些方案中,对于同一网站集,单个词可以具有多个查找 ID;例如,对于网站集中使用的每个词(这些词稍后会合并为单个词),合并的词具有一个查找 ID。在找到某个词的查找 ID 后,可以在协作应用程序标记语言 (CAML) 查询中的 IN 子句中组合这些 ID,以查询用该词标记的项。以下是一个协作应用程序标记语言 (CAML) 查询,它可在列表中查询字段 ItemType 中具有查找 ID 14 和查找 ID 15 的项。

<Query><Where><In><FieldRef LookupId="TRUE" Name="ItemType" /><Values><Value Type="Integer">14</Value><Value Type="Integer">15</Value></Where></Query>

可以构建包含多个词的查找 ID 的查询。常见任务是,构建一个用于获取用某个指定词或该词的任意子代词标记的项的查询。通过将 true 传递给 TaxonomyField.GetWssIdsOfKeywordTerm 的 bool includeDescendants 参数,获取带指定 GUID 的词的查找 ID,并获取其所有子词的 ID。

搜索查询

搜索将自动创建与托管元数据字段对应的索引属性,这些字段可加快用于搜索用特定托管元数据值标记的项的查询,如表 1 所述。

表 1. 搜索查询属性

Property

说明

ows_tax_idFieldName

对于每个已爬网项,为 FieldName 字段中的词的 ID 编制索引。此类 ID 将映射到托管属性 owsTaxIdFieldName,使您能够使用指定词 ID 查询该字段中用词标记的已爬网项。

ows_FieldName

对于每个已爬网项,为 FieldName 字段中的词的标签编制索引。此处只包括用于标记项的词的标签;不会为备用标签编制索引。

ows_taxid_MetadataAllTagsInfo

对于每个已爬网项,为其所有元数据字段中的所有词的 ID 编制索引。此属性将映射到托管属性 owsTaxIdMetadataAllTagsInfo,使您能够使用指定词 ID 查询用词标记的已爬网项。

owsMetadataFacetInfo

此可检索属性将保留一个数据结构,其中包含有关某个项在所有元数据字段中具有的所有词的信息。以下是此数据结构的格式:ColmunInternalName|ColumnDisplayName|CompressedGuid(StoreId)|CompressedGuid(TermSetId)|CompressedGuid(TermId)|CurrentLabel;#

示例

表 2 中列出的示例介绍用于搜索包含关键字"turtle"并用特定字段/值组合标记的项的搜索查询字符串。

表 2. 搜索查询字符串示例

示例

查询字符串

说明

字段 ItemType 中的单个词"reference materials":

http://contoso/searchcenter/pages/results.aspx?k=turtle&r="owstaxIdItemType"=#052263385-1fc3-4323-8d6b-50c8f6c3c45d:"reference materials"

查询字符串中位于词 ID 之前的"#0"表示仅针对指定的词 ID 进行查询。

字段 ItemType 中的词"reference materials"或其任何后代:

http://contoso/searchcenter/pages/results.aspx?k= turtle &r="owstaxIdItemType"=#52263385-1fc3-4323-8d6b-50c8f6c3c45d:"reference materials"

词 ID 之前的"#"表示针对指定的词 ID 或该词的任何子词的 ID 进行查询。

其任何字段中的单个词:

http://contoso/searchcenter/pages/results.aspx?k= turtle &r=owstaxIdMetadataAllTagsInfo=#052263385-1fc3-4323-8d6b-50c8f6c3c45d:"reference materials"

以下代码示例演示如何基于元数据字段值进行查询。

using System;
using System.IO;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace Microsoft.SDK.SharePointServer.Samples
{
// We are trying to get all the Wss IDs of this term when used as a keyword.
            int[] wssIds = TaxonomyField.GetWssIdsOfKeywordTerm(site, term.Id, 500);
            Console.Write("The wss ID's of your term are");
            if (wssIds.Length == 0)
            {
                Console.Write(" empty");
            }
            else
            {
                foreach (int wssId in wssIds)
                {
                    Console.WriteLine(" " + wssId.ToString());
                }
            }

            Console.WriteLine(".");
        }

        /// <summary>
        /// This method writes to the console the Wss ID values of the managed metadata term
        /// on the specified SPSite.
        /// </summary>
        private void TestGetWssIdsOfTerm(SPSite site, Term term)
        {
            if (term == null)
            {
                throw new ArgumentException("Parameter term cannot be null");
            }
            if (site == null)
            {
                throw new ArgumentException("Parameter site cannot be null");
            }

            // We are trying to get all the Wss IDs of this term without its children.
            int[] wssIds = TaxonomyField.GetWssIdsOfTerm(site, term.TermStore.Id, term.TermSet.Id, term.Id, false /*includeDescendants*/, 500);
            Console.Write("The wss ID's of your term are");
            if (wssIds.Length == 0)
            {
                Console.Write(" empty");
            }
            else
            {
                foreach (int wssId in wssIds)
                {
                    Console.Write(" " + wssId.ToString());
                }
            }

            Console.WriteLine(".");
        }
    }
}

请参阅

概念

管理元数据

元数据和分类编程模型