Skip to content

Commit

Permalink
Merge pull request #1122 from elasticsearch/feature/span-not-query-up…
Browse files Browse the repository at this point in the history
…dates

fix #1099 add support for pre/post/dist to span not query
  • Loading branch information
gmarz committed Dec 10, 2014
2 parents 0673203 + 998018f commit c344285
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
75 changes: 53 additions & 22 deletions src/Nest/DSL/Query/SpanNotQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ public interface ISpanNotQuery : ISpanSubQuery
[JsonProperty(PropertyName = "exclude")]
ISpanQuery Exclude { get; set; }

[JsonProperty(PropertyName = "boost")]
double? Boost { get; set; }
[JsonProperty(PropertyName = "boost")]
double? Boost { get; set; }

[JsonProperty(PropertyName = "pre")]
int? Pre { get; set; }

[JsonProperty(PropertyName = "post")]
int? Post { get; set; }

[JsonProperty(PropertyName = "dist")]
int? Dist { get; set; }

}

Expand All @@ -31,22 +40,30 @@ protected override void WrapInContainer(IQueryContainer container)
bool IQuery.IsConditionless { get { return false; } }
public ISpanQuery Include { get; set; }
public ISpanQuery Exclude { get; set; }
public double? Boost { get; set; }
public double? Boost { get; set; }
public int? Pre { get; set; }
public int? Post { get; set; }
public int? Dist { get; set; }
}

public class SpanNotQuery<T> : ISpanNotQuery where T : class
{
private ISpanNotQuery Self { get { return this; } }

ISpanQuery ISpanNotQuery.Include { get; set; }

ISpanQuery ISpanNotQuery.Exclude { get; set; }
double? ISpanNotQuery.Boost { get; set; }
double? ISpanNotQuery.Boost { get; set; }
int? ISpanNotQuery.Pre { get; set; }
int? ISpanNotQuery.Post { get; set; }
int? ISpanNotQuery.Dist { get; set; }

bool IQuery.IsConditionless
bool IQuery.IsConditionless
{
get
{
var excludeQuery = ((ISpanNotQuery)this).Exclude as IQuery;
var includeQuery = ((ISpanNotQuery)this).Include as IQuery;
var excludeQuery = Self.Exclude as IQuery;
var includeQuery = Self.Include as IQuery;

return excludeQuery == null && includeQuery == null
|| (includeQuery == null && excludeQuery.IsConditionless)
Expand All @@ -59,29 +76,43 @@ bool IQuery.IsConditionless

public SpanNotQuery<T> Include(Func<SpanQuery<T>, SpanQuery<T>> selector)
{
if (selector == null)
return this;
var descriptors = new List<SpanQuery<T>>();
if (selector == null) return this;
var span = new SpanQuery<T>();
var q = selector(span);
((ISpanNotQuery)this).Include = q;
Self.Include = selector(span); ;
return this;
}

public SpanNotQuery<T> Exclude(Func<SpanQuery<T>, SpanQuery<T>> selector)
{
if (selector == null)
return this;
var descriptors = new List<SpanQuery<T>>();
if (selector == null) return this;
var span = new SpanQuery<T>();
var q = selector(span);
((ISpanNotQuery)this).Exclude = q;
Self.Exclude = selector(span);;
return this;
}

public SpanNotQuery<T> Boost(double boost)
{
Self.Boost = boost;
return this;
}

public SpanNotQuery<T> Pre(int pre)
{
Self.Pre = pre;
return this;
}

public SpanNotQuery<T> Post(int post)
{
Self.Post = post;
return this;
}

public SpanNotQuery<T> Dist(int dist)
{
Self.Dist = dist;
return this;
}

public ISpanNotQuery Boost(double boost)
{
((ISpanNotQuery)this).Boost = boost;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public void SpanNot_Deserializes()
var q = this.SerializeThenDeserialize(
f=>f.SpanNot,
f=>f.SpanNot(sn=>sn
.Pre(1)
.Post(2)
.Dist(3)
.Include(e =>e.SpanTerm(p => p.Name, "elasticsearch.pm", 1.1))
.Exclude(e=>e.SpanTerm(p => p.Name, "elasticsearch.pm", 1.1))
)
Expand Down
16 changes: 11 additions & 5 deletions src/Tests/Nest.Tests.Unit/Search/Query/Singles/SpanNotQueryJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public void SpanNotQuery()
.From(0)
.Size(10)
.Query(q => q
.SpanNot(sf=>sf
.Include(e =>e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
.Exclude(e=>e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
.Boost(2.2)
.SpanNot(sf => sf
.Pre(1)
.Post(2)
.Dist(3)
.Include(e => e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
.Exclude(e => e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
.Boost(2.2)
)
);
var json = TestElasticClient.Serialize(s);
Expand All @@ -39,7 +42,10 @@ public void SpanNotQuery()
}
}
},
boost: 2.2
boost: 2.2,
pre: 1,
post: 2,
dist: 3
}}}";
Assert.True(json.JsonEquals(expected), json);
}
Expand Down

0 comments on commit c344285

Please sign in to comment.