Skip to content

Commit

Permalink
Merge pull request SolrNet#354 from SolrNet/collation-improvements
Browse files Browse the repository at this point in the history
Collation improvements
  • Loading branch information
gjunge authored Dec 21, 2017
2 parents 59653a6 + 71b4c1f commit 03d858d
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result numFound="1" start="0">
<doc>
<str name="Key">224fbdc1-12df-4520-9fbe-dd91f916eba1</str>
</doc>
</result>
<lst name="spellcheck">
<lst name="suggestions">
<lst name="aodit">
<int name="numFound">5</int>
<int name="startOffset">0</int>
<int name="endOffset">5</int>
<arr name="suggestion">
<str>audit</str>
<str>aod it</str>
<str>addit</str>
<str>ao dit</str>
<str>andit</str>
</arr>
</lst>
<lst name="differences">
<int name="numFound">4</int>
<int name="startOffset">6</int>
<int name="endOffset">17</int>
<arr name="suggestion">
<str>difference s</str>
<str>d ifferences</str>
<str>differ ences</str>
<str>differenc es</str>
</arr>
</lst>
<lst name="collation">
<str name="collationQuery">audit differences</str>
<int name="hits">1111</int>
<lst name="misspellingsAndCorrections">
<str name="aodit">audit</str>
<str name="differencex">differences</str>
</lst>
</lst>
<lst name="collation">
<str name="collationQuery">(ao dit) differences</str>
<int name="hits">1234</int>
<lst name="misspellingsAndCorrections">
<str name="aodit">ao dit</str>
<str name="differencex">differences</str>
</lst>
</lst>
</lst>
</lst>
</response>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result numFound="1" start="0">
<doc>
<str name="Key">224fbdc1-12df-4520-9fbe-dd91f916eba1</str>
</doc>
</result>
<lst name="spellcheck">
<lst name="suggestions">
<lst name="aodit">
<int name="numFound">5</int>
<int name="startOffset">0</int>
<int name="endOffset">5</int>
<arr name="suggestion">
<str>audit</str>
<str>ao dit</str>
<str>addit</str>
<str>a od it</str>
<str>adoit</str>
</arr>
</lst>
<lst name="differencex">
<int name="numFound">4</int>
<int name="startOffset">6</int>
<int name="endOffset">17</int>
<arr name="suggestion">
<str>difference s</str>
<str>d ifferences</str>
<str>differ ences</str>
<str>differenc es</str>
</arr>
</lst>
</lst>
<lst name="collations">
<lst name="collation">
<str name="collationQuery">audit differences</str>
<long name="hits">1111</long>
<lst name="misspellingsAndCorrections">
<str name="aodit">audit</str>
<str name="differencex">differences</str>
</lst>
</lst>
<lst name="collation">
<str name="collationQuery">(ao dit) differences</str>
<long name="hits">1234</long>
<lst name="misspellingsAndCorrections">
<str name="aodit">ao dit</str>
<str name="differencex">differences</str>
</lst>
</lst>
</lst>
</lst>
</response>
4 changes: 4 additions & 0 deletions SolrNet.Tests/SolrNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@
<ItemGroup>
<Analyzer Include="..\packages\xunit.analyzers.0.7.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\responseWithSpellCheckingAndCollationsSolr4-.xml" />
<EmbeddedResource Include="Resources\responseWithSpellCheckingAndCollationsSolr5+.xml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
44 changes: 41 additions & 3 deletions SolrNet.Tests/SolrQueryResultsParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public void ParseSpellChecking()
var docNode = xml.XPathSelectElement("response/lst[@name='spellcheck']");
var spellChecking = parser.ParseSpellChecking(docNode);
Assert.NotNull(spellChecking);
Assert.Equal("dell ultrasharp", spellChecking.Collation);
Assert.Equal("dell ultrasharp", spellChecking.Collations.First().CollationQuery);
Assert.Equal(2, spellChecking.Count);
}

Expand All @@ -587,7 +587,8 @@ public void ParseSpellCheckingCollateTrueInSuggestions()
var docNode = xml.XPathSelectElement("response/lst[@name='spellcheck']");
var spellChecking = parser.ParseSpellChecking(docNode);
Assert.NotNull(spellChecking);
Assert.Equal("audit", spellChecking.Collation);
Assert.Equal("audit", spellChecking.Collations.First().CollationQuery);
Assert.Equal("au dt", spellChecking.Collations.Last().CollationQuery);
Assert.Equal(2, spellChecking.Count);
Assert.Equal(2, spellChecking.Collations.Count);
}
Expand All @@ -601,11 +602,48 @@ public void ParseSpellCheckingCollateTrueInCollations()
var docNode = xml.XPathSelectElement("response/lst[@name='spellcheck']");
var spellChecking = parser.ParseSpellChecking(docNode);
Assert.NotNull(spellChecking);
Assert.Equal("dell ultrasharp", spellChecking.Collation);
Assert.Equal("dell ultrasharp", spellChecking.Collations.First().CollationQuery);
Assert.Equal(2, spellChecking.Count);
Assert.Equal(1, spellChecking.Collations.Count);
}

