How to enable phonetic results when using KeywordQuery?
-
16-10-2019 - |
Question
I cannot seem to get a custom keyword query to return phonetic results when searching for people.
I have FAST search installed, and if I do a people search (in the FAST search center) for "Jon", I get the expected results containing both "Jon" and "John". However, if I construct a keyword query (as shown below) I only return the results for "Jon".
Any suggestions?
using (SPSite spSite = new SPSite("http://localhost"))
{
// get the query and settings service proxy
SearchQueryAndSiteSettingsServiceProxy settingsProxy = spSite.WebApplication.Farm.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
// get the search service application proxy by name
SearchServiceApplicationProxy proxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("FAST Query SSA");
KeywordQuery keywordQuery = new KeywordQuery(proxy);
keywordQuery.RowLimit = 100;
keywordQuery.EnablePhonetic = true;
keywordQuery.EnableNicknames = true;
keywordQuery.ResultsProvider = SearchProvider.SharepointSearch;
keywordQuery.ResultTypes = ResultType.RelevantResults;
keywordQuery.SelectProperties.Add("firstname");
keywordQuery.SelectProperties.Add("lastname");
keywordQuery.QueryText = "jon";
ResultTableCollection fullTextSearchResults = keywordQuery.Execute();
if (fullTextSearchResults.Exists(ResultType.RelevantResults))
{
ResultTable searchResult = fullTextSearchResults[ResultType.RelevantResults];
Console.WriteLine("Total Rows: " + searchResult.TotalRows) ;
Console.WriteLine("Total Row Count: " + searchResult.RowCount);
Console.WriteLine("Total Rows Including Duplicates: " + searchResult.TotalRowsIncludingDuplicates);
Console.WriteLine("Is Total Rows Exact: " + searchResult.IsTotalRowsExact);
}
}
Solution
I found a blog where someone referenced the fact that you need to set the RankingModelId
on the KeywordQuery. This works for the sample code you see above, as well as the custom extended CoreSearchResults web part I've built.
Here is the sample code:
using (SPSite spSite = new SPSite("http://localhost"))
{
SearchQueryAndSiteSettingsServiceProxy settingsProxy = spSite.WebApplication.Farm.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
SearchServiceApplicationProxy proxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("FAST Query SSA");
KeywordQuery keywordQuery = new KeywordQuery(proxy);
keywordQuery.RowLimit = 100;
keywordQuery.EnablePhonetic = true;
keywordQuery.EnableNicknames = true;
keywordQuery.ResultsProvider = SearchProvider.SharepointSearch;
keywordQuery.RankingModelId = "D9BFB1A1-9036-4627-83B2-BBD9983AC8A1";
keywordQuery.ResultTypes = ResultType.RelevantResults;
keywordQuery.SelectProperties.Add("firstname");
keywordQuery.SelectProperties.Add("lastname");
keywordQuery.QueryText = "jon";
ResultTableCollection fullTextSearchResults = keywordQuery.Execute();
if (fullTextSearchResults.Exists(ResultType.RelevantResults))
{
ResultTable searchResult = fullTextSearchResults[ResultType.RelevantResults];
Console.WriteLine("Total Rows: " + searchResult.TotalRows) ;
Console.WriteLine("Total Row Count: " + searchResult.RowCount);
Console.WriteLine("Total Rows Including Duplicates: " + searchResult.TotalRowsIncludingDuplicates);
Console.WriteLine("Is Total Rows Exact: " + searchResult.IsTotalRowsExact);
}
}
OTHER TIPS
What about changing the ResultsProvider value to "FASTSearch"?
For anyone trying to do this through a web service the element you'll need is named RelevanceModel
instead of RankingModelId
I was stuck trying to get it to work through jQuery and SPServices until I found the Microsoft.Search.Query Schema reference and discovered the element name is RelevanceModel
when doing Web Service calls. The reference doesn't mention EnableNicknames
or EnablePhonetic
, but I found they were also needed to get this to work.
Here's the XML query I ended up with:
<QueryPacket Revision="1000">'
<Query>
<Context>
<QueryText language="en-US" type="STRING">Jon scope:"People" </QueryText>
</Context>
<SupportedFormats Format="urn:Microsoft.Search.Response.Document.Document" />
<ResultProvider>SharepointSearch</ResultProvider>
<EnableNicknames>true</EnableNicknames>
<EnablePhonetic>true</EnablePhonetic>
<RelevanceModel>d9bfb1a1-9036-4627-83b2-bbd9983ac8a1</RelevanceModel>
<Properties>
...
<Property name="FirstName" />
<Property name="LastName" />
</Properties>
</Query>
</QueryPacket>