Skip to content

Commit

Permalink
Add field_masking_span query
Browse files Browse the repository at this point in the history
Forward port of #2231
  • Loading branch information
russcam committed Sep 6, 2016
1 parent b9fded9 commit d2f9905
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 24 deletions.
2 changes: 2 additions & 0 deletions docs/query-dsl-usage.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ include::query-dsl/nest-specific/raw/raw-query-usage.asciidoc[]

include::query-dsl/span/container/span-containing-query-usage.asciidoc[]

include::query-dsl/span/field-masking/span-field-masking-usage.asciidoc[]

include::query-dsl/span/first/span-first-query-usage.asciidoc[]

include::query-dsl/span/multi-term/span-multi-term-query-usage.asciidoc[]
Expand Down
2 changes: 2 additions & 0 deletions docs/query-dsl.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ NEST exposes all of the query DSL endpoints available in Elasticsearch

* <<span-containing-query-usage,Span Containing Query Usage>>

* <<span-field-masking-usage,Span Field Masking Usage>>

* <<span-first-query-usage,Span First Query Usage>>

* <<span-multi-term-query-usage,Span Multi Term Query Usage>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/2.3

:github: https://github.com/elastic/elasticsearch-net

:nuget: https://www.nuget.org/packages

////
IMPORTANT NOTE
==============
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/2.x/src/Tests/QueryDsl/Span/FieldMasking/SpanFieldMaskingUsageTests.cs.
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
please modify the original csharp file found at the link and submit the PR with that change. Thanks!
////

[[span-field-masking-usage]]
== Span Field Masking Usage

=== Fluent DSL Example

[source,csharp]
----
q
.SpanFieldMasking(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Name)
.Query(sq=>sq
.SpanTerm(st=>st.Field(p=>p.Description).Value("dolorem"))
)
)
----

=== Object Initializer Syntax Example

[source,csharp]
----
new SpanFieldMaskingQuery
{
Name = "named_query",
Boost = 1.1,
Field = Infer.Field<Project>(p => p.Name),
Query = new SpanQuery
{
SpanTerm = new SpanTermQuery
{
Field = Infer.Field<Project>(p => p.Description),
Value = "dolorem"
}
}
}
----

[source,javascript]
.Example json output
----
{
"field_masking_span": {
"_name": "named_query",
"boost": 1.1,
"field": "name",
"query": {
"span_term": {
"description": {
"value": "dolorem"
}
}
}
}
}
----

1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@
<Compile Include="QueryDsl\Operator.cs" />
<Compile Include="QueryDsl\Query.cs" />
<Compile Include="QueryDsl\Span\Containing\SpanContainingQuery.cs" />
<Compile Include="QueryDsl\Span\FieldMasking\SpanFieldMaskingQuery.cs" />
<Compile Include="QueryDsl\Span\First\SpanFirstQuery.cs" />
<Compile Include="QueryDsl\Span\ISpanSubQuery.cs" />
<Compile Include="QueryDsl\Span\MultiTerm\SpanMultiTermQuery.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/QueryDsl/Abstractions/Container/IQueryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public interface IQueryContainer
[JsonProperty("span_multi")]
ISpanMultiTermQuery SpanMultiTerm { get; set; }

[JsonProperty("field_masking_span")]
ISpanFieldMaskingQuery SpanFieldMasking { get; set; }

