Skip to content

Add support for span multi term query. #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Nest/DSL/Query/IQueryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public interface IQueryContainer : ICustomJson
[JsonProperty(PropertyName = "span_not")]
ISpanNotQuery SpanNot { get; set; }

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

[JsonProperty(PropertyName = "top_children")]
ITopChildrenQuery TopChildren { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/Nest/DSL/Query/QueryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class QueryContainer : IQueryContainer

ISpanNearQuery IQueryContainer.SpanNear { get; set; }

ISpanMultiTermQuery IQueryContainer.SpanMultiTerm { get; set; }

ITopChildrenQuery IQueryContainer.TopChildren { get; set; }

INestedQuery IQueryContainer.Nested { get; set; }
Expand Down
13 changes: 13 additions & 0 deletions src/Nest/DSL/Query/QueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,19 @@ public QueryContainer SpanNot(Action<SpanNotQuery<T>> selector)
return this.New(span, q => q.SpanNot = span);
}

/// <summary>
/// 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 SpanMultiTerm(Action<SpanMultiTermQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var span = new SpanMultiTermQueryDescriptor<T>();
selector(span);

return this.New(span, q => q.SpanMultiTerm = 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
42 changes: 42 additions & 0 deletions src/Nest/DSL/Query/SpanMultiTermQueryDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface ISpanMultiTermQuery : ISpanSubQuery
{
[JsonProperty("match")]
IQueryContainer Match { get; set; }
}

public class SpanMultiTermQuery : PlainQuery, ISpanMultiTermQuery
{
protected override void WrapInContainer(IQueryContainer container)
{
container.SpanMultiTerm = this;
}

public IQueryContainer Match { get; set; }

public bool IsConditionless { get { return false; } }
}

public class SpanMultiTermQueryDescriptor<T> : ISpanMultiTermQuery
where T : class
{
IQueryContainer ISpanMultiTermQuery.Match { get; set; }

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

public SpanMultiTermQueryDescriptor<T> Match(Func<QueryDescriptor<T>, QueryContainer> querySelector)
{
var q = new QueryDescriptor<T>();
((ISpanMultiTermQuery)this).Match = querySelector(q);
return this;
}
}
}
15 changes: 14 additions & 1 deletion src/Nest/DSL/Query/SpanQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public interface ISpanQuery : IQuery

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

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

public class SpanQuery : ISpanQuery
Expand All @@ -35,6 +38,7 @@ public class SpanQuery : ISpanQuery
public ISpanNearQuery SpanNear { get; set; }
public ISpanOrQuery SpanOr { get; set; }
public ISpanNotQuery SpanNot { get; set; }
public ISpanMultiTermQuery SpanMultiTerm { get; set; }
}

public class SpanQuery<T> : ISpanQuery where T : class
Expand All @@ -49,6 +53,8 @@ public class SpanQuery<T> : ISpanQuery where T : class

ISpanNotQuery ISpanQuery.SpanNot { get; set; }

ISpanMultiTermQuery ISpanQuery.SpanMultiTerm { get; set; }

bool IQuery.IsConditionless
{
get
Expand All @@ -59,7 +65,8 @@ bool IQuery.IsConditionless
((ISpanQuery)this).SpanFirst as IQuery,
((ISpanQuery)this).SpanNear as IQuery,
((ISpanQuery)this).SpanOr as IQuery,
((ISpanQuery)this).SpanNot as IQuery
((ISpanQuery)this).SpanNot as IQuery,
((ISpanQuery)this).SpanMultiTerm as IQuery
};
return queries.All(q => q == null || q.IsConditionless);
}
Expand Down Expand Up @@ -117,6 +124,12 @@ public SpanQuery<T> SpanNot(Func<SpanNotQuery<T>, SpanNotQuery<T>> selector)
var q = selector(new SpanNotQuery<T>());
return CreateQuery(q, (sq) => ((ISpanQuery)sq).SpanNot = q);
}
public SpanQuery<T> SpanMultiTerm(Func<SpanMultiTermQueryDescriptor<T>, SpanMultiTermQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var q= selector(new SpanMultiTermQueryDescriptor<T>());
return CreateQuery(q, (sq) => ((ISpanQuery)sq).SpanMultiTerm = q);
}

