Skip to content

Commit 409804c

Browse files
committed
Append #2231 Made field_masking_span a top level query
As per elastic/elasticsearch#3007 this is now possible (before 1.x GA) Added additional serialization tests
1 parent 53797d1 commit 409804c

File tree

4 files changed

+89
-43
lines changed

4 files changed

+89
-43
lines changed

src/Nest/DSL/Query/QueryDescriptor.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,27 @@ public QueryContainer SpanMultiTerm(Action<SpanMultiTermQueryDescriptor<T>> sele
886886
return this.New(span, q => q.SpanMultiTerm = span);
887887
}
888888

889+
/// <summary>
890+
/// Wrapper to allow span queries participate in composite single-field span queries by
891+
/// 'lying' about their search field. The span field masking query maps to Lucene `SpanFieldMaskingQuery`
892+
///
893+
/// This can be used to support queries like `span-near` or `span-or` across different fields, which is not ordinarily permitted.
894+
/// Span field masking query is invaluable in conjunction with *multi-fields* when same content is
895+
/// indexed with multiple analyzers. For instance we could index a field with the standard analyzer
896+
/// which breaks text up into words, and again with the english analyzer which stems words into their root
897+
/// form. That will add more precision to queries. Wrap a multi term query (one of fuzzy, prefix, term range or regexp query)
898+
/// as a span query so it can be nested.
899+
/// </summary>
900+
public QueryContainer SpanFieldMasking(Action<SpanFieldMaskingQueryDescriptor<T>> selector)
901+
{
902+
selector.ThrowIfNull("selector");
903+
var span = new SpanFieldMaskingQueryDescriptor<T>();
904+
selector(span);
905+
906+
return this.New(span, q => q.SpanFieldMasking= span);
907+
}
908+
909+
889910
/// <summary>
890911
/// custom_score query allows to wrap another query and customize the scoring of it optionally with a
891912
/// computation derived from other field values in the doc (numeric ones) using script or boost expression

