Skip to content

Commit

Permalink
NCBC-1432: Support Facets in FTS query responses
Browse files Browse the repository at this point in the history
MOTIVATION
----------
Facets can be added to FTS queries for additional searching behaviour.
The submission of facets in a query has already been committed, however,
the parsing and processing of the results is missing. This commit adds
support for the three available facets: term, numeric range and date
range.

MODIFICATIONS
-------------
- Update ISearchResult.Facets to be a dictionary<string, IFacetResult>
- Update SearchDataMapper to parse and process facet results
- Add FacetResultType, IFacetResult, DefaultFacetResult,
  TermFacetResult, Term, NumericRangeFacetResult NumericRange,
  DateRangeFacetResut and DateRange
- Add unit and integration tests to verify behaviour

RESULT
------
Facet results are now supported and returned when included in an FTS
query.

Change-Id: Ie36150bfb51a515ff539e15d0f581962d29e6825
Reviewed-on: http://review.couchbase.org/78488
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Jeffry Morris <jeffrymorris@gmail.com>
  • Loading branch information
MikeGoldsmith committed May 25, 2017
1 parent 63bc440 commit 7d66ec0
Show file tree
Hide file tree
Showing 19 changed files with 1,958 additions and 1,518 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ public void Facets_Success()
{
var results = bucket.Query(new SearchQuery
{
Index = "id_travel",
Index = "idx_travel",
Query = new MatchQuery("inn")
}.Facets(
new TermFacet("termfacet", "thefield", 10),
new TermFacet("termfacet", "name", 1),
new DateRangeFacet("daterangefacet", "thefield", 10).AddRange(DateTime.Now, DateTime.Now.AddDays(1)),
new NumericRangeFacet("numericrangefacet", "thefield", 2).AddRange(2.2f, 3.5f)));

Assert.IsFalse(results.Success);
Assert.IsTrue(results.Success);
Assert.AreEqual(3, results.Facets.Count);
}
}
}
Expand Down
2,939 changes: 1,483 additions & 1,456 deletions Src/Couchbase.NetStandard/Couchbase.NetStandard.csproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Src/Couchbase.UnitTests.NetStandard/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"embed": [
"..\\Couchbase.UnitTests\\Data\\*.json",
"..\\Couchbase.UnitTests\\Search\\search-response-success.js"
"..\\Couchbase.UnitTests\\Search\\*.js"
]
},
"frameworks": {
Expand Down
37 changes: 20 additions & 17 deletions Src/Couchbase.UnitTests/Couchbase.UnitTests.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 ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -131,21 +131,21 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Search\BooleanFieldQueryTests.cs" />
<Compile Include="Search\DocIdQueryTests.cs" />
<Compile Include="Search\GeoBoundingBoxQueryTests.cs" />
<Compile Include="Search\TermRangeQueryTests.cs" />
<Compile Include="Search\GeoDistanceQueryTests.cs" />
<Compile Include="Search\FieldSearchSortTests.cs" />
<Compile Include="Search\GeoDistanceSearchSortTests.cs" />
<Compile Include="Search\IdSearchSortTests.cs" />
<Compile Include="Search\MatchAllQueryTests.cs" />
<Compile Include="Search\MatchNoneQueryTests.cs" />
<Compile Include="Search\PhraseQueryTests.cs" />
<Compile Include="ResourceHelper.cs" />
<Compile Include="Search\QueryStringQueryTests.cs" />
<Compile Include="Search\ScoreSearchSortTests.cs" />
<Compile Include="Search\TermQueryTests.cs" />
<Compile Include="Search\SearchClientTests.cs" />
<Compile Include="Search\SearchParamsTests.cs" />
<Compile Include="Search\GeoBoundingBoxQueryTests.cs" />
<Compile Include="Search\TermRangeQueryTests.cs" />
<Compile Include="Search\GeoDistanceQueryTests.cs" />
<Compile Include="Search\FieldSearchSortTests.cs" />
<Compile Include="Search\GeoDistanceSearchSortTests.cs" />
<Compile Include="Search\IdSearchSortTests.cs" />
<Compile Include="Search\MatchAllQueryTests.cs" />
<Compile Include="Search\MatchNoneQueryTests.cs" />
<Compile Include="Search\PhraseQueryTests.cs" />
<Compile Include="ResourceHelper.cs" />
<Compile Include="Search\QueryStringQueryTests.cs" />
<Compile Include="Search\ScoreSearchSortTests.cs" />
<Compile Include="Search\TermQueryTests.cs" />
<Compile Include="Search\SearchClientTests.cs" />
<Compile Include="Search\SearchParamsTests.cs" />
<Compile Include="Search\WildcardQueryTests.cs" />
<Compile Include="StringExtensionTests.cs" />
<Compile Include="Search\FtsDataMapperTests.cs" />
Expand Down Expand Up @@ -202,6 +202,9 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Search\search-response-with-facets.js" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand All @@ -211,4 +214,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
61 changes: 61 additions & 0 deletions Src/Couchbase.UnitTests/Search/FtsDataMapperTests.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using Couchbase.Search;
using Newtonsoft.Json;
using NUnit.Framework;
using NUnit.Framework.Internal;

Expand Down Expand Up @@ -113,6 +115,65 @@ public void Hits_WhenSuccess_ReturnsValidData()
}
}