[JsonProperty("nested")]
INestedQuery Nested { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class QueryContainer : IQueryContainer, IDescriptor
private ISpanContainingQuery _spanContaining;
private ISpanWithinQuery _spanWithin;
private ISpanMultiTermQuery _spanMultiTerm;
private ISpanFieldMaskingQuery _spanFieldMasking;
private INestedQuery _nested;
private IIndicesQuery _indices;
private IFunctionScoreQuery _functionScore;
Expand Down Expand Up @@ -109,6 +110,7 @@ private T Set<T>(T value) where T : IQuery
ISpanContainingQuery IQueryContainer.SpanContaining { get { return _spanContaining; } set { _spanContaining = Set(value); } }
ISpanWithinQuery IQueryContainer.SpanWithin { get { return _spanWithin; } set { _spanWithin = Set(value); } }
ISpanMultiTermQuery IQueryContainer.SpanMultiTerm { get { return _spanMultiTerm; } set { _spanMultiTerm = Set(value); } }
ISpanFieldMaskingQuery IQueryContainer.SpanFieldMasking { get { return _spanFieldMasking; } set { _spanFieldMasking = Set(value); } }
INestedQuery IQueryContainer.Nested { get { return _nested; } set { _nested = Set(value); } }
IIndicesQuery IQueryContainer.Indices { get { return _indices; } set { _indices = Set(value); } }
IFunctionScoreQuery IQueryContainer.FunctionScore { get { return _functionScore; } set { _functionScore = Set(value); } }
Expand All @@ -122,8 +124,5 @@ private T Set<T>(T value) where T : IQuery
IExistsQuery IQueryContainer.Exists { get { return _exists; } set { _exists = Set(value); } }
IMissingQuery IQueryContainer.Missing { get { return _missing; } set { _missing = Set(value); } }
ITypeQuery IQueryContainer.Type { get { return _type; } set { _type = Set(value); } }


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ public QueryContainer SpanContaining(Func<SpanContainingQueryDescriptor<T>, ISpa
public QueryContainer SpanWithin(Func<SpanWithinQueryDescriptor<T>, ISpanWithinQuery> selector) =>
WrapInContainer(selector, (query, container) => container.SpanWithin = query);

/// <summary>
/// Wraps span queries to allow them to participate in composite single-field Span queries by 'lying' about their search field.
/// That is, the masked span query will function as normal, but the field points back to the set field of the query.
/// This can be used to support queries like SpanNearQuery or SpanOrQuery across different fields,
/// which is not ordinarily permitted.
/// </summary>
public QueryContainer SpanFieldMasking(Func<SpanFieldMaskingQueryDescriptor<T>, ISpanFieldMaskingQuery> selector) =>
WrapInContainer(selector, (query, container) => container.SpanFieldMasking = query);

/// <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
8 changes: 4 additions & 4 deletions src/Nest/QueryDsl/Query.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Nest
{
// TODO: Write a unit test for these using reflection to make sure all queries are covered
public static class Query<T> where T : class
{
public static QueryContainer Bool(Func<BoolQueryDescriptor<T>, IBoolQuery> selector) =>
Expand Down Expand Up @@ -161,8 +158,11 @@ public static QueryContainer SpanTerm(Func<SpanTermQueryDescriptor<T>, ISpanTerm
public static QueryContainer SpanWithin(Func<SpanWithinQueryDescriptor<T>, ISpanWithinQuery> selector) =>
new QueryContainerDescriptor<T>().SpanWithin(selector);

public static QueryContainer SpanFieldMasking(Func<SpanFieldMaskingQueryDescriptor<T>, ISpanFieldMaskingQuery> selector) =>
new QueryContainerDescriptor<T>().SpanFieldMasking(selector);

#pragma warning disable 618
[Obsolete("Scheduled to be removed in 5.0. Setting Strict() at the container level does is a noop and must be set on each individual query.")]
[Obsolete("Scheduled to be removed in 5.0. Setting Strict() at the container level is a noop and must be set on each individual query.")]
public static QueryContainerDescriptor<T> Strict(bool strict = true) =>
new QueryContainerDescriptor<T>().Strict(strict);
#pragma warning restore 618
Expand Down
49 changes: 49 additions & 0 deletions src/Nest/QueryDsl/Span/FieldMasking/SpanFieldMaskingQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeJsonConverter<SpanFieldMaskingQueryDescriptor<object>>))]
public interface ISpanFieldMaskingQuery : ISpanSubQuery
{
[JsonProperty("field")]
Field Field { get; set; }

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

public class SpanFieldMaskingQuery : QueryBase, ISpanFieldMaskingQuery
{
protected override bool Conditionless => IsConditionless(this);
public ISpanQuery Query { get; set; }
public Field Field { get; set; }

internal override void InternalWrapInContainer(IQueryContainer c) => c.SpanFieldMasking = this;
internal static bool IsConditionless(ISpanFieldMaskingQuery q) =>
q.Field.IsConditionless() || q.Query == null || q.Query.Conditionless;
}

public class SpanFieldMaskingQueryDescriptor<T>
: QueryDescriptorBase<SpanFieldMaskingQueryDescriptor<T>, ISpanFieldMaskingQuery>
, ISpanFieldMaskingQuery where T : class
{
protected override bool Conditionless => SpanFieldMaskingQuery.IsConditionless(this);
ISpanQuery ISpanFieldMaskingQuery.Query { get; set; }
Field ISpanFieldMaskingQuery.Field { get; set; }

public SpanFieldMaskingQueryDescriptor<T> Field(Field field) => Assign(a => a.Field = field);

public SpanFieldMaskingQueryDescriptor<T> Field(Expression<Func<T, object>> objectPath) =>
Assign(a => a.Field = objectPath);

public SpanFieldMaskingQueryDescriptor<T> Query(Func<SpanQueryDescriptor<T>, ISpanQuery> selector) =>
Assign(a => a.Query = selector?.Invoke(new SpanQueryDescriptor<T>()));
}
}
28 changes: 19 additions & 9 deletions src/Nest/QueryDsl/Span/SpanQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@ namespace Nest
[JsonConverter(typeof(ReadAsTypeJsonConverter<SpanQueryDescriptor<object>>))]
public interface ISpanQuery : IQuery
{
[JsonProperty(PropertyName = "span_term")]
[JsonProperty("span_term")]
ISpanTermQuery SpanTerm { get; set; }

[JsonProperty(PropertyName = "span_first")]
[JsonProperty("span_first")]
ISpanFirstQuery SpanFirst { get; set; }

[JsonProperty(PropertyName = "span_near")]
[JsonProperty("span_near")]
ISpanNearQuery SpanNear { get; set; }

[JsonProperty(PropertyName = "span_or")]
[JsonProperty("span_or")]
ISpanOrQuery SpanOr { get; set; }

[JsonProperty(PropertyName = "span_not")]
[JsonProperty("span_not")]
ISpanNotQuery SpanNot { get; set; }

[JsonProperty(PropertyName = "span_containing")]
[JsonProperty("span_containing")]
ISpanContainingQuery SpanContaining { get; set; }

[JsonProperty(PropertyName = "span_within")]
[JsonProperty("span_within")]
ISpanWithinQuery SpanWithin { get; set; }

[JsonProperty(PropertyName = "span_multi")]
[JsonProperty("span_multi")]
ISpanMultiTermQuery SpanMultiTerm { get; set; }

[JsonProperty("field_masking_span")]
ISpanFieldMaskingQuery SpanFieldMasking { get; set; }

void Accept(IQueryVisitor visitor);
}

Expand All @@ -52,6 +55,8 @@ public class SpanQuery : ISpanQuery
public ISpanMultiTermQuery SpanMultiTerm { get; set; }
public ISpanContainingQuery SpanContaining{ get; set; }
public ISpanWithinQuery SpanWithin { get; set; }
public ISpanFieldMaskingQuery SpanFieldMasking { get; set; }

public void Accept(IQueryVisitor visitor) => new QueryWalker().Walk(this, visitor);

internal static bool IsConditionless(ISpanQuery q) => new[]
Expand All @@ -61,7 +66,8 @@ internal static bool IsConditionless(ISpanQuery q) => new[]
q.SpanNear,
q.SpanOr ,
q.SpanNot,
q.SpanMultiTerm
q.SpanMultiTerm,
q.SpanFieldMasking
}.All(sq => sq == null || sq.Conditionless);
}