[Theory]
[InlineData("4-")]
[InlineData("5+")]
public void ParseSpellCheckingCollations(string solrVersion)
{
//Collations node now separates from collation nodes from suggestions
var parser = new SpellCheckResponseParser<Product>();
XDocument xml;

if (solrVersion.Equals("5+"))
{
xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithSpellCheckingAndCollationsSolr5+.xml");
}
else
{
xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithSpellCheckingAndCollationsSolr4-.xml");
}

var docNode = xml.XPathSelectElement("response/lst[@name='spellcheck']");
var spellChecking = parser.ParseSpellChecking(docNode);
Assert.NotNull(spellChecking);
Assert.NotNull(spellChecking.Collations);
Assert.Equal(2, spellChecking.Collations.Count);
//First result
Assert.Equal("audit differences", spellChecking.Collations.First().CollationQuery);
Assert.Equal(1111, spellChecking.Collations.First().Hits);
Assert.Equal(2, spellChecking.Collations.First().MisspellingsAndCorrections.Count);
Assert.Equal("audit", spellChecking.Collations.First().MisspellingsAndCorrections["aodit"]);
Assert.Equal("differences", spellChecking.Collations.First().MisspellingsAndCorrections["differencex"]);
//Second result
Assert.Equal("(ao dit) differences", spellChecking.Collations.Last().CollationQuery);
Assert.Equal(1234, spellChecking.Collations.Last().Hits);
Assert.Equal(2, spellChecking.Collations.Last().MisspellingsAndCorrections.Count);
Assert.Equal("ao dit", spellChecking.Collations.Last().MisspellingsAndCorrections["aodit"]);
Assert.Equal("differences", spellChecking.Collations.Last().MisspellingsAndCorrections["differencex"]);
}

[Fact]
public void ParseClustering()
{
Expand Down
39 changes: 39 additions & 0 deletions SolrNet/Impl/CollationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System.Collections.Generic;

namespace SolrNet.Impl {
/// <summary>
/// Collation Result
/// </summary>
public class CollationResult {
/// <summary>
/// Collation query term
/// </summary>
public string CollationQuery { get; set;}

/// <summary>
/// Number of hits
/// </summary>
public long Hits { get; set;}

/// <summary>
/// MisspellingsAndCorrections
/// </summary>
public IDictionary<string, string> MisspellingsAndCorrections { get; set;}
}
}
29 changes: 20 additions & 9 deletions SolrNet/Impl/ResponseParsers/SpellCheckResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public SpellCheckResults ParseSpellChecking(XElement node)
var r = new SpellCheckResults();
var suggestionsNode = node.XPathSelectElement("lst[@name='suggestions']");

var collationNode = suggestionsNode.XPathSelectElement("str[@name='collation']");
if (collationNode != null)
{
r.Collation = collationNode.Value;
}

IEnumerable<XElement> collationNodes;
var collationsNode = node.XPathSelectElement("lst[@name='collations']");
if (collationsNode != null)
Expand All @@ -70,15 +64,32 @@ public SpellCheckResults ParseSpellChecking(XElement node)
collationNodes = suggestionsNode.XPathSelectElements("lst[@name='collation']");
}

CollationResult tempCollation;
foreach (var cn in collationNodes)
{
//If it does not contain collationQuery element, it is a suggestion
if (cn.XPathSelectElement("str[@name='collationQuery']") != null)
{
r.Collations.Add(cn.XPathSelectElement("str[@name='collationQuery']").Value);
if (string.IsNullOrEmpty(r.Collation))
tempCollation = new CollationResult();
tempCollation.CollationQuery = cn.XPathSelectElement("str[@name='collationQuery']").Value;

if (cn.XPathSelectElement("long[@name='hits']") != null)
{
tempCollation.Hits = Convert.ToInt64(cn.XPathSelectElement("long[@name='hits']").Value);
}
else if (cn.XPathSelectElement("int[@name='hits']") != null)
{
tempCollation.Hits = Convert.ToInt32(cn.XPathSelectElement("int[@name='hits']").Value);
}

//Selects the mispellings and corrections
var correctionNodes = cn.XPathSelectElements("lst[@name='misspellingsAndCorrections']");
tempCollation.MisspellingsAndCorrections = new Dictionary<string, string>();
foreach (var mc in correctionNodes.Elements())
{
r.Collation = cn.XPathSelectElement("str[@name='collationQuery']").Value;
tempCollation.MisspellingsAndCorrections.Add(mc.Attribute("name").Value, mc.Value);
}
r.Collations.Add(tempCollation);
}
}

Expand Down
6 changes: 1 addition & 5 deletions SolrNet/Impl/SpellCheckResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ namespace SolrNet.Impl {
/// Spell-checking query results
/// </summary>
public class SpellCheckResults : ICollection<SpellCheckResult> {
/// <summary>
/// Suggestion query from spell-checking
/// </summary>
public string Collation { get; set; }

/// <summary>
/// Multiple collations returned
/// </summary>
public ICollection<string> Collations = new List<string>();
public ICollection<CollationResult> Collations = new List<CollationResult>();

private readonly ICollection<SpellCheckResult> SpellChecks = new List<SpellCheckResult>();

Expand Down

0 comments on commit 03d858d

Please sign in to comment.