Skip to content

Commit

Permalink
Merge branch 'collapse-expand'
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Sep 20, 2014
2 parents 6bf7a63 + 4dcd1e0 commit 4b8da41
Show file tree
Hide file tree
Showing 15 changed files with 569 additions and 6 deletions.
45 changes: 45 additions & 0 deletions SolrNet.Tests/CollapseExpandResponseParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using MbUnit.Framework;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.ResponseParsers;
using SolrNet.Mapping;
using SolrNet.Tests.Utils;

namespace SolrNet.Tests
{
[TestFixture]
public class CollapseExpandResponseParserTests
{
[Test]
public void Parse()
{
var mapper = new AttributesMappingManager();
var parser = new CollapseExpandResponseParser<Doc>(new SolrDocumentResponseParser<Doc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<Doc>()));
var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.collapseWithoutExpandResponse.xml");
var results = new SolrQueryResults<Doc>();
parser.Parse(xml, results);
Assert.IsNull(results.CollapseExpand);
}

[Test]
public void Parse2()
{
var mapper = new AttributesMappingManager();
var parser = new CollapseExpandResponseParser<Doc>(new SolrDocumentResponseParser<Doc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<Doc>()));
var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.collapseWithExpandResponse.xml");
var results = new SolrQueryResults<Doc>();
parser.Parse(xml, results);
Assert.IsNotNull(results.CollapseExpand);
Assert.AreEqual(4, results.CollapseExpand.Groups.Count);

var group = results.CollapseExpand.Groups.ElementAt(0);
Assert.AreEqual(group.Documents.Count, 2);
Assert.AreEqual(group.GroupValue, "First");
Assert.AreEqual(group.NumFound, 2);
}