private SpanQuery<T> CreateQuery<K>(K query, Action<SpanQuery<T>> setProperty) where K : ISpanSubQuery
{
Expand Down
5 changes: 5 additions & 0 deletions src/Nest/Domain/DSL/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public static QueryContainer SpanTerm(string field, string value, double? Boost
return new QueryDescriptor<T>().SpanTerm(field, value, Boost);
}

public static QueryContainer SpanMultiTerm(Action<SpanMultiTermQueryDescriptor<T>> selector)
{
return new QueryDescriptor<T>().SpanMultiTerm(selector);
}

public static QueryContainer Term<K>(Expression<Func<T, K>> fieldDescriptor, K value, double? Boost = null)
{
return new QueryDescriptor<T>().Term(fieldDescriptor, value, Boost);
Expand Down
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="DSL\MultiPercolateDescriptor.cs" />
<Compile Include="DSL\Paths\IndexOptionalNamePathDescriptor.cs" />
<Compile Include="DSL\Paths\IndicesOptionalTypesNamePathDecriptor.cs" />
<Compile Include="DSL\Query\SpanMultiTermQueryDescriptor.cs" />
<Compile Include="DSL\TypeExistsDescriptor.cs" />
<Compile Include="DSL\Query\Functions\FieldValueFactorDescriptor.cs" />
<Compile Include="DSL\Query\GeoShapeMultiPointQueryDescriptor.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<Compile Include="Search\Filter\PrefixFilterTests.cs" />
<Compile Include="Search\Filter\RangeFilterTests.cs" />
<Compile Include="Search\Query\BoolQueryResults.cs" />
<Compile Include="Search\Query\SpanQueryTests.cs" />
<Compile Include="Search\Query\TermToString.cs" />
<Compile Include="Core\UpdateTests.cs" />
<Compile Include="Search\Filter\BoolFilterTests.cs" />
Expand Down
34 changes: 34 additions & 0 deletions src/Tests/Nest.Tests.Integration/Search/Query/SpanQueryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using FluentAssertions;
using Nest.Tests.MockData.Domain;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nest.Tests.Integration.Search.Query
{
[TestFixture]
public class SpanQueryTests : IntegrationTests
{
[Test]
public void SpanMultiTermQuery()
{
var r = Client.Search<ElasticsearchProject>(s => s
.Query(q => q
.SpanMultiTerm(sp => sp
.Match(m => m
.Prefix(p => p
.OnField(ep => ep.Name)
.Value("NEST")
)
)
)
)
);

r.IsValid.Should().BeTrue();
}
}
}
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 @@ -398,6 +398,7 @@
<Compile Include="Search\Query\Singles\HasParentQueryJson.cs" />
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
<Compile Include="Search\Query\Modes\QueryModesTests.cs" />
<Compile Include="Search\Query\Singles\SpanMultiTermQueryJson.cs" />
<Compile Include="Search\Query\Singles\Term\TermToStringJson.cs" />
<Compile Include="Search\Rescoring\RescoreTests.cs" />
<Compile Include="Search\Facets\DateHistogramFacetJson.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Nest.Tests.MockData.Domain;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nest.Tests.Unit.Search.Query.Singles
{
[TestFixture]
public class SpanMultiTermQueryJson : BaseJsonTests
{
[Test]
public void SpanMultiTermQuery()
{
var s = new SearchDescriptor<ElasticsearchProject>()
.From(0)
.Size(10)
.Query(q => q
.SpanMultiTerm(sp => sp
.Match(m => m
.Prefix(p => p
.OnField(ep => ep.Name)
.Value("NEST")
)
)
)
);
var json = TestElasticClient.Serialize(s);
var expected = @"{ from: 0, size: 10, query :
{
span_multi:{
match:{
prefix : { name : { value : ""NEST"" } }
}
}
}}";
Assert.True(json.JsonEquals(expected), json);
}
}
}