Skip to content

Commit f9828d5

Browse files
committed
fix #256 allow queries to be included in the percolate calls, also added *Async variants and removed overloads in favor of descriptor call
1 parent 1e63c7b commit f9828d5

File tree

10 files changed

+299
-81
lines changed

10 files changed

+299
-81
lines changed

src/Nest.Connection.Thrift/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[assembly: System.Runtime.InteropServices.ComVisible(false)]
1616
[assembly: System.CLSCompliant(true)]
1717
[assembly: System.Runtime.InteropServices.Guid("4d165338-2060-4641-8be6-b7aacbdee52d")]
18-
[assembly: System.Reflection.AssemblyVersion("0.10.0.0")]
19-
[assembly: System.Reflection.AssemblyFileVersion("0.10.0.0")]
18+
[assembly: System.Reflection.AssemblyVersion("0.10.1.0")]
19+
[assembly: System.Reflection.AssemblyFileVersion("0.10.1.0")]
2020

2121

src/Nest.Dsl.Factory/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[assembly: System.Runtime.InteropServices.ComVisible(false)]
1717
[assembly: System.CLSCompliant(true)]
1818
[assembly: System.Runtime.InteropServices.Guid("665cc582-c91f-4f6c-924a-614289fa9449")]
19-
[assembly: System.Reflection.AssemblyVersion("0.10.0.0")]
20-
[assembly: System.Reflection.AssemblyFileVersion("0.10.0.0")]
19+
[assembly: System.Reflection.AssemblyVersion("0.10.1.0")]
20+
[assembly: System.Reflection.AssemblyFileVersion("0.10.1.0")]
2121

2222

