From 2c07d8c4c5a2ebadb69e826b26026a589d8b71f6 Mon Sep 17 00:00:00 2001 From: gmarz Date: Wed, 17 Dec 2014 18:36:12 -0500 Subject: [PATCH 1/2] Fix missing _all when types are specified but no default index is set Closes #1160 --- src/Nest/DSL/Paths/QueryPathDescriptor.cs | 5 +- .../Nest.Tests.Unit/Nest.Tests.Unit.csproj | 1 + .../Nest.Tests.Unit/Search/SearchUrlTests.cs | 331 ++++++++++++++++++ 3 files changed, 334 insertions(+), 3 deletions(-) create mode 100644 src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs 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..b85eb3df63a --- /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).Replace(" ", ""); + 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") + ); + } + } +} From 8e19bde04dfe30f89483cd3130f2677e9c0a37d5 Mon Sep 17 00:00:00 2001 From: gmarz Date: Wed, 17 Dec 2014 18:45:41 -0500 Subject: [PATCH 2/2] No need to replace spaces --- src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs b/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs index b85eb3df63a..eec890a46c1 100644 --- a/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs +++ b/src/Tests/Nest.Tests.Unit/Search/SearchUrlTests.cs @@ -19,7 +19,7 @@ private void TestUrl(string expected, SearchDescriptor des 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).Replace(" ", ""); + var actual = WebUtility.UrlDecode(uri.AbsolutePath); actual.Should().Be(expected); }