[Test]
public void Facets_Are_Populated_When_Result_Contains_Facets()
{
ISearchQueryResult result;
var mapper = new SearchDataMapper();
using (var stream = OpenResource("search-response-with-facets.js"))
{
result = mapper.Map(stream);
}

var expectedFacets = JsonConvert.SerializeObject(new
{
category = new TermFacetResult
{
Name = "category",
Field = "term_field",
Total = 100,
Missing = 65,
Other = 35,
Terms = new[]
{
new Term {Name = "term1", Count = 10}
}
},
strength = new NumericRangeFacetResult
{
Name = "strength",
Field = "numeric_field",
Total = 13,
Missing = 11,
Other = 2,
NumericRanges = new[]
{
new NumericRange {Name = "numeric1", Min = 0.1f, Max = 0.2f, Count = 50}
}
},
updateRange = new DateRangeFacetResult
{
Name = "updateRange",
Field = "daterange_field",
Total = 65,
Missing = 43,
Other = 22,
DateRanges = new[]
{
new DateRange
{
Name = "daterange1",
Start = new DateTime(2017, 1, 1),
End = new DateTime(2017, 1, 2),
Count = 54
}
}
}
});

Assert.AreEqual(expectedFacets, JsonConvert.SerializeObject(result.Facets));
}

