)POST requests:
URL:http://server/_api/search/postquery
在Post方式中,你可以使用JavaScript Object Notation (JSON)方式传参
?
Post方式的场景有三种:
Get方式传参,Url超出长度限制,只有使用Post方式;
查询条件非常复杂,Get方式难以满足需要,使用Post方式;
对于某些特定参数,只支持Post方式。
二、Get方式查看返回XML
1、? 在我配置好的搜索页面上,搜索“北京”,如下图,有4个结果
2、? 使用Get方式返回xml,在_api/search/query 后面添加querytext参数”北京”的Escape值“%E5%8C%97%E4%BA%AC”
http://URL/_api/search/query?querytext='%E5%8C%97%E4%BA%AC'
?
(返回的xml截图)
3、单个项目的XML节点
Rank 10.3831567764282 Edm.Double DocId 52 Edm.Int64 WorkId 52 Edm.Int64 Title 北京 Edm.String Author 系统帐户 Edm.String Size 120 Edm.Int64 Path http://url/DocLib/北方城市/北京.txt Edm.String Description Null Write 2013-08-21T02:22:46.0000000Z Edm.DateTime CollapsingStatus 0 Edm.Int64 HitHighlightedSummary 北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都北京是首都 Edm.String HitHighlightedProperties Null contentclass STS_ListItem_DocumentLibrary Edm.String PictureThumbnailURL Null ServerRedirectedURL Null ServerRedirectedEmbedURL Null ServerRedirectedPreviewURL Null FileExtension txt Edm.String ContentTypeId 0x01010027E858607844AC42AD371DFAA2B2557C Edm.String ParentLink http://url/DocLib/北方城市 Edm.String ViewsLifeTime Null ViewsRecent Null SectionNames Edm.String SectionIndexes Edm.String SiteLogo Null SiteDescription Null deeplinks Null importance 0 Edm.Int64 SiteName http://url Edm.String IsDocument true Edm.Boolean LastModifiedTime 2013-08-21T02:22:46.0000000Z Edm.DateTime FileType txt Edm.String IsContainer false Edm.Boolean WebTemplate Null SecondaryFileExtension txt Edm.String docaclmeta Null OriginalPath http://url/DocLib/北方城市/北京.txt Edm.String PartitionId 0c37852b-34d0-418e-91c6-2ac25af4be5b Edm.Guid UrlZone 1 Edm.Int32 AAMEnabledManagedProperties AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL Edm.String RenderTemplateId ~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js Edm.String piSearchResultId 3_1 Edm.String
View Code 4、解疑,我看到这个结果,很奇怪,为什么我的网站中有4条结果,REST返回了5条,详细查看每个SimpleDateRow发现,最后一个居然是之前做BCS测试的时候的结果,XML附后。?
Pathbdc3://blobdatacontent_blobdatacontent/Default/00000000%252D0000%252D0000%252D0000%252D000000000000/1394/BlobDataContent/1396?s_id=iAwAAAA==&s_ce=048g24810001020408000h20005sEdm.String
?
三、?? Get 方式 - XML调用实例
1、? 调用效果图,如下:
2、? 后台方法:
1 public void gvDateBind() 2 { 3 string keywords = "'" + System.Web.HttpUtility.UrlEncode(tbKeyWord.Text) + "'"; 4 string strRes = "http://url/_api/search/query?querytext=" + keywords; 5 6 WebClient wc = new WebClient(); 7 wc.Encoding = System.Text.Encoding.UTF8; 8 NetworkCredential nc = new NetworkCredential("username", "password", "ad"); 9 wc.Credentials = nc;10 string str = wc.DownloadString(strRes);11 12 XmlDocument doc = new XmlDocument();13 doc.LoadXml(str);14 XmlNodeList xnodelist = doc.GetElementsByTagName("d:Rows");15 XmlNode node = xnodelist[0];16 17 DataTable dt = new DataTable();18 dt.Columns.Add("标题");19 dt.Columns.Add("作者");20 dt.Columns.Add("描述");21 dt.Columns.Add("链接");22 23 XmlNodeList xlist = node.ChildNodes;24 25 foreach (XmlNode xnode in xlist)26 {27 XmlNodeList nn = xnode.ChildNodes[0].ChildNodes;28 DataRow dr = dt.NewRow();29 30 foreach (XmlNode nnode in nn)31 {32 switch (nnode.ChildNodes[0].InnerText)33 {34 case "Title":35 dr["标题"] = nnode.ChildNodes[1].InnerText;36 break;37 case "Author":38 dr["作者"] = nnode.ChildNodes[1].InnerText;39 break;40 case "HitHighlightedSummary":41 dr["描述"] = nnode.ChildNodes[1].InnerText;42 break;43 case "Path":44 dr["链接"] = nnode.ChildNodes[1].InnerText;45 break;46 }47 }48 dt.Rows.Add(dr);49 }50 51 gvSearchResult.DataSource = dt;52 gvSearchResult.DataBind();53 }
Get方式-Xml调用示例
?四、?? POST方式 - JSON调用实例
1、调用结果显示
?
2、核心代码
?
$.ajax( { url: http://url/_api/search/postquery, type: "Post", dataType: "application/json;odata=verbose", data: JSON.stringify({ 'request': { 'Querytext': queryText, 'StartRow': 1, 'RowLimit': 8, 'SelectProperties': { 'results': ['Title', 'ContentSource', 'DisplayAuthor', 'Path'] }, 'TrimDuplicates': true, 'Refiners': 'companies,contentclass,FileType(filter=6/0/*)', //'RefinementFilters': { 'results': ['filetype:equals("txt")'] } } }), headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": xRequestDigest }, complete: ProcessSearchResult });
注:本来自己想写个调用JSON的博客,但是看到CSDN上有人写过了,自己就不写了,把最重要的那段代码贴给大家参考,大家有兴趣可以参考下,博客地址附后!
?
五、?? 常用的查询参数:
查询关键字(querytext)
http://server/_api/search/query?querytext='sharepoint'
?
JSON实例:
{'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint'}
?
起始行(StartRow)
http://server/_api/search/query?querytext='sharepoint'&startrow=10
?
JSON实例:
{'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','StartRow' : '10'}
?
返回行限制(RowLimit)
http://server/_api/search/query?querytext='sharepoint'&rowlimit=30
?
JSON实例:
{'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','RowLimit' : '30'}
?
选择属性(SelectProperties)
http://server/_api/search/query?querytext='sharepoint'&selectproperties='Title,Author'
?
JSON实例:
{'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','SelectProperties' : { 'results' : [ 'Title, Author' ]}}
?
扩展名(RefinementFilters)
http://server/_api/search/query?querytext='sharepoint'&refinementfilters='fileExtension:equals("docx")'
?
JSON实例:
{'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','RefinementFilters' : {'results' : ['fileExtension:equals("docx")']}}
?
排序(SortList)
http://server/_api/search/query?querytext='sharepoint'&sortlist='rank:descending,modifiedby:ascending'
?
JSON实例:
{'__metadata' : {'type':'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','SortList' :{ 'results' : [ { 'Property':'Created', 'Direction': '0' }, { 'Property':'FileExtension', 'Direction': '1' } ]}}
?
返回总长度(SummaryLength)
http://server/_api/search/query?querytext='sharepoint'&summarylength=150
?
JSON实例:
{'__metadata':{'type':'Microsoft.Office.Server.Search.REST.SearchRequest'},'Querytext' : 'sharepoint','Summarylength' : '150'}