I'm writing some code against the search API in SharePoint 2007, and am seeing some interesting behaviour. I'm using the KeywordQuery 'model' rather than FullTextSqlQuery, since this matches the advanced search form our client wants.
I'm using the following code to execute the search - this builds a keyword search query in the same format as the OOTB web parts:
ResultTableCollection resultTables = null; using (KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site)) { string fullQuery = string.Empty; // TODO: refactor.. string allWords = HttpUtility.UrlDecode(queryString[AllWordsQsParam]); string exactWords = HttpUtility.UrlDecode(queryString[ExactPhraseParam]); string excludeWords = HttpUtility.UrlDecode(queryString[NoneWordsQsParam]); string anyWords = HttpUtility.UrlDecode(queryString[AnyWordsQsParam]); if (!string.IsNullOrEmpty(allWords)) { fullQuery += getFormattedTermString(' ', '+', allWords); } if (!string.IsNullOrEmpty(exactWords)) { fullQuery += exactWords; } if (!string.IsNullOrEmpty(anyWords)) { fullQuery += anyWords; } if (!string.IsNullOrEmpty(excludeWords)) { fullQuery += getFormattedTermString(' ', '-', excludeWords); } keywordQuery.QueryText = fullQuery; keywordQuery.ResultTypes = ResultType.RelevantResults; resultTables = keywordQuery.Execute(); }
After this, I load into a DataTable and eventually get XML to transform. Currently I'm using the default XSL from the CoreResultsWebPart, but I'm finding the produced XML is not what this XSL is expecting:
- element names are not all lowercase (not too difficult to amend the XSL to match up, but curious)
- the hit highlighting properties are in a different format, possibly due to an encoding issue somewhere. As an example, HitHighlightingSummary does not contain child XML elements as it does in an OOTB search - instead, there is one text node as a child which effectively contains an 'InnerXml' of the child elements. Effectively all of the angled brackets have been encoded e.g. '<' to 'ampersand lt semi-colon' (had to write it that way due to SO encoding, but you hopefully get my drift).
This latter means the xsl:apply-templates for the hit highlighting stuff is not able to process the XML, since it's really a string rather than a tree.
I've verified that it isn't the later steps of loading into a DataTable/obtaining XML/transforming which is the issue - it's coming back from the search API like this.
Any ideas why?? I'm not immediately seeing something I'm doing wrong with the KeywordQuery class, but could it be something there?