class Doc {}
}
}
51 changes: 51 additions & 0 deletions SolrNet.Tests/Resources/collapseWithExpandResponse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8" ?>
<response>
<result name="response" numFound="4" start="0">
<doc>
<str name="fieldKey">First</str>
<str name="fieldValue">First value</str>
</doc>
<doc>
<str name="fieldKey">Second</str>
<str name="fieldValue">Second value</str>
</doc>
<doc>
<str name="fieldKey">Third</str>
<str name="fieldValue">Third value</str>
</doc>
<doc>
<str name="fieldKey">Fourth</str>
<str name="fieldValue">Fourth value</str>
</doc>
</result>
<lst name="expanded">
<result name="First" numFound="2" start="0">
<doc>
<str name="fieldKey">First</str>
<str name="fieldValue">First value</str>
</doc>
<doc>
<str name="fieldKey">First</str>
<str name="fieldValue">First value 2</str>
</doc>
</result>
<result name="Second" numFound="1" start="0">
<doc>
<str name="fieldKey">Second</str>
<str name="fieldValue">Second value</str>
</doc>
</result>
<result name="Third" numFound="1" start="0">
<doc>
<str name="fieldKey">Third</str>
<str name="fieldValue">Third value</str>
</doc>
</result>
<result name="Fourth" numFound="1" start="0">
<doc>
<str name="fieldKey">Fourth</str>
<str name="fieldValue">Fourth value</str>
</doc>
</result>
</lst>
</response>
21 changes: 21 additions & 0 deletions SolrNet.Tests/Resources/collapseWithoutExpandResponse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<response>
<result name="response" numFound="4" start="0">
<doc>
<str name="fieldKey">First</str>
<str name="fieldValue">First value</str>
</doc>
<doc>
<str name="fieldKey">Second</str>
<str name="fieldValue">Second value</str>
</doc>
<doc>
<str name="fieldKey">Third</str>
<str name="fieldValue">Third value</str>
</doc>
<doc>
<str name="fieldKey">Fourth</str>
<str name="fieldValue">Fourth value</str>
</doc>
</result>
</response>
7 changes: 6 additions & 1 deletion SolrNet.Tests/SolrNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -90,6 +90,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="CollapseExpandResponseParserTests.cs" />
<Compile Include="DateTimeOffsetFieldParserTests.cs" />
<Compile Include="DefaultResponseParserTests.cs" />
<Compile Include="LocationTests.cs" />
Expand Down Expand Up @@ -329,6 +330,10 @@
<ItemGroup>
<EmbeddedResource Include="Resources\partialResponseWithStats.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\collapseWithExpandResponse.xml" />
<EmbeddedResource Include="Resources\collapseWithoutExpandResponse.xml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
49 changes: 47 additions & 2 deletions SolrNet.Tests/SolrQueryExecuterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using MbUnit.Framework;
Expand Down Expand Up @@ -708,7 +709,7 @@ public void GetClusteringParameters() {
public void GetCursormarkWithDefaultSetup()
{
var e = new SolrQueryExecuter<TestDocument>(null, null, null, null, null);
var p = e.GetCommonParameters(new QueryOptions {
var p = e.GetCommonParameters(new CommonQueryOptions {
StartOrCursor = StartOrCursor.Cursor.Start
});

Expand All @@ -720,12 +721,56 @@ public void GetCursormarkWithDefaultSetup()
public void GetCursormarkWithMarkSet()
{
var e = new SolrQueryExecuter<TestDocument>(null, null, null, null, null);
var p = e.GetCommonParameters(new QueryOptions {
var p = e.GetCommonParameters(new CommonQueryOptions {
StartOrCursor = new StartOrCursor.Cursor("AoEoZTQ3YmY0NDM=")
});

Assert.AreEqual("cursorMark", p.First().Key);
Assert.AreEqual("AoEoZTQ3YmY0NDM=", p.First().Value);
}

[Test]
public void GetCollapseExpandParameters() {
var querySerializer = new DefaultQuerySerializer(new DefaultFieldSerializer());
var e = new SolrQueryExecuter<TestDocument>(null, null, querySerializer, null, null);
var p = e.GetAllParameters(SolrQuery.All, new QueryOptions {
Rows = 1,
CollapseExpand = new CollapseExpandParameters("somefield", null, null, null),
}).ToList();
Assert.Contains(p, KV.Create("fq", "{!collapse field=somefield}"));
}

[Test]
public void GetCollapseExpandParameters_min_policy() {
var querySerializer = new DefaultQuerySerializer(new DefaultFieldSerializer());
var e = new SolrQueryExecuter<TestDocument>(null, null, querySerializer, null, null);
var max = new CollapseExpandParameters.MinOrMax.Max("maxfield");
var policy = CollapseExpandParameters.NullPolicyType.Collapse;
var p = e.GetAllParameters(SolrQuery.All, new QueryOptions {
Rows = 1,
CollapseExpand = new CollapseExpandParameters("somefield", null, max, policy),
}).ToList();
Assert.Contains(p, KV.Create("fq", "{!collapse field=somefield nullPolicy=collapse max=maxfield}"));
}

[Test]
public void GetCollapseExpandParameters_Expand() {
var querySerializer = new DefaultQuerySerializer(new DefaultFieldSerializer());
var e = new SolrQueryExecuter<TestDocument>(null, null, querySerializer, null, null);
var expand = new ExpandParameters(
sort: new SortOrder("sortField", Order.ASC),
rows: 100,
query: new SolrQuery("aquery"),
filterQuery: null);

var p = e.GetAllParameters(SolrQuery.All, new QueryOptions {
Rows = 1,
CollapseExpand = new CollapseExpandParameters("somefield", expand, null, null),
}).ToList();
Assert.Contains(p, KV.Create("fq", "{!collapse field=somefield}"));
Assert.Contains(p, KV.Create("expand.sort", "sortField asc"));
Assert.Contains(p, KV.Create("expand.rows", "100"));
Assert.Contains(p, KV.Create("expand.q", "aquery"));
}
}
}
28 changes: 28 additions & 0 deletions SolrNet/CollapseExpandResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;

namespace SolrNet
{
/// <summary>
/// Collapse/expand results model
/// </summary>
public class CollapseExpandResults<T>
{
private readonly ICollection<Group<T>> groups;

/// <summary>
/// Grouped documents
/// </summary>
public ICollection<Group<T>> Groups
{
get { return groups; }
}

/// <summary>
/// Constructor for CollapseExpandResults
/// </summary>
public CollapseExpandResults(ICollection<Group<T>> groups)
{
this.groups = groups;
}
}
}
Loading

0 comments on commit 4b8da41

Please sign in to comment.