src/Nest.Tests.Integration/Search/PercolateTests.cs

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public void RegisterPercolateTest()
1515
{
1616
var name = "mypercolator";
1717
var c = this._client;
18-
var r = c.RegisterPercolator<ElasticSearchProject>(name, q => q.Term(f => f.Name, "elasticsearch.pm"));
18+
var r = c.RegisterPercolator<ElasticSearchProject>(p => p
19+
.Name(name)
20+
.Query(q => q
21+
.Term(f => f.Name, "elasticsearch.pm")
22+
)
23+
);
1924
Assert.True(r.IsValid);
2025
Assert.True(r.OK);
2126
Assert.AreEqual(r.Type, ElasticsearchConfiguration.DefaultIndex);
@@ -27,7 +32,13 @@ public void UnregisterPercolateTest()
2732
{
2833
var name = "mypercolator";
2934
var c = this._client;
30-
var r = c.RegisterPercolator<ElasticSearchProject>(name, q => q.Term(f => f.Name, "elasticsearch.pm"));
35+
var r = c.RegisterPercolator<ElasticSearchProject>(p => p
36+
.Name(name)
37+
.Add("color", "blue")
38+
.Query(q => q
39+
.Term(f => f.Name, "elasticsearch.pm")
40+
)
41+
);
3142
Assert.True(r.IsValid);
3243
Assert.True(r.OK);
3344
Assert.AreEqual(r.Type, ElasticsearchConfiguration.DefaultIndex);
@@ -53,12 +64,12 @@ public void PercolateDoc()
5364
this.RegisterPercolateTest(); // I feel a little dirty.
5465
var c = this._client;
5566
var name = "mypercolator";
56-
var r = c.Percolate(new ElasticSearchProject()
67+
var r = c.Percolate<ElasticSearchProject>(p=>p.Object(new ElasticSearchProject()
5768
{
5869
Name = "elasticsearch.pm",
5970
Country = "netherlands",
6071
LOC = 100000, //Too many :(
61-
});
72+
}));
6273
Assert.True(r.IsValid);
6374
Assert.True(r.OK);
6475
Assert.NotNull(r.Matches);
@@ -71,23 +82,74 @@ public void PercolateTypedDoc()
7182
this.RegisterPercolateTest(); // I feel a little dirty.
7283
var c = this._client;
7384
var name = "eclecticsearch";
74-
var r = c.RegisterPercolator<ElasticSearchProject>(name, q => q.Term(f => f.Country, "netherlands"));
85+
var r = c.RegisterPercolator<ElasticSearchProject>(p => p
86+
.Name(name)
87+
.Query(q => q
88+
.Term(f => f.Country, "netherlands")
89+
)
90+
);
7591
Assert.True(r.IsValid);
7692
Assert.True(r.OK);
77-
var percolateResponse = this._client.Percolate(
93+
var percolateResponse = this._client.Percolate<ElasticSearchProject>(p=>p.Object(
7894
new ElasticSearchProject()
7995
{
8096
Name = "NEST",
8197
Country = "netherlands",
8298
LOC = 100000, //Too many :(
8399
}
84-
);
100+
));
85101
Assert.True(percolateResponse.IsValid);
86102
Assert.True(percolateResponse.OK);
87103
Assert.NotNull(percolateResponse.Matches);
88104
Assert.True(percolateResponse.Matches.Contains(name));
89105

90106
var re = c.UnregisterPercolator<ElasticSearchProject>(name);
91107
}
108+
[Test]
109+
public void PercolateTypedDocWithQuery()
110+
{
111+
var c = this._client;
112+
var name = "eclecticsearch" + ElasticsearchConfiguration.NewUniqueIndexName();
113+
var re = c.UnregisterPercolator<ElasticSearchProject>(name);
114+
var r = c.RegisterPercolator<ElasticSearchProject>(p => p
115+
.Name(name)
116+
.Add("color", "blue")
117+
.Query(q => q
118+
.Term(f => f.Country, "netherlands")
119+
)
120+
);
121+
Assert.True(r.IsValid);
122+
Assert.True(r.OK);
123+
var percolateResponse = this._client.Percolate<ElasticSearchProject>(p => p
124+
.Object(new ElasticSearchProject()
125+
{
126+
Name = "NEST",
127+
Country = "netherlands",
128+
LOC = 100000, //Too many :(
129+
})
130+
.Query(q=>q.Term("color", "blue"))
131+
);
132+
Assert.True(percolateResponse.IsValid);
133+
Assert.True(percolateResponse.OK);
134+
Assert.NotNull(percolateResponse.Matches);
135+
Assert.True(percolateResponse.Matches.Contains(name));
136+
137+
//should not match since we registered with the color blue
138+
percolateResponse = this._client.Percolate<ElasticSearchProject>(p => p
139+
.Object(new ElasticSearchProject()
140+
{
141+
Name = "NEST",
142+
Country = "netherlands",
143+
LOC = 100000, //Too many :(
144+
})
145+
.Query(q => q.Term("color", "green"))
146+
);
147+
Assert.True(percolateResponse.IsValid);
148+
Assert.True(percolateResponse.OK);
149+
Assert.NotNull(percolateResponse.Matches);
150+
Assert.False(percolateResponse.Matches.Contains(name));
151+
152+
re = c.UnregisterPercolator<ElasticSearchProject>(name);
153+
}
92154
}
93155
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Converters;
7+
using Nest.Resolvers.Converters;
8+
using System.Linq.Expressions;
9+
using Nest.Resolvers;
10+
11+
namespace Nest
12+
{
13+
public class PercolateDescriptor<T> where T : class
14+
{
15+
protected readonly TypeNameResolver typeNameResolver;
16+
17+
public PercolateDescriptor()
18+
{
19+
this.typeNameResolver = new TypeNameResolver();
20+
}
21+
22+
internal string _Index { get; set; }
23+
internal string _Type { get; set; }
24+
internal string _Id { get; set; }
25+
[JsonProperty(PropertyName = "query")]
26+
internal BaseQuery _Query { get; set; }
27+
28+
[JsonProperty(PropertyName = "doc")]
29+
internal T _Document { get; set; }
30+
31+
/// <summary>
32+
/// Explicitly specify an index, otherwise the default index for T is used.
33+
/// </summary>
34+
public PercolateDescriptor<T> Index(string index)
35+
{
36+
this._Index = index;
37+
return this;
38+
}
39+
40+
/// <summary>
41+
/// Explicitly specify an type, otherwise the default type for T is used.
42+
/// </summary>
43+
public PercolateDescriptor<T> Type(string type)
44+
{
45+
this._Type = type;
46+
return this;
47+
}
48+
49+
/// <summary>
50+
/// The object to perculate
51+
/// </summary>
52+
public PercolateDescriptor<T> Object(T @object)
53+
{
54+
this._Document = @object;
55+
return this;
56+
}
57+
58+
/// <summary>
59+
/// Optionally specify more search options such as facets, from/to etcetera.
60+
/// </summary>
61+
public PercolateDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
62+
{
63+
querySelector.ThrowIfNull("querySelector");
64+
var d = querySelector(new QueryDescriptor<T>());
65+
this._Query = d;
66+
return this;
67+
}
68+
}
69+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Converters;
7+
using Nest.Resolvers.Converters;
8+
using System.Linq.Expressions;
9+
using Nest.Resolvers;
10+
11+
namespace Nest
12+
{
13+
public class PercolatorDescriptor<T> : FluentDictionary<string, object> where T : class
14+
{
15+
internal string _Index { get; set; }
16+
internal string _Name { get; set; }
17+
18+
[JsonProperty(PropertyName = "query")]
19+
internal BaseQuery _Query { get; set; }
20+
21+
/// <summary>
22+
/// Explicitly specify an index, otherwise the default index for T is used.
23+
/// </summary>
24+
public PercolatorDescriptor<T> Index(string index)
25+
{
26+
this._Index = index;
27+
return this;
28+
}
29+
30+
/// <summary>
31+
/// Explicitly specify an type, otherwise the default type for T is used.
32+
/// </summary>
33+
public PercolatorDescriptor<T> Name(string name)
34+
{
35+
this._Name = name;
36+
return this;
37+
}
38+
/// <summary>
39+
/// Add metadata associated with this percolator query document
40+
/// </summary>
41+
public new PercolatorDescriptor<T> Add(string key, object value)
42+
{
43+
base.Add(key, value);
44+
return this;
45+
}
46+
47+
//Remove metadata associated with this percolator query document
48+
public PercolatorDescriptor<T> Remove(string key)
49+
{
50+
base.Remove(key);
51+
return this;
52+
}
53+
54+
/// <summary>
55+
/// Optionally specify more search options such as facets, from/to etcetera.
56+
/// </summary>
57+
public PercolatorDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
58+
{
59+
querySelector.ThrowIfNull("querySelector");
60+
var d = querySelector(new QueryDescriptor<T>());
61+
this._Query = d;
62+
base.Add("query", d);
63+
return this;
64+
}
65+
}
66+
}

src/Nest/Domain/PathAndData.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Nest.Resolvers;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Linq.Expressions;
8+
using System.Text;
9+
10+
namespace Nest
11+
{
12+
internal class PathAndData
13+
{
14+
public string Path { get; set; }
15+
public string Data { get; set; }
16+
}
17+
}

0 commit comments

Comments
 (0)