Skip to content

Commit bdd51bc

Browse files
committed
Implemented terms lookup filter fix #405
1 parent eb3bc8f commit bdd51bc

File tree

7 files changed

+212
-21
lines changed

7 files changed

+212
-21
lines changed

src/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@
107107
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
108108
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
109109
<Compile Include="Internals\Serialize\OptOutTests.cs" />
110+
<Compile Include="Search\Filter\Singles\TermsLookupFilterJson.cs" />
111+
<Compile Include="Search\Filter\Singles\GeoIndexedShapeFilterJson.cs" />
110112
<Compile Include="Search\Filter\Singles\GeoShapeFilterJson.cs" />
111113
<Compile Include="Search\Filter\Singles\RegexpFilterJson.cs" />
112114
<Compile Include="Search\Query\BoolQueryMerges\BoolQueryMergesTests.cs" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using NUnit.Framework;
2+
using Nest.Tests.MockData.Domain;
3+
4+
namespace Nest.Tests.Unit.Search.Filter.Singles
5+
{
6+
[TestFixture]
7+
public class TermsLookupFilterJson
8+
{
9+
[Test]
10+
public void TermsLookupFilter()
11+
{
12+
var s = new SearchDescriptor<ElasticSearchProject>()
13+
.From(0)
14+
.Size(10)
15+
.Filter(ff=>ff
16+
.TermsLookup(f=>f.Name, t => t.Lookup<ElasticSearchProject>(p=>p.Name, "NEST"))
17+
);
18+
19+
var json = TestElasticClient.Serialize(s);
20+
var expected = @"{ from: 0, size: 10,
21+
filter : {
22+
terms: {
23+
""name"": {
24+
id: ""NEST"",
25+
type: ""elasticsearchprojects"",
26+
index: ""mydefaultindex"",
27+
path: ""name""
28+
}
29+
}
30+
}
31+
}";
32+
Assert.True(json.JsonEquals(expected), json);
33+
}
34+
[Test]
35+
public void TermsLookupFilterWithCache()
36+
{
37+
var s = new SearchDescriptor<ElasticSearchProject>()
38+
.From(0)
39+
.Size(10)
40+
.Filter(ff => ff
41+
.Cache(true)
42+
.Name("terms_filter")
43+
.CacheKey("NEST_TERMS")
44+
.TermsLookup(f => f.Name, t => t
45+
.Lookup<ElasticSearchProject>(p => p.Name, "NEST")
46+
.Routing("dot_net_clients")
47+
)
48+
);
49+
50+
var json = TestElasticClient.Serialize(s);
51+
var expected = @"{ from: 0, size: 10,
52+
filter : {
53+
terms: {
54+
""name"": {
55+
id: ""NEST"",
56+
type: ""elasticsearchprojects"",
57+
index: ""mydefaultindex"",
58+
path: ""name"",
59+
routing: ""dot_net_clients""
60+
},
61+
_cache:true,
62+
_name: ""terms_filter"",
63+
_cache_key: ""NEST_TERMS""
64+
}
65+
}
66+
}";
67+
Assert.True(json.JsonEquals(expected), json);
68+
}
69+
}
70+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Nest.Resolvers;
7+
using Newtonsoft.Json;
8+
9+
namespace Nest
10+
{
11+
/// <summary>
12+
/// http://www.elasticsearch.org/blog/terms-filter-lookup/
13+
/// </summary>
14+
public class TermsLookupFilterDescriptor : FilterBase
15+
{
16+
internal override bool IsConditionless
17+
{
18+
get
19+
{
20+
return this._Type == null || this._Index == null || this._Id.IsNullOrEmpty()
21+
|| this._Path.IsNullOrEmpty();
22+
}
23+
}
24+
25+
[JsonProperty("id")]
26+
internal string _Id { get; set; }
27+
28+
[JsonProperty("type")]
29+
internal TypeNameMarker _Type { get; set; }
30+
31+
[JsonProperty("index")]
32+
internal IndexNameMarker _Index { get; set; }
33+
34+
[JsonProperty("path")]
35+
internal string _Path { get; set; }
36+
37+
[JsonProperty("routing")]
38+
internal string _Routing { get; set; }
39+
40+
41+
public TermsLookupFilterDescriptor Lookup<T>(string field, string id, string index = null, string type = null)
42+
{
43+
this._Path = field;
44+
this._Id = id;
45+
this._Type = type ?? new TypeNameMarker {Type = typeof (T)};
46+
this._Index = index ?? new IndexNameMarker {Type = typeof (T)};
47+
return this;
48+
}
49+
50+
public TermsLookupFilterDescriptor Lookup<T>(Expression<Func<T, object>> field, string id, string index = null, string type = null)
51+
{
52+
var fieldName = new PropertyNameResolver().Resolve(field);
53+
return this.Lookup<T>(fieldName, id, index, type);
54+
}
55+
56+
/// <summary>
57+
/// If not specified will use the default typename for the type specified on Lookup&lt;T&gt;
58+
/// </summary>
59+
public TermsLookupFilterDescriptor Type(string type)
60+
{
61+
this._Type = type;
62+
return this;
63+
}
64+
65+
/// <summary>
66+
/// If not specified will use the default index for the type specified on Lookup&lt;T&gt;
67+
/// </summary>
68+
public TermsLookupFilterDescriptor Index(string index)
69+
{
70+
this._Index = index;
71+
return this;
72+
}
73+
74+
/// <summary>
75+
/// A custom routing value to be used when retrieving the external terms doc.
76+
/// </summary>
77+
public TermsLookupFilterDescriptor Routing(string routing)
78+
{
79+
this._Routing = routing;
80+
return this;
81+
}
82+
}
83+
84+
}