Expand All @@ -77,6 +83,7 @@ public class SpanQueryDescriptor<T> : QueryDescriptorBase<SpanQueryDescriptor<T>
ISpanMultiTermQuery ISpanQuery.SpanMultiTerm { get; set; }
ISpanContainingQuery ISpanQuery.SpanContaining{ get; set; }
ISpanWithinQuery ISpanQuery.SpanWithin { get; set; }
ISpanFieldMaskingQuery ISpanQuery.SpanFieldMasking { get; set; }

public SpanQueryDescriptor<T> SpanTerm(Func<SpanTermQueryDescriptor<T>, ISpanTermQuery> selector) =>
Assign(a => a.SpanTerm = selector?.Invoke(new SpanTermQueryDescriptor<T>()));
Expand All @@ -102,6 +109,9 @@ public SpanQueryDescriptor<T> SpanContaining(Func<SpanContainingQueryDescriptor<
public SpanQueryDescriptor<T> SpanWithin(Func<SpanWithinQueryDescriptor<T>, ISpanWithinQuery> selector) =>
Assign(a => a.SpanWithin = selector?.Invoke(new SpanWithinQueryDescriptor<T>()));

public SpanQueryDescriptor<T> SpanFieldMasking(Func<SpanFieldMaskingQueryDescriptor<T>, ISpanFieldMaskingQuery> selector) =>
Assign(a => a.SpanFieldMasking = selector?.Invoke(new SpanFieldMaskingQueryDescriptor<T>()));

void ISpanQuery.Accept(IQueryVisitor visitor) => new QueryWalker().Walk(this, visitor);

}
Expand Down
2 changes: 2 additions & 0 deletions src/Nest/QueryDsl/Visitor/DslPrettyPrintVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ private void Write(string queryType, Field field = null)

public virtual void Visit(ISpanTermQuery query) => Write("span_term");