src/Nest/DSL/Query/SpanFieldMaskingQueryDescriptor.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,47 @@ namespace Nest
1010
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
1111
[JsonConverter(typeof(ReadAsTypeConverter<SpanFieldMaskingQueryDescriptor<object>>))]
1212
public interface ISpanFieldMaskingQuery : ISpanSubQuery, IFieldNameQuery
13-
{
14-
[JsonProperty(PropertyName = "field")]
15-
PropertyPathMarker Field { get; set; }
16-
17-
[JsonProperty(PropertyName = "query")]
13+
{
14+
[JsonProperty(PropertyName = "field")]
15+
PropertyPathMarker Field { get; set; }
16+
17+
[JsonProperty(PropertyName = "query")]
1818
ISpanQuery Query { get; set; }
1919
}
2020

2121
public class SpanFieldMaskingQuery : PlainQuery, ISpanFieldMaskingQuery
22-
{
22+
{
2323
protected override void WrapInContainer(IQueryContainer container)
2424
{
2525
container.SpanFieldMasking = this;
2626
}
2727

2828
bool IQuery.IsConditionless { get { return false; } }
2929

30-
public string Name { get; set; }
31-
public PropertyPathMarker Field { get; set; }
32-
public ISpanQuery Query { get; set; }
30+
public string Name { get; set; }
31+
public PropertyPathMarker Field { get; set; }
32+
public ISpanQuery Query { get; set; }
3333

34-
PropertyPathMarker IFieldNameQuery.GetFieldName()
35-
{
36-
return this.Field;
37-
}
34+
PropertyPathMarker IFieldNameQuery.GetFieldName()
35+
{
36+
return this.Field;
37+
}
3838

39-
void IFieldNameQuery.SetFieldName(string fieldName)
40-
{
41-
this.Field = fieldName;
42-
}
43-
}
39+
void IFieldNameQuery.SetFieldName(string fieldName)
40+
{
41+
this.Field = fieldName;
42+
}
43+
}
4444

4545
public class SpanFieldMaskingQueryDescriptor<T> : ISpanFieldMaskingQuery where T : class
4646
{
4747
ISpanFieldMaskingQuery Self { get { return this; } }
48-
bool IQuery.IsConditionless { get { return false; } }
48+
bool IQuery.IsConditionless { get { return false; } }
4949

50-
ISpanQuery ISpanFieldMaskingQuery.Query { get; set; }
51-
PropertyPathMarker ISpanFieldMaskingQuery.Field { get; set; }
52-
string IQuery.Name { get; set; }
53-
50+
ISpanQuery ISpanFieldMaskingQuery.Query { get; set; }
51+
PropertyPathMarker ISpanFieldMaskingQuery.Field { get; set; }
52+
string IQuery.Name { get; set; }
53+
5454
public SpanFieldMaskingQueryDescriptor<T> Name(string name)
5555
{
5656
Self.Name = name;
@@ -71,26 +71,26 @@ public SpanFieldMaskingQueryDescriptor<T> OnField(string field)
7171
return this;
7272
}
7373

74-
public SpanFieldMaskingQueryDescriptor<T> OnField(Expression<Func<T, object>> field)
75-
{
76-
Self.Field = field;
77-
return this;
78-
}
74+
public SpanFieldMaskingQueryDescriptor<T> OnField(Expression<Func<T, object>> field)
75+
{
76+
Self.Field = field;
77+
return this;
78+
}
7979

80-
public SpanFieldMaskingQueryDescriptor<T> OnField<K>(Expression<Func<T, K>> field)
81-
{
82-
Self.Field = field;
83-
return this;
84-
}
85-
86-
public PropertyPathMarker GetFieldName()
87-
{
88-
return Self.Field;
89-
}
80+
public SpanFieldMaskingQueryDescriptor<T> OnField<K>(Expression<Func<T, K>> field)
81+
{
82+
Self.Field = field;
83+
return this;
84+
}
85+
86+
public PropertyPathMarker GetFieldName()
87+
{
88+
return Self.Field;
89+
}
9090

91-
public void SetFieldName(string fieldName)
92-
{
93-
Self.Field = fieldName;
94-
}
95-
}
91+
public void SetFieldName(string fieldName)
92+
{
93+
Self.Field = fieldName;
94+
}
95+
}
9696
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@
406406
<Compile Include="QueryParsers\Queries\SpanOrQueryTests.cs" />
407407
<Compile Include="QueryParsers\Queries\SpanTermQueryTests.cs" />
408408
<Compile Include="QueryParsers\Queries\TermQueryTests.cs" />
409+
<Compile Include="QueryParsers\Queries\FieldMaskingSpanQueryTests.cs" />
409410
<Compile Include="QueryParsers\Queries\TermsQueryTests.cs" />
410411
<Compile Include="QueryParsers\Queries\WildCardQueryTests.cs" />
411412
<Compile Include="QueryParsers\Visitor\DslPrettyPrintVisitor.cs" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using FluentAssertions;
2+
using Nest.Tests.MockData.Domain;
3+
using NUnit.Framework;
4+
5+
namespace Nest.Tests.Unit.QueryParsers.Queries
6+
{
7+
[TestFixture]
8+
public class FieldMaskingSpanQueryTests : ParseQueryTestsBase
9+
{
10+
[Test]
11+
public void FieldMaskingSpan_Deserializes()
12+
{
13+
var q = this.SerializeThenDeserialize(
14+
f => f.SpanFieldMasking,
15+
f => f.SpanFieldMasking(s => s.OnField(p => p.Name).Query(qq => qq.SpanTerm("x", "y")))
16+
);
17+
18+
19+
q.Field.Should().NotBeNull().And.Be("name");
20+
q.Query.Should().NotBeNull();
21+
q.Query.SpanTermQueryDescriptor.Should().NotBeNull();
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)