src/Nest/DSL/FilterDescriptor.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,36 @@ public BaseFilter Terms(string field, IEnumerable<string> terms, TermsExecution?
751751
});
752752

753753
}
754+
755+
/// <summary>
756+
/// Filter documents indexed using the geo_shape type.
757+
/// </summary>
758+
public BaseFilter TermsLookup(Expression<Func<T, object>> fieldDescriptor, Action<TermsLookupFilterDescriptor> filterDescriptor)
759+
{
760+
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
761+
return this.TermsLookup(field, filterDescriptor);
762+
}
763+
/// <summary>
764+
/// Filter documents indexed using the geo_shape type.
765+
/// </summary>
766+
public BaseFilter TermsLookup(string field, Action<TermsLookupFilterDescriptor> filterDescriptor)
767+
{
768+
var filter = new TermsLookupFilterDescriptor();
769+
if (filterDescriptor == null)
770+
return CreateConditionlessFilterDescriptor("terms", filter);
771+
772+
filterDescriptor(filter);
773+
if (filter.IsConditionless)
774+
return CreateConditionlessFilterDescriptor("terms", filter);
775+
776+
return this.SetDictionary("terms", field, filter, (d, b) =>
777+
{
778+
b.TermsFilter = d;
779+
});
780+
781+
}
782+
783+
754784
/// <summary>
755785
/// A filter that matches documents using AND boolean operator on other queries.
756786
/// This filter is more performant then bool filter.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Nest.Resolvers;
2+
using Newtonsoft.Json;
3+
4+
namespace Nest
5+
{
6+
/// <summary>
7+
/// An object to describe a geoshape vetor
8+
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
9+
/// </summary>
10+
public class GeoIndexedShapeVector
11+
{
12+
[JsonProperty("id")]
13+
public string Id { get; set; }
14+
15+
[JsonProperty("type")]
16+
public TypeNameMarker Type { get; set; }
17+
18+
[JsonProperty("index")]
19+
public IndexNameMarker Index { get; set; }
20+
21+
[JsonProperty("shape_field_name")]
22+
public string Field { get; set; }
23+
}
24+
}

src/Nest/Domain/DSL/GeoShapeVector.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5-
using Nest.Resolvers;
65
using Newtonsoft.Json;
76

87
namespace Nest
@@ -19,24 +18,4 @@ public class GeoShapeVector
1918
[JsonProperty("coordinates")]
2019
public IEnumerable<IEnumerable<double>> Coordinates { get; set; }
2120
}
22-
23-
24-
/// <summary>
25-
/// An object to describe a geoshape vetor
26-
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
27-
/// </summary>
28-
public class GeoIndexedShapeVector
29-
{
30-
[JsonProperty("id")]
31-
public string Id { get; set; }
32-
33-
[JsonProperty("type")]
34-
public TypeNameMarker Type { get; set; }
35-
36-
[JsonProperty("index")]
37-
public IndexNameMarker Index { get; set; }
38-
39-
[JsonProperty("shape_field_name")]
40-
public string Field { get; set; }
41-
}
4221
}

src/Nest/Nest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@
7070
</ItemGroup>
7171
<ItemGroup>
7272
<Compile Include="Domain\Connection\AsyncRequestOperation.cs" />
73+
<Compile Include="Domain\DSL\GeoIndexedShapeVector.cs" />
7374
<Compile Include="Domain\DSL\GeoShapeVector.cs" />
7475
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
7576
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
7677
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
78+
<Compile Include="DSL\Filter\TermsLookupFilterDescriptor.cs" />
7779
<Compile Include="DSL\Filter\GeoIndexedShapeFilterDescriptor.cs" />
7880
<Compile Include="DSL\Filter\GeoShapeFilterDescriptor.cs" />
7981
<Compile Include="DSL\Suggest\FuzzySuggestDescriptor.cs" />

0 commit comments

Comments
 (0)