diff --git a/src/Nest/DSL/Paths/QueryPathDescriptor.cs b/src/Nest/DSL/Paths/QueryPathDescriptor.cs index acdefea8f0c..d90d02c2214 100644 --- a/src/Nest/DSL/Paths/QueryPathDescriptor.cs +++ b/src/Nest/DSL/Paths/QueryPathDescriptor.cs @@ -52,7 +52,6 @@ public static void SetRouteParameters( //start out with defaults var inferrer = new ElasticInferrer(settings); - var index = inferrer.IndexName(); var type = inferrer.TypeName(); pathInfo.Index = index; @@ -66,11 +65,11 @@ public static void SetRouteParameters( if (path.Indices.HasAny()) pathInfo.Index = inferrer.IndexNames(path.Indices); - else if (path.AllIndices.GetValueOrDefault(false) && !pathInfo.Type.IsNullOrEmpty()) - pathInfo.Index = "_all"; else pathInfo.Index = path.AllIndices.GetValueOrDefault(false) ? null : inferrer.IndexName(); + if (pathInfo.Index.IsNullOrEmpty() && !pathInfo.Type.IsNullOrEmpty()) + pathInfo.Index = "_all"; } } diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index 57933dc8412..248d7a03c57 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -446,6 +446,7 @@ + diff --git a/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs b/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs new file mode 100644 index 00000000000..eec890a46c1 --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs @@ -0,0 +1,331 @@ +using FluentAssertions; +using Elasticsearch.Net.Connection; +using Nest.Tests.MockData.Domain; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; + +namespace Nest.Tests.Unit.Search +{ + [TestFixture] + public class SearchUrlTests + { + private void TestUrl(string expected, SearchDescriptor descriptor, ConnectionSettings settings = null) + { + var client = new ElasticClient(settings, new InMemoryConnection()); + var response = client.Search(descriptor); + var uri = new Uri(response.ConnectionStatus.RequestUrl); + var actual = WebUtility.UrlDecode(uri.AbsolutePath); + actual.Should().Be(expected); + } + + [Test] + public void AllIndices_AllTypes_Test() + { + TestUrl( + expected: "/_search", + descriptor: new SearchDescriptor() + .AllIndices() + .AllTypes() + ); + } + + [Test] + public void AllIndices_ExplicitType_Test() + { + TestUrl( + expected: "/_all/type1/_search", + descriptor: new SearchDescriptor() + .AllIndices() + .Type("type1") + ); + } + + [Test] + public void AllIndices_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/_all/type1,type2/_search", + descriptor: new SearchDescriptor() + .AllIndices() + .Types("type1", "type2") + ); + } + + [Test] + public void AllIndices_InferredType_Test() + { + TestUrl( + expected: "/_all/elasticsearchprojects/_search", + descriptor: new SearchDescriptor() + .AllIndices() + ); + } + + [Test] + public void AllIndices_MappedType_Test() + { + TestUrl( + expected: "/_all/type1/_search", + descriptor: new SearchDescriptor() + .AllIndices(), + settings: new ConnectionSettings() + .MapDefaultTypeNames(m => m + .Add(typeof(ElasticsearchProject), "type1") + ) + ); + } + + [Test] + public void DefaultIndex_AllIndices_AllTypes_Test() + { + TestUrl( + expected: "/_search", + descriptor: new SearchDescriptor() + .AllIndices() + .AllTypes(), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + ); + } + + [Test] + public void DefaultIndex_AllTypes_Test() + { + TestUrl( + expected: "/defaultindex/_search", + descriptor: new SearchDescriptor() + .AllTypes(), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + ); + } + + [Test] + public void DefaultIndex_ExplicitType_Test() + { + TestUrl( + expected: "/defaultindex/type1/_search", + descriptor: new SearchDescriptor() + .Type("type1"), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + ); + } + + [Test] + public void DefaultIndex_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/defaultindex/type1,type2/_search", + descriptor: new SearchDescriptor() + .Types("type1", "type2"), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + ); + } + + [Test] + public void DefaultIndex_InferredType_Test() + { + TestUrl( + expected: "/defaultindex/elasticsearchprojects/_search", + descriptor: new SearchDescriptor(), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + ); + } + + [Test] + public void DefaultIndex_MappedType_Test() + { + TestUrl( + expected: "/defaultindex/type1/_search", + descriptor: new SearchDescriptor(), + settings: new ConnectionSettings() + .SetDefaultIndex("defaultindex") + .MapDefaultTypeNames(m => m + .Add(typeof(ElasticsearchProject), "type1") + ) + ); + } + + [Test] + public void No_DefaultIndex_AllIndices_AllTypes_Test() + { + TestUrl( + expected: "/_search", + descriptor: new SearchDescriptor() + .AllIndices() + .AllTypes() + ); + } + + [Test] + public void No_DefaultIndex_AllTypes_Test() + { + TestUrl( + expected: "/_search", + descriptor: new SearchDescriptor() + .AllTypes() + ); + } + + [Test] + public void No_DefaultIndex_ExplicitType_Test() + { + TestUrl( + expected: "/_all/type1/_search", + descriptor: new SearchDescriptor() + .Type("type1") + ); + } + + [Test] + public void No_DefaultIndex_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/_all/type1,type2/_search", + descriptor: new SearchDescriptor() + .Types("type1", "type2") + ); + } + + [Test] + public void No_DefaultIndex_InferredType_Test() + { + TestUrl( + expected: "/_all/elasticsearchprojects/_search", + descriptor: new SearchDescriptor() + ); + } + + [Test] + public void No_DefaultIndex_MappedType_Test() + { + TestUrl( + expected: "/_all/type1/_search", + descriptor: new SearchDescriptor(), + settings: new ConnectionSettings() + .MapDefaultTypeNames(m => m + .Add(typeof(ElasticsearchProject), "type1") + ) + ); + } + + [Test] + public void MappedIndex_AllTypes_Test() + { + TestUrl( + expected: "/index1/_search", + descriptor: new SearchDescriptor() + .AllTypes(), + settings: new ConnectionSettings() + .MapDefaultTypeIndices(m => m + .Add(typeof(ElasticsearchProject), "index1") + ) + ); + } + + [Test] + public void MappedIndex_MappedType_Test() + { + TestUrl( + expected: "/index1/type1/_search", + descriptor: new SearchDescriptor(), + settings: new ConnectionSettings() + .MapDefaultTypeIndices(m => m + .Add(typeof(ElasticsearchProject), "index1") + ) + .MapDefaultTypeNames(m => m + .Add(typeof(ElasticsearchProject), "type1") + ) + ); + } + + [Test] + public void MappedIndex_InferredType_Test() + { + TestUrl( + expected: "/index1/elasticsearchprojects/_search", + descriptor: new SearchDescriptor(), + settings: new ConnectionSettings() + .MapDefaultTypeIndices(m => m + .Add(typeof(ElasticsearchProject), "index1") + ) + ); + } + + [Test] + public void MappedIndex_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/index1/type1,type2/_search", + descriptor: new SearchDescriptor() + .Types("type1", "type2"), + settings: new ConnectionSettings() + .MapDefaultTypeIndices(m => m + .Add(typeof(ElasticsearchProject), "index1") + ) + ); + } + + [Test] + public void ExplicitIndex_AllTypes_Test() + { + TestUrl( + expected: "/index1/_search", + descriptor: new SearchDescriptor() + .Index("index1") + .AllTypes() + ); + } + + [Test] + public void ExplicitIndex_InferredType_Test() + { + TestUrl( + expected: "/index1/elasticsearchprojects/_search", + descriptor: new SearchDescriptor() + .Index("index1") + ); + } + + [Test] + public void ExplicitIndex_ExplicitType_Test() + { + TestUrl( + expected: "/index1/type1/_search", + descriptor: new SearchDescriptor() + .Index("index1") + .Type("type1") + ); + } + + [Test] + public void ExplicitIndex_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/index1/type1,type2/_search", + descriptor: new SearchDescriptor() + .Index("index1") + .Types("type1", "type2") + ); + } + + [Test] + public void MultipleExplicitIndices_MultipleExplicitTypes_Test() + { + TestUrl( + expected: "/index1,index2/type1,type2/_search", + descriptor: new SearchDescriptor() + .Indices("index1", "index2") + .Types("type1", "type2") + ); + } + } +}