Skip to content

Commit

Permalink
Append #2231 Made field_masking_span a top level query
Browse files Browse the repository at this point in the history
As per elastic/elasticsearch#3007 this is now possible (before 1.x GA)

Added additional serialization tests
  • Loading branch information
Mpdreamz committed Sep 12, 2016
1 parent 53797d1 commit 409804c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 43 deletions.
21 changes: 21 additions & 0 deletions src/Nest/DSL/Query/QueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,27 @@ public QueryContainer SpanMultiTerm(Action<SpanMultiTermQueryDescriptor<T>> sele
return this.New(span, q => q.SpanMultiTerm = span);
}

/// <summary>
/// Wrapper to allow span queries participate in composite single-field span queries by
/// 'lying' about their search field. The span field masking query maps to Lucene `SpanFieldMaskingQuery`
///
/// This can be used to support queries like `span-near` or `span-or` across different fields, which is not ordinarily permitted.
/// Span field masking query is invaluable in conjunction with *multi-fields* when same content is
/// indexed with multiple analyzers. For instance we could index a field with the standard analyzer
/// which breaks text up into words, and again with the english analyzer which stems words into their root
/// form. That will add more precision to queries. Wrap a multi term query (one of fuzzy, prefix, term range or regexp query)
/// as a span query so it can be nested.
/// </summary>
public QueryContainer SpanFieldMasking(Action<SpanFieldMaskingQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var span = new SpanFieldMaskingQueryDescriptor<T>();
selector(span);

return this.New(span, q => q.SpanFieldMasking= span);
}


/// <summary>
/// custom_score query allows to wrap another query and customize the scoring of it optionally with a
/// computation derived from other field values in the doc (numeric ones) using script or boost expression
Expand Down
86 changes: 43 additions & 43 deletions src/Nest/DSL/Query/SpanFieldMaskingQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@ namespace Nest
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeConverter<SpanFieldMaskingQueryDescriptor<object>>))]
public interface ISpanFieldMaskingQuery : ISpanSubQuery, IFieldNameQuery
{
[JsonProperty(PropertyName = "field")]
PropertyPathMarker Field { get; set; }
[JsonProperty(PropertyName = "query")]
{
[JsonProperty(PropertyName = "field")]
PropertyPathMarker Field { get; set; }

[JsonProperty(PropertyName = "query")]
ISpanQuery Query { get; set; }
}

public class SpanFieldMaskingQuery : PlainQuery, ISpanFieldMaskingQuery
{
{
protected override void WrapInContainer(IQueryContainer container)
{
container.SpanFieldMasking = this;
}

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

public string Name { get; set; }
public PropertyPathMarker Field { get; set; }
public ISpanQuery Query { get; set; }
public string Name { get; set; }
public PropertyPathMarker Field { get; set; }
public ISpanQuery Query { get; set; }

PropertyPathMarker IFieldNameQuery.GetFieldName()
{
return this.Field;
}
PropertyPathMarker IFieldNameQuery.GetFieldName()
{
return this.Field;
}

void IFieldNameQuery.SetFieldName(string fieldName)
{
this.Field = fieldName;
}
}
void IFieldNameQuery.SetFieldName(string fieldName)
{
this.Field = fieldName;
}
}

public class SpanFieldMaskingQueryDescriptor<T> : ISpanFieldMaskingQuery where T : class
{
ISpanFieldMaskingQuery Self { get { return this; } }
bool IQuery.IsConditionless { get { return false; } }
bool IQuery.IsConditionless { get { return false; } }

ISpanQuery ISpanFieldMaskingQuery.Query { get; set; }
PropertyPathMarker ISpanFieldMaskingQuery.Field { get; set; }
string IQuery.Name { get; set; }
ISpanQuery ISpanFieldMaskingQuery.Query { get; set; }
PropertyPathMarker ISpanFieldMaskingQuery.Field { get; set; }
string IQuery.Name { get; set; }
public SpanFieldMaskingQueryDescriptor<T> Name(string name)
{
Self.Name = name;
Expand All @@ -71,26 +71,26 @@ public SpanFieldMaskingQueryDescriptor<T> OnField(string field)
return this;
}

public SpanFieldMaskingQueryDescriptor<T> OnField(Expression<Func<T, object>> field)
{
Self.Field = field;
return this;
}
public SpanFieldMaskingQueryDescriptor<T> OnField(Expression<Func<T, object>> field)
{
Self.Field = field;
return this;
}

public SpanFieldMaskingQueryDescriptor<T> OnField<K>(Expression<Func<T, K>> field)
{
Self.Field = field;
return this;
}
public PropertyPathMarker GetFieldName()
{
return Self.Field;
}
public SpanFieldMaskingQueryDescriptor<T> OnField<K>(Expression<Func<T, K>> field)
{
Self.Field = field;
return this;
}
public PropertyPathMarker GetFieldName()
{
return Self.Field;
}

public void SetFieldName(string fieldName)
{
Self.Field = fieldName;
}
}
public void SetFieldName(string fieldName)
{
Self.Field = fieldName;
}
}
}
1 change: 1 addition & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@
<Compile Include="QueryParsers\Queries\SpanOrQueryTests.cs" />
<Compile Include="QueryParsers\Queries\SpanTermQueryTests.cs" />
<Compile Include="QueryParsers\Queries\TermQueryTests.cs" />
<Compile Include="QueryParsers\Queries\FieldMaskingSpanQueryTests.cs" />
<Compile Include="QueryParsers\Queries\TermsQueryTests.cs" />
<Compile Include="QueryParsers\Queries\WildCardQueryTests.cs" />
<Compile Include="QueryParsers\Visitor\DslPrettyPrintVisitor.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentAssertions;
using Nest.Tests.MockData.Domain;
using NUnit.Framework;

namespace Nest.Tests.Unit.QueryParsers.Queries
{
[TestFixture]
public class FieldMaskingSpanQueryTests : ParseQueryTestsBase
{
[Test]
public void FieldMaskingSpan_Deserializes()
{
var q = this.SerializeThenDeserialize(
f => f.SpanFieldMasking,
f => f.SpanFieldMasking(s => s.OnField(p => p.Name).Query(qq => qq.SpanTerm("x", "y")))
);


q.Field.Should().NotBeNull().And.Be("name");
q.Query.Should().NotBeNull();
q.Query.SpanTermQueryDescriptor.Should().NotBeNull();
}
}
}

0 comments on commit 409804c

Please sign in to comment.