Skip to content

Commit 3c67685

Browse files
committed
addendum #610 can now specify documents in body for mtermvector calls
1 parent a3b36f3 commit 3c67685

File tree

6 files changed

+234
-76
lines changed

6 files changed

+234
-76
lines changed

src/Nest/DSL/MultiTermVectorsDescriptor.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Linq.Expressions;
45
using System.Text;
56
using Elasticsearch.Net;
7+
using Nest.Resolvers;
8+
using Newtonsoft.Json;
69

710
namespace Nest
811
{
912
[DescriptorFor("Mtermvectors")]
1013
public partial class MultiTermVectorsDescriptor<T> : IndexTypePathTypedDescriptor<MultiTermVectorsDescriptor<T>, MultiTermVectorsRequestParameters, T>
1114
, IPathInfo<MultiTermVectorsRequestParameters> where T : class
1215
{
16+
[JsonProperty("docs")]
17+
internal IEnumerable<MultiTermVectorDocument> _Documents { get; set;}
18+
19+
public MultiTermVectorsDescriptor<T> Documents(params Func<MultiTermVectorDocumentDescriptor<T>, IMultiTermVectorDocumentDescriptor>[] documentSelectors)
20+
{
21+
this._Documents = documentSelectors.Select(s => s(new MultiTermVectorDocumentDescriptor<T>()).GetDocument()).Where(d=>d!= null).ToList();
22+
return this;
23+
}
24+
1325
ElasticsearchPathInfo<MultiTermVectorsRequestParameters> IPathInfo<MultiTermVectorsRequestParameters>.ToPathInfo(IConnectionSettingsValues settings)
1426
{
1527
var pathInfo = base.ToPathInfo(settings, this._QueryString);
@@ -18,4 +30,90 @@ ElasticsearchPathInfo<MultiTermVectorsRequestParameters> IPathInfo<MultiTermVect
1830
return pathInfo;
1931
}
2032
}
33+
34+
public interface IMultiTermVectorDocumentDescriptor
35+
{
36+
MultiTermVectorDocument Document { get; set; }
37+
MultiTermVectorDocument GetDocument();
38+
}
39+
40+
public class MultiTermVectorDocumentDescriptor<T> : DocumentOptionalPathDescriptorBase<MultiTermVectorDocumentDescriptor<T>, T, MultiTermVectorsRequestParameters>, IMultiTermVectorDocumentDescriptor where T : class
41+
{
42+
43+
MultiTermVectorDocument IMultiTermVectorDocumentDescriptor.Document { get; set; }
44+
MultiTermVectorDocument IMultiTermVectorDocumentDescriptor.GetDocument()
45+
{
46+
IMultiTermVectorDocumentDescriptor d = this;
47+
if (d.Document == null) d.Document = new MultiTermVectorDocument();
48+
d.Document.Id = this._Id;
49+
d.Document.Type = this._Type;
50+
d.Document.Index = this._Index;
51+
return d.Document;
52+
}
53+
54+
private MultiTermVectorDocumentDescriptor<T> SetDocValue(Action<IMultiTermVectorDocumentDescriptor> setter)
55+
{
56+
IMultiTermVectorDocumentDescriptor d = this;
57+
if (d.Document == null) d.Document = new MultiTermVectorDocument();
58+
setter(d);
59+
return this;
60+
}
61+
62+
public MultiTermVectorDocumentDescriptor<T> Fields(params string[] fields)
63+
{
64+
return this.SetDocValue(d => d.Document.Fields = fields.Select(f => (PropertyPathMarker)f).ToList());
65+
}
66+
public MultiTermVectorDocumentDescriptor<T> Fields(params Expression<Func<T, object>>[] fields)
67+
{
68+
return this.SetDocValue(d => d.Document.Fields = fields.Select(f => (PropertyPathMarker)f).ToList());
69+
}
70+
public MultiTermVectorDocumentDescriptor<T> Fields(Func<FluentFieldList<T>, FluentFieldList<T>> fields)
71+
{
72+
return this.SetDocValue(d => d.Document.Fields = fields(new FluentFieldList<T>()).ToList());
73+
}
74+
75+
76+
public MultiTermVectorDocumentDescriptor<T> Offsets(bool offsets = true)
77+
{
78+
return this.SetDocValue(d =>d.Document.Offsets = offsets);
79+
}
80+
public MultiTermVectorDocumentDescriptor<T> Payloads(bool payloads = true)
81+
{
82+
return this.SetDocValue(d =>d.Document.Payloads = payloads);
83+
}
84+
public MultiTermVectorDocumentDescriptor<T> Positions(bool positions = true)
85+
{
86+
return this.SetDocValue(d =>d.Document.Positions = positions);
87+
}
88+
public MultiTermVectorDocumentDescriptor<T> TermStatistics(bool termStatistics = true)
89+
{
90+
return this.SetDocValue(d => d.Document.TermStatistics = termStatistics);
91+
}
92+
public MultiTermVectorDocumentDescriptor<T> FieldStatistics (bool fieldStatistics = true)
93+
{
94+
return this.SetDocValue(d => d.Document.FieldStatistics = fieldStatistics);
95+
}
96+
}
97+
98+
public class MultiTermVectorDocument
99+
{
100+
[JsonProperty("_index")]
101+
public IndexNameMarker Index { get; set; }
102+
[JsonProperty("_type")]
103+
public TypeNameMarker Type { get; set; }
104+
[JsonProperty("_id")]
105+
public string Id { get; set; }
106+
[JsonProperty("fields")]
107+
public IEnumerable<PropertyPathMarker> Fields { get; set; }
108+
[JsonProperty("offsets")]
109+
public bool? Offsets { get; set; }
110+
[JsonProperty("payloads")]
111+
public bool? Payloads { get; set; }
112+
[JsonProperty("positions")]
113+
public bool? Positions { get; set; }
114+
[JsonProperty("term_statistics")]
115+
public bool? TermStatistics { get; set; }
116+
[JsonProperty("field_statistics")]
117+
public bool? FieldStatistics { get; set; }
118+
}
21119
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Principal;
5+
using System.Text;
6+
using Elasticsearch.Net;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Converters;
9+
using Nest.Resolvers.Converters;
10+
using System.Linq.Expressions;
11+
using Nest.Resolvers;
12+
13+
namespace Nest
14+
{
15+
/// <summary>
16+
/// Provides a base for descriptors that need to describe a path in the form of
17+
/// <pre>
18+
/// /{index}/{type}/{id}
19+
/// </pre>
20+
/// if one of the parameters is not explicitly specified this will fall back to the defaults for type
21+
/// this version won't throw if any of the parts are inferred to be empty<para>T</para>
22+
/// </summary>
23+
public class DocumentOptionalPathDescriptorBase<TDescriptor, T, TParameters> : BasePathDescriptor<TDescriptor>
24+
where TDescriptor : DocumentOptionalPathDescriptorBase<TDescriptor, T, TParameters>, new()
25+
where T : class
26+
where TParameters : FluentRequestParameters<TParameters>, new()
27+
{
28+
29+
internal IndexNameMarker _Index { get; set; }
30+
internal TypeNameMarker _Type { get; set; }
31+
internal string _Id { get; set; }
32+
internal T _Object { get; set; }
33+
34+
public TDescriptor Index(string index)
35+
{
36+
this._Index = index;
37+
return (TDescriptor)this;
38+
}
39+
40+
public TDescriptor Index(Type index)
41+
{
42+
this._Index = index;
43+
return (TDescriptor)this;
44+
}
45+
46+
public TDescriptor Index<TAlternative>() where TAlternative : class
47+
{
48+
this._Index = typeof(TAlternative);
49+
return (TDescriptor)this;
50+
}
51+
52+
public TDescriptor Type(string type)
53+
{
54+
this._Type = type;
55+
return (TDescriptor)this;
56+
}
57+
public TDescriptor Type(Type type)
58+
{
59+
this._Type = type;
60+
return (TDescriptor)this;
61+
}
62+
public TDescriptor Type<TAlternative>() where TAlternative : class
63+
{
64+
this._Type = typeof(TAlternative);
65+
return (TDescriptor)this;
66+
}
67+
public TDescriptor Id(long id)
68+
{
69+
return this.Id(id.ToString());
70+
}
71+
public TDescriptor Id(string id)
72+
{
73+
this._Id = id;
74+
return (TDescriptor)this;
75+
}
76+
public TDescriptor Object(T @object)
77+
{
78+
this._Object = @object;
79+
return (TDescriptor)this;
80+
}
81+
internal virtual ElasticsearchPathInfo<TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
82+
{
83+
var inferrer = new ElasticInferrer(settings);
84+
var index = this._Index != null ? inferrer.IndexName(this._Index) : inferrer.IndexName<T>();
85+
var type = this._Type != null ? inferrer.TypeName(this._Type) : inferrer.TypeName<T>();
86+
var id = this._Id ?? inferrer.Id(this._Object);
87+
88+
var pathInfo = base.ToPathInfo(queryString);
89+
pathInfo.Index = index;
90+
pathInfo.Type = type;
91+
pathInfo.Id = id;
92+
return pathInfo;
93+
}
94+
95+
}
96+
}

src/Nest/DSL/Paths/DocumentPathDescriptor.cs

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,80 +19,20 @@ namespace Nest
1919
/// </pre>
2020
/// if one of the parameters is not explicitly specified this will fall back to the defaults for type <para>T</para>
2121
/// </summary>
22-
public class DocumentPathDescriptorBase<TDescriptor, T, TParameters> : BasePathDescriptor<TDescriptor>
22+
public class DocumentPathDescriptorBase<TDescriptor, T, TParameters> : DocumentOptionalPathDescriptorBase<TDescriptor, T, TParameters>
2323
where TDescriptor : DocumentPathDescriptorBase<TDescriptor, T, TParameters>, new()
2424
where T : class
2525
where TParameters : FluentRequestParameters<TParameters>, new()
2626
{
2727

28-
internal IndexNameMarker _Index { get; set; }
29-
internal TypeNameMarker _Type { get; set; }
30-
internal string _Id { get; set; }
31-
internal T _Object { get; set; }
32-
33-
public TDescriptor Index(string index)
34-
{
35-
this._Index = index;
36-
return (TDescriptor)this;
37-
}
38-
39-
public TDescriptor Index(Type index)
40-
{
41-
this._Index = index;
42-
return (TDescriptor)this;
43-
}
44-
45-
public TDescriptor Index<TAlternative>() where TAlternative : class
28+
internal override ElasticsearchPathInfo<TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
4629
{
47-
this._Index = typeof(TAlternative);
48-
return (TDescriptor)this;
49-
}
50-
51-
public TDescriptor Type(string type)
52-
{
53-
this._Type = type;
54-
return (TDescriptor)this;
55-
}
56-
public TDescriptor Type(Type type)
57-
{
58-
this._Type = type;
59-
return (TDescriptor)this;
60-
}
61-
public TDescriptor Type<TAlternative>() where TAlternative : class
62-
{
63-
this._Type = typeof(TAlternative);
64-
return (TDescriptor)this;
65-
}
66-
public TDescriptor Id(long id)
67-
{
68-
return this.Id(id.ToString());
69-
}
70-
public TDescriptor Id(string id)
71-
{
72-
this._Id = id;
73-
return (TDescriptor)this;
74-
}
75-
public TDescriptor Object(T @object)
76-
{
77-
this._Object = @object;
78-
return (TDescriptor)this;
79-
}
80-
internal virtual ElasticsearchPathInfo<TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
81-
{
82-
var inferrer = new ElasticInferrer(settings);
83-
var index = this._Index != null ? inferrer.IndexName(this._Index) : inferrer.IndexName<T>();
84-
var type = this._Type != null ? inferrer.TypeName(this._Type) : inferrer.TypeName<T>();
85-
var id = this._Id ?? inferrer.Id(this._Object);
30+
var pathInfo = base.ToPathInfo(queryString);
8631

87-
index.ThrowIfNullOrEmpty("index");
88-
type.ThrowIfNullOrEmpty("type");
89-
id.ThrowIfNullOrEmpty("id");
90-
32+
pathInfo.Index.ThrowIfNullOrEmpty("index");
33+
pathInfo.Type.ThrowIfNullOrEmpty("type");
34+
pathInfo.Id.ThrowIfNullOrEmpty("id");
9135

92-
var pathInfo = base.ToPathInfo(queryString);
93-
pathInfo.Index = index;
94-
pathInfo.Type = type;
95-
pathInfo.Id = id;
9636
return pathInfo;
9737
}
9838

src/Nest/DSL/TermVectorDescriptor.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
namespace Nest
88
{
9-
public partial class TermvectorDescriptor<T> : DocumentPathDescriptorBase<TermvectorDescriptor<T>, T, TermvectorRequestParameters>
10-
, IPathInfo<TermvectorRequestParameters> where T : class
11-
{
12-
ElasticsearchPathInfo<TermvectorRequestParameters> IPathInfo<TermvectorRequestParameters>.ToPathInfo(IConnectionSettingsValues settings)
13-
{
14-
var pathInfo = base.ToPathInfo(settings, this._QueryString);
15-
pathInfo.HttpMethod = PathInfoHttpMethod.GET;
9+
public partial class TermvectorDescriptor<T> : DocumentPathDescriptorBase<TermvectorDescriptor<T>, T, TermvectorRequestParameters>
10+
, IPathInfo<TermvectorRequestParameters> where T : class
11+
{
12+
ElasticsearchPathInfo<TermvectorRequestParameters> IPathInfo<TermvectorRequestParameters>.ToPathInfo(IConnectionSettingsValues settings)
13+
{
14+
var pathInfo = base.ToPathInfo(settings, this._QueryString);
15+
pathInfo.HttpMethod = PathInfoHttpMethod.GET;
1616

17-
return pathInfo;
18-
}
19-
}
17+
return pathInfo;
18+
}
19+
}
2020
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<Compile Include="DSL\DeleteSnapshotDescriptor.cs" />
143143
<Compile Include="DSL\GetSnapshotDescriptor.cs" />
144144
<Compile Include="DSL\MultiTermVectorsDescriptor.cs" />
145+
<Compile Include="DSL\Paths\DocumentOptionalPathDescriptor.cs" />
145146
<Compile Include="DSL\RestoreDescriptor.cs" />
146147
<Compile Include="DSL\SnapshotDescriptor.cs" />
147148
<Compile Include="DSL\DeleteRepositoryDescriptor.cs" />

src/Tests/Nest.Tests.Integration/Core/TermVectors/MultiTermVectorsTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ public void MultiTermVectorsTest()
3232
document.TermVectors.First().Key.Should().Be("content");
3333
}
3434
}
35+
36+
[Test]
37+
public void MultiTermVectorsTest_DocumentsInBody()
38+
{
39+
var result = _client.MultiTermVectors<ElasticsearchProject>(s => s
40+
.Fields(ep => ep.Content)
41+
.Documents(
42+
m=>m.Id(1).TermStatistics(),
43+
m=>m.Id(2).FieldStatistics().Offsets(false)
44+
)
45+
);
46+
47+
result.IsValid.Should().BeTrue();
48+
49+
result.Documents.Should().NotBeNull();
50+
result.Documents.Count().Should().Be(2);
51+
52+
foreach (var document in result.Documents)
53+
{
54+
document.TermVectors.Count().Should().Be(1);
55+
document.TermVectors.First().Key.Should().Be("content");
56+
}
57+
}
3558

3659
[Test]
3760
public void MultiTermVectorsNonExistentIdTest()

0 commit comments

Comments
 (0)