Skip to content

Commit c4fefcf

Browse files
committed
support for _source, _source.include, _source.exclude
1 parent a556610 commit c4fefcf

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.DSL.Search
10+
{
11+
public class SourceDescriptor<T> where T : class
12+
{
13+
[JsonProperty("include")]
14+
internal IEnumerable<PropertyPathMarker> _Include { get; set; }
15+
16+
[JsonProperty("exclude")]
17+
internal IEnumerable<PropertyPathMarker> _Exclude { get; set; }
18+
19+
public SourceDescriptor<T> Include(params string[] fields)
20+
{
21+
this._Include = fields.Select(f => (PropertyPathMarker) f).ToList();
22+
return this;
23+
}
24+
public SourceDescriptor<T> Include(params Expression<Func<T, object>>[] fields)
25+
{
26+
this._Include = fields.Select(f => (PropertyPathMarker) f).ToList();
27+
return this;
28+
}
29+
public SourceDescriptor<T> Include(Func<FluentFieldList<T>, FluentFieldList<T>> fields)
30+
{
31+
this._Include = fields(new FluentFieldList<T>()).ToList();
32+
return this;
33+
}
34+
public SourceDescriptor<T> Exclude(params string[] fields)
35+
{
36+
this._Exclude = fields.Select(f => (PropertyPathMarker) f).ToList();
37+
return this;
38+
}
39+
public SourceDescriptor<T> Exclude(params Expression<Func<T, object>>[] fields)
40+
{
41+
this._Exclude = fields.Select(f => (PropertyPathMarker) f).ToList();
42+
return this;
43+
}
44+
public SourceDescriptor<T> Exclude(Func<FluentFieldList<T>, FluentFieldList<T>> fields)
45+
{
46+
this._Exclude = fields(new FluentFieldList<T>()).ToList();
47+
return this;
48+
}
49+
}
50+
}

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Security.Cryptography;
55
using System.Text;
6+
using Nest.DSL.Search;
67
using Newtonsoft.Json;
78
using Newtonsoft.Json.Converters;
89
using Nest.Resolvers.Converters;
@@ -280,8 +281,20 @@ internal RawOrFilterDescriptor<T> _FilterOrRaw
280281
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
281282
internal FluentDictionary<string, ScriptFilterDescriptor> _ScriptFields { get; set; }
282283

284+
[JsonProperty(PropertyName = "_source")]
285+
internal object _Source { get; set; }
283286

287+
public SearchDescriptor<T> Source(bool include = true)
288+
{
289+
this._Source = include;
290+
return this;
291+
}
284292

293+
public SearchDescriptor<T> Source(Func<SourceDescriptor<T>, SourceDescriptor<T>> sourceSelector)
294+
{
295+
this._Source = sourceSelector(new SourceDescriptor<T>());
296+
return this;
297+
}
285298
/// <summary>
286299
/// The number of hits to return. Defaults to 10. When using scroll search type
287300
/// size is actually multiplied by the number of shards!

src/Nest/Domain/Responses/QueryResponse.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,14 @@ public double MaxScore
8080
}
8181
}
8282

83+
private IList<T> _documents;
8384
public IEnumerable<T> Documents
8485
{
8586
get
8687
{
87-
if (this.HitsMetaData != null)
88-
{
89-
foreach (var hit in this.HitsMetaData.Hits)
90-
{
91-
yield return hit.Source;
92-
}
93-
}
88+
if (this.HitsMetaData != null && this._documents == null)
89+
this._documents = this.HitsMetaData.Hits.Select(h => h.Source).ToList();
90+
return this._documents;
9491
}
9592
}
9693

src/Tests/Nest.Tests.Integration/Search/SubClassSupport/SubClassSupportTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class SubClassSupportTests : IntegrationTests
1515
public abstract class MyBaseClass
1616
{
1717
public string Title { get; set; }
18+
public string Description { get; set; }
1819
}
1920
public class ClassA : MyBaseClass
2021
{
@@ -196,6 +197,7 @@ public void MultipleTypesUsingBaseClassMultiSearchSourceInclude()
196197
else
197198
o = new ClassB() { ClassBProperty = Guid.NewGuid().ToString() };
198199
o.Title = Guid.NewGuid().ToString();
200+
o.Description = Guid.NewGuid().ToString();
199201
return o;
200202
});
201203

@@ -207,31 +209,38 @@ public void MultipleTypesUsingBaseClassMultiSearchSourceInclude()
207209
.Types(typeof(ClassA), typeof(ClassB))
208210
.From(0)
209211
.Size(100)
210-
//.SourceExclude()
211-
.Fields(p=>p.Title)
212+
.Source(source=>source
213+
.Include(i=>i.Add(p=>p.Title).Add("classBProperty"))
214+
)
212215
.MatchAll()
213216
)
214217
.Search<MyBaseClass>("using_selector", s => s.AllIndices()
215218
.Types("classa", "classb")
216219
.ConcreteTypeSelector((o, h) => o.classBProperty != null ? typeof(ClassB) : typeof(ClassA))
217220
.From(0)
218221
.Size(100)
219-
.Fields(f=>f.Add(p=>p.Title).Add("classBProperty"))
222+
.Source(source=>source
223+
.Include(i=>i.Add(p=>p.Description).Add("classBProperty"))
224+
)
220225
.MatchAll()
221226
)
222227
);
223228
Assert.True(queryResults.IsValid);
224229
var firstResult = queryResults.GetResponse<MyBaseClass>("using_types");
225230

226231
Assert.True(firstResult.Documents.Any());
227-
firstResult.Hits.OfType<Hit<ClassA>>().Any().Should().BeTrue();
228-
firstResult.Hits.OfType<Hit<ClassB>>().Any().Should().BeTrue();
232+
firstResult.Documents.All(d => !d.Title.IsNullOrEmpty());
233+
firstResult.Documents.All(d => d.Description.IsNullOrEmpty());
234+
firstResult.Documents.OfType<ClassA>().Any().Should().BeTrue();
235+
firstResult.Documents.OfType<ClassB>().Any().Should().BeTrue();
229236

230237
var secondResult = queryResults.GetResponse<MyBaseClass>("using_selector");
231238

232239
Assert.True(secondResult.Documents.Any());
233-
secondResult.Hits.OfType<Hit<ClassA>>().Any().Should().BeTrue();
234-
secondResult.Hits.OfType<Hit<ClassB>>().Any().Should().BeTrue();
240+
secondResult.Documents.All(d => d.Title.IsNullOrEmpty());
241+
secondResult.Documents.All(d => !d.Description.IsNullOrEmpty());
242+
secondResult.Documents.OfType<ClassA>().Any().Should().BeTrue();
243+
secondResult.Documents.OfType<ClassB>().Any().Should().BeTrue();
235244
}
236245
}
237246
}

0 commit comments

Comments
 (0)