Skip to content

fix #1930 unwrap .Terms(new List<T>) to just T #1948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Nest/QueryDsl/TermLevel/Terms/TermsQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
Expand Down Expand Up @@ -26,14 +27,14 @@ public class TermsQuery : FieldNameQueryBase, ITermsQuery
internal override void WrapInContainer(IQueryContainer c) => c.Terms = this;
internal static bool IsConditionless(ITermsQuery q)
{
return q.Field.IsConditionless()
return q.Field.IsConditionless()
|| (
(q.Terms == null
|| !q.Terms.HasAny()
|| q.Terms.All(t=>t == null
(q.Terms == null
|| !q.Terms.HasAny()
|| q.Terms.All(t=>t == null
|| ((t as string)?.IsNullOrEmpty()).GetValueOrDefault(false))
)
&&
&&
(q.TermsLookup == null
|| q.TermsLookup.Id == null
|| q.TermsLookup.Path.IsConditionless()
Expand All @@ -44,11 +45,11 @@ internal static bool IsConditionless(ITermsQuery q)
}

/// <summary>
/// A query that match on any (configurable) of the provided terms.
/// A query that match on any (configurable) of the provided terms.
/// This is a simpler syntax query for using a bool query with several term queries in the should clauses.
/// </summary>
/// <typeparam name="T">The type that represents the expected hit type</typeparam>
public class TermsQueryDescriptor<T>
public class TermsQueryDescriptor<T>
: FieldNameQueryDescriptorBase<TermsQueryDescriptor<T>, ITermsQuery, T>
, ITermsQuery where T : class
{
Expand All @@ -67,7 +68,13 @@ public TermsQueryDescriptor<T> TermsLookup<TOther>(Func<FieldLookupDescriptor<TO

public TermsQueryDescriptor<T> Terms<TValue>(IEnumerable<TValue> terms) => Assign(a => a.Terms = terms?.Cast<object>());

public TermsQueryDescriptor<T> Terms<TValue>(params TValue[] terms) => Assign(a => a.Terms = terms?.Cast<object>());
public TermsQueryDescriptor<T> Terms<TValue>(params TValue[] terms) => Assign(a => {
if(terms?.Length == 1 && typeof(IEnumerable).IsAssignableFrom(typeof(TValue)) && typeof(TValue) != typeof(string))
{
a.Terms = (terms.First() as IEnumerable)?.Cast<object>();
}
else a.Terms = terms?.Cast<object>();
});

}
}
4 changes: 2 additions & 2 deletions src/Nest/QueryDsl/TermLevel/Terms/TermsQueryJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
case "minimum_should_match":
reader.Read();
var min = serializer.Deserialize<MinimumShouldMatch>(reader);
f.MinimumShouldMatch = min;
f.MinimumShouldMatch = min;
break;
case "boost":
reader.Read();
Expand Down Expand Up @@ -131,7 +131,7 @@ private void ReadTerms(ITermsQuery termsQuery, JsonReader reader, JsonSerializer
}
else if (reader.TokenType == JsonToken.StartArray)
{
var values = JArray.Load(reader).Values<string>();
var values = JArray.Load(reader).Values<object>();
termsQuery.Terms = values;
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/Tests/QueryDsl/QueryDslIntegrationTestsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
using Tests.Framework.MockData;
using Xunit;

namespace Tests.QueryDsl
{
[Collection(IntegrationContext.ReadOnly)]
public abstract class QueryDslIntegrationTestsBase : ApiIntegrationTestBase<ISearchResponse<Project>, ISearchRequest, SearchDescriptor<Project>, SearchRequest<Project>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

{
protected QueryDslIntegrationTestsBase(IIntegrationCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
protected override LazyResponses ClientUsage() => Calls(
fluent: (client, f) => client.Search<Project>(f),
fluentAsync: (client, f) => client.SearchAsync<Project>(f),
request: (client, r) => client.Search<Project>(r),
requestAsync: (client, r) => client.SearchAsync<Project>(r)
);

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override string UrlPath => "/project/project/_search";
protected override int ExpectStatusCode => 200;
protected override bool ExpectIsValid => true;

protected abstract object QueryJson { get; }

protected abstract QueryContainer QueryInitializer { get; }
protected abstract QueryContainer QueryFluent(QueryContainerDescriptor<Project> q);

protected override object ExpectJson => new { query = this.QueryJson };

protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
.Query(this.QueryFluent);

protected override SearchRequest<Project> Initializer =>
new SearchRequest<Project>
{
Query = this.QueryInitializer
};
}
}
138 changes: 138 additions & 0 deletions src/Tests/QueryDsl/TermLevel/Terms/TermsListQueryUsageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
using Tests.Framework.MockData;

namespace Tests.QueryDsl.TermLevel.Terms
{
public class TermsListQueryUsageTests : QueryDslUsageTestsBase
{
public TermsListQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) {}

protected override object QueryJson => new
{
terms = new
{
_name = "named_query",
boost = 1.1,
description = new[] { "term1", "term2" },
disable_coord = true,
minimum_should_match = 2
}
};

protected override QueryContainer QueryInitializer => new TermsQuery
{
Name = "named_query",
Boost = 1.1,
Field = "description",
Terms = new List<string> { "term1", "term2" },
DisableCoord = true,
MinimumShouldMatch = 2
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.Terms(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Description)
.DisableCoord()
.MinimumShouldMatch(MinimumShouldMatch.Fixed(2))
.Terms(new List<string> { "term1", "term2" })
);
}

public class TermsListOfListIntegrationTests : QueryDslIntegrationTestsBase
{
public TermsListOfListIntegrationTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private List<List<string>> _terms = new List<List<string>> { new List<string> { "term1", "term2" } };

protected override object QueryJson => new
{
terms = new
{
_name = "named_query",
boost = 1.1,
description = new[] { new [] { "term1", "term2" } },
disable_coord = true,
}
};

protected override QueryContainer QueryInitializer => new TermsQuery
{
Name = "named_query",
Boost = 1.1,
Field = "description",
Terms = _terms,
DisableCoord = true,
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.Terms(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Description)
.DisableCoord()
.Terms(_terms)
);

}

public class TermsListOfListStringAgainstNumericFieldIntegrationTests : QueryDslIntegrationTestsBase
{
public TermsListOfListStringAgainstNumericFieldIntegrationTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private List<List<string>> _terms = new List<List<string>> { new List<string> { "term1", "term2" } };


protected override int ExpectStatusCode => 400;
protected override bool ExpectIsValid => false;

protected override object QueryJson => new
{
terms = new
{
_name = "named_query",
boost = 1.1,
numberOfCommits = new[] { new [] { "term1", "term2" } },
disable_coord = true,
}
};

protected override QueryContainer QueryInitializer => new TermsQuery
{
Name = "named_query",
Boost = 1.1,
Field = "numberOfCommits",
Terms = _terms,
DisableCoord = true,
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.Terms(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.NumberOfCommits)
.DisableCoord()
.Terms(_terms)
);

[I] public Task AsserResponse() => AssertOnAllResponses(r =>
{
r.ServerError.Should().NotBeNull();
r.ServerError.Status.Should().Be(400);
r.ServerError.Error.Should().NotBeNull();
var rootCauses = r.ServerError.Error.RootCause;
rootCauses.Should().NotBeNullOrEmpty();
var rootCause = rootCauses.First();
rootCause.Type.Should().Be("number_format_exception");
});

}

}
28 changes: 25 additions & 3 deletions src/Tests/QueryDsl/TermLevel/Terms/TermsQueryUsageTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Nest;
using Tests.Framework.Integration;
Expand All @@ -7,6 +8,8 @@ namespace Tests.QueryDsl.TermLevel.Terms
{
public class TermsQueryUsageTests : QueryDslUsageTestsBase
{
protected virtual string[] ExpectedTerms => new [] { "term1", "term2" };

public TermsQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) {}

protected override object QueryJson => new
Expand All @@ -15,7 +18,7 @@ public TermsQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
{
_name = "named_query",
boost = 1.1,
description = new[] { "term1", "term2" },
description = ExpectedTerms,
disable_coord = true,
minimum_should_match = 2
}
Expand All @@ -26,7 +29,7 @@ public TermsQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
Name = "named_query",
Boost = 1.1,
Field = "description",
Terms = new [] { "term1", "term2" },
Terms = ExpectedTerms,
DisableCoord = true,
MinimumShouldMatch = 2
};
Expand All @@ -49,4 +52,23 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
q => q.Terms = new [] { "" }
};
}
}

public class SingleTermTermsQueryUsageTests : TermsQueryUsageTests
{
protected override string[] ExpectedTerms => new [] { "term1" };

public SingleTermTermsQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.Terms(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Description)
.DisableCoord()
.MinimumShouldMatch(MinimumShouldMatch.Fixed(2))
.Terms("term1")
);
}


}
2 changes: 2 additions & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@
<Compile Include="Cat\CatRepositories\CatRepositoriesUrlTests.cs" />
<Compile Include="Cat\CatSnapshots\CatSnapshotsApiTests.cs" />
<Compile Include="Cat\CatSnapshots\CatSnapshotsUrlTests.cs" />
<Compile Include="QueryDsl\QueryDslIntegrationTestsBase.cs" />
<Compile Include="QueryDsl\TermLevel\Terms\TermsListQueryUsageTests.cs" />
<Compile Include="Search\Request\ProfileUsageTests.cs" />
<Compile Include="Indices\StatusManagement\ForceMerge\ForceMergeApiTests.cs" />
<Compile Include="Indices\StatusManagement\ForceMerge\ForceMergeUrlTests.cs" />
Expand Down