public virtual void Visit(ISpanFieldMaskingQuery query) => Write("field_masking_span");

public virtual void Visit(ITermQuery query) => Write("term", query.Field);

public virtual void Visit(IWildcardQuery query) => Write("wildcard");
Expand Down
11 changes: 7 additions & 4 deletions src/Nest/QueryDsl/Visitor/QueryVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public interface IQueryVisitor
void Visit(ISpanContainingQuery query);
void Visit(ISpanWithinQuery query);
void Visit(ISpanMultiTermQuery query);
void Visit(ISpanFieldMaskingQuery query);

void Visit(IGeoIndexedShapeQuery query);
void Visit(IGeoShapeQuery query);
Expand Down Expand Up @@ -193,6 +194,12 @@ public virtual void Visit(ISpanOrQuery query) { }

public virtual void Visit(ISpanTermQuery query) { }

public virtual void Visit(ISpanSubQuery query) { }

public virtual void Visit(ISpanMultiTermQuery query) { }

public virtual void Visit(ISpanFieldMaskingQuery query) { }

public virtual void Visit(ITermQuery query) { }

public virtual void Visit(IWildcardQuery query) { }
Expand All @@ -215,8 +222,6 @@ public virtual void Visit(IGeoHashCellQuery query) { }

public virtual void Visit(ITemplateQuery query) { }

public virtual void Visit(ISpanMultiTermQuery query) { }

public virtual void Visit(IGeoShapeMultiPointQuery query) { }

public virtual void Visit(IGeoShapeMultiPolygonQuery query) { }
Expand All @@ -231,8 +236,6 @@ public virtual void Visit(IGeoShapeLineStringQuery query) { }

public virtual void Visit(IGeoShapeEnvelopeQuery query) { }

public virtual void Visit(ISpanSubQuery query) { }

public virtual void Visit(IGeoShapeCircleQuery query) { }

public virtual void Visit(ISpanQuery query) { }
Expand Down
13 changes: 9 additions & 4 deletions src/Nest/QueryDsl/Visitor/QueryWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public void Walk(IQueryContainer qd, IQueryVisitor visitor)
VisitQuery(qd.MoreLikeThis, visitor, (v, d) => v.Visit(d));
VisitQuery(qd.MultiMatch, visitor, (v, d) => v.Visit(d));
VisitQuery(qd.CommonTerms, visitor, (v, d) => v.Visit(d));
VisitQuery(qd.Fuzzy, visitor, (v, d) =>
VisitQuery(qd.Fuzzy, visitor, (v, d) =>
{
v.Visit(d);
VisitQuery(d as IFuzzyStringQuery, visitor, (vv, dd) => v.Visit(dd));
VisitQuery(d as IFuzzyNumericQuery, visitor, (vv, dd) => v.Visit(dd));
VisitQuery(d as IFuzzyDateQuery, visitor, (vv, dd) => v.Visit(dd));
});
VisitQuery(qd.Range, visitor, (v, d) =>
VisitQuery(qd.Range, visitor, (v, d) =>
{
v.Visit(d);
VisitQuery(d as IDateRangeQuery, visitor, (vv, dd) => v.Visit(dd));
VisitQuery(d as INumericRangeQuery, visitor, (vv, dd) => v.Visit(dd));
VisitQuery(d as ITermRangeQuery, visitor, (vv, dd) => v.Visit(dd));
});
VisitQuery(qd.GeoShape, visitor, (v, d) =>
VisitQuery(qd.GeoShape, visitor, (v, d) =>
{
v.Visit(d);
VisitQuery(d as IGeoIndexedShapeQuery, visitor, (vv, dd) => v.Visit(dd));
Expand Down Expand Up @@ -250,6 +250,11 @@ private static void VisitSpan<T>(T qd, IQueryVisitor visitor) where T : class, I
Accept(visitor, d.Big);
Accept(visitor, d.Little);
});
VisitSpanSubQuery(qd.SpanFieldMasking, visitor, (v, d) =>
{
v.Visit(d);
Accept(visitor, d.Query);
});
}

private static void VisitQuery<T>(T qd, IQueryVisitor visitor, Action<IQueryVisitor, T> scoped)
Expand All @@ -269,7 +274,7 @@ private static void VisitSpanSubQuery<T>(T qd, IQueryVisitor visitor, Action<IQu
if (qd == null) return;
VisitQuery(qd, visitor, (v, d) =>
{
visitor.Visit(qd as ISpanSubQuery);
visitor.Visit(qd);
scoped(v, d);
});
}
Expand Down
Loading

0 comments on commit d2f9905

Please sign in to comment.