private Stream OpenResource(string resourceName)
{
return ResourceHelper.ReadResourceAsStream(resourceName);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 33 additions & 24 deletions Src/Couchbase/Couchbase.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 ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -77,14 +77,14 @@
<Compile Include="Analytics\AnalyticsClient.cs" />
<Compile Include="Analytics\AnalyticsResult.cs" />
<Compile Include="Authentication\AuthContext.cs" />
<Compile Include="Authentication\AuthenticatorExtensions.cs" />
<Compile Include="Authentication\AuthenticatorExtensions.cs" />
<Compile Include="Authentication\AuthType.cs" />
<Compile Include="Authentication\ClassicAuthenticator.cs" />
<Compile Include="Authentication\ClassicAuthenticator.cs" />
<Compile Include="Authentication\ClusterCredentials.cs" />
<Compile Include="Authentication\AuthenticatorType.cs" />
<Compile Include="Authentication\IAuthenticator.cs" />
<Compile Include="Authentication\AuthenticatorType.cs" />
<Compile Include="Authentication\IAuthenticator.cs" />
<Compile Include="Authentication\IClusterCredentials.cs" />
<Compile Include="Authentication\PasswordAuthenticator.cs" />
<Compile Include="Authentication\PasswordAuthenticator.cs" />
<Compile Include="Authentication\SASL\CramMd5Mechanism.cs" />
<Compile Include="Authentication\SASL\ISaslMechanism.cs" />
<Compile Include="Authentication\SASL\MechanismType.cs" />
Expand Down Expand Up @@ -176,7 +176,7 @@
<Compile Include="Core\Serialization\DeserializationOptions.cs" />
<Compile Include="Core\Services\ServiceNotSupportedException.cs" />
<Compile Include="Core\ExpressionVisitors\SubDocumentPathExpressionVisitor.cs" />
<Compile Include="Core\Transcoders\BinaryTranscoder.cs" />
<Compile Include="Core\Transcoders\BinaryTranscoder.cs" />
<Compile Include="Core\Transcoders\ITypeTranscoder.cs" />
<Compile Include="Core\Transcoders\DefaultTranscoder.cs" />
<Compile Include="Core\Transcoders\BinaryToJsonTranscoder.cs" />
Expand Down Expand Up @@ -248,7 +248,7 @@
<Compile Include="CouchbaseResponseException.cs" />
<Compile Include="DocumentAlreadyExistsException.cs" />
<Compile Include="DocumentDoesNotExistException.cs" />
<Compile Include="HttpServiceBase.cs" />
<Compile Include="HttpServiceBase.cs" />
<Compile Include="IDocument.cs" />
<Compile Include="IDocumentFragment.cs" />
<Compile Include="IO\BufferUnavailableException.cs" />
Expand All @@ -257,8 +257,8 @@
<Compile Include="IO\Http\NonAuthenticatingHttpClientHandler.cs" />
<Compile Include="IO\IOBuffer.cs" />
<Compile Include="Core\SubdocLookupFlags.cs" />
<Compile Include="IO\Operations\Authentication\SelectBucket.cs" />
<Compile Include="IO\Operations\GetErrorMap.cs" />
<Compile Include="IO\Operations\Authentication\SelectBucket.cs" />
<Compile Include="IO\Operations\GetErrorMap.cs" />
<Compile Include="IO\Operations\SubDocument\MultiLookup.cs" />
<Compile Include="IO\Operations\SubDocument\MultiMutation.cs" />
<Compile Include="IO\Operations\SubDocument\SubDocDictUpsert.cs" />
Expand Down Expand Up @@ -329,9 +329,9 @@
<Compile Include="Management\Indexes\IndexResult.cs" />
<Compile Include="Management\Indexes\IndexType.cs" />
<Compile Include="Management\MemcachedBucketManager.cs" />
<Compile Include="Management\Role.cs" />
<Compile Include="Management\User.cs" />
<Compile Include="Management\IUserManager.cs" />
<Compile Include="Management\Role.cs" />
<Compile Include="Management\User.cs" />
<Compile Include="Management\IUserManager.cs" />
<Compile Include="MediaType.cs" />
<Compile Include="MemcachedBucket.cs" />
<Compile Include="Core\Buckets\NodeLocatorEnum.cs" />
Expand Down Expand Up @@ -387,16 +387,23 @@
<Compile Include="ResponseExtensions.cs" />
<Compile Include="ScanConsistencyNameResolver.cs" />
<Compile Include="Search\CouchbaseSearchResponseException.cs" />
<Compile Include="Search\DateRange.cs" />
<Compile Include="Search\DateRangeFacetResult.cs" />
<Compile Include="Search\DateRangeFacet.cs" />
<Compile Include="Search\Queries\Range\TermRangeQuery.cs" />
<Compile Include="Search\Sort\FieldSearchSort.cs" />
<Compile Include="Search\Sort\GeoDistanceSearchSort.cs" />
<Compile Include="Search\Queries\Geo\GeoBoundingBoxQuery.cs" />
<Compile Include="Search\Queries\Geo\GeoDistanceQuery.cs" />
<Compile Include="Search\Sort\IdSearchSort.cs" />
<Compile Include="Search\Sort\ISearchSort.cs" />
<Compile Include="Search\DefaultFacetResult.cs" />
<Compile Include="Search\FacetResultType.cs" />
<Compile Include="Search\IFacetResult.cs" />
<Compile Include="Search\NumericRange.cs" />
<Compile Include="Search\NumericRangeFacetResult.cs" />
<Compile Include="Search\Queries\Range\TermRangeQuery.cs" />
<Compile Include="Search\Sort\FieldSearchSort.cs" />
<Compile Include="Search\Sort\GeoDistanceSearchSort.cs" />
<Compile Include="Search\Queries\Geo\GeoBoundingBoxQuery.cs" />
<Compile Include="Search\Queries\Geo\GeoDistanceQuery.cs" />
<Compile Include="Search\Sort\IdSearchSort.cs" />
<Compile Include="Search\Sort\ISearchSort.cs" />
<Compile Include="Search\Queries\Simple\QueryStringQuery.cs" />
<Compile Include="Search\Sort\ScoreSearchSort.cs" />
<Compile Include="Search\Sort\ScoreSearchSort.cs" />
<Compile Include="Search\SearchMetrics.cs" />
<Compile Include="Search\ISearchParams.cs" />
<Compile Include="Search\Queries\Simple\BooleanFieldQuery.cs" />
Expand Down Expand Up @@ -431,14 +438,16 @@
<Compile Include="Search\SearchQuery.cs" />
<Compile Include="Search\SearchQueryResult.cs" />
<Compile Include="Search\SearchQueryRow.cs" />
<Compile Include="Search\Sort\SearchSortBase.cs" />
<Compile Include="Search\Sort\SearchSortBase.cs" />
<Compile Include="Search\SearchStatus.cs" />
<Compile Include="Search\TermFacetResult.cs" />
<Compile Include="Search\TermFacet.cs" />
<Compile Include="Search\Term.cs" />
<Compile Include="SubdocExtensions.cs" />
<Compile Include="TemporaryLockFailureException.cs" />
<Compile Include="Utils\AssemblyVersion.cs" />
<Compile Include="Configuration\Server\Serialization\NodeArrayExtensions.cs" />
<Compile Include="Utils\ClientIdentifier.cs" />
<Compile Include="Utils\ClientIdentifier.cs" />
<Compile Include="Utils\ExceptionUtil.cs" />
<Compile Include="IO\MissingKeyException.cs" />
<Compile Include="Utils\QueryResultExtensions.cs" />
Expand Down Expand Up @@ -558,4 +567,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
39 changes: 39 additions & 0 deletions Src/Couchbase/Search/DateRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace Couchbase.Search
{
public class DateRange
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }

/// <summary>
/// Gets or sets the start date.
/// </summary>
/// <value>
/// The start.
/// </value>
public DateTime Start { get; set; }

/// <summary>
/// Gets or sets the end date.
/// </summary>
/// <value>
/// The end.
/// </value>
public DateTime End { get; set; }

/// <summary>
/// Gets or sets the count.
/// </summary>
/// <value>
/// The count.
/// </value>
public long Count { get; set; }
}
}
31 changes: 31 additions & 0 deletions Src/Couchbase/Search/DateRangeFacetResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;

namespace Couchbase.Search
{
/// <summary>
/// The result for a <see cref="DateRangeFacet"/>.
/// </summary>
public class DateRangeFacetResult : DefaultFacetResult
{
public DateRangeFacetResult()
{
DateRanges = new List<DateRange>();
}

/// <summary>
/// Gets or sets the date ranges.
/// </summary>
/// <value>
/// The date ranges.
/// </value>
public IReadOnlyCollection<DateRange> DateRanges { get; set; }

/// <summary>
/// Gets the type of the facet result.
/// </summary>
/// <value>
/// The type of the facet result.
/// </value>
public override FacetResultType FacetResultType { get { return FacetResultType.DateRange; } }
}
}
Loading

0 comments on commit 7d66ec0

Please sign in to comment.