Skip to content
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

Implement Intervals query #3711

Merged
merged 1 commit into from
May 2, 2019
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/QueryDsl/Abstractions/Container/IQueryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public interface IQueryContainer
[DataMember(Name ="ids")]
IIdsQuery Ids { get; set; }

[DataMember(Name = "intervals")]
IIntervalsQuery Intervals { get; set; }

[IgnoreDataMember]
bool IsConditionless { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class QueryContainer : IQueryContainer, IDescriptor
private IHasChildQuery _hasChild;
private IHasParentQuery _hasParent;
private IIdsQuery _ids;
private IIntervalsQuery _intervals;
private IMatchQuery _match;
private IMatchAllQuery _matchAllQuery;
private IMatchNoneQuery _matchNoneQuery;
Expand Down Expand Up @@ -148,6 +149,12 @@ IIdsQuery IQueryContainer.Ids
set => _ids = Set(value);
}

IIntervalsQuery IQueryContainer.Intervals
{
get => _intervals;
set => _intervals = Set(value);
}

IMatchQuery IQueryContainer.Match
{
get => _match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ public QueryContainer Prefix(Func<PrefixQueryDescriptor<T>, IPrefixQuery> select
public QueryContainer Ids(Func<IdsQueryDescriptor, IIdsQuery> selector) =>
WrapInContainer(selector, (query, container) => container.Ids = query);

/// <summary>
/// Allows fine-grained control over the order and proximity of matching terms.
/// Matching rules are constructed from a small set of definitions,
/// and the rules are then applied to terms from a particular field.
/// The definitions produce sequences of minimal intervals that span terms in a body of text.
/// These intervals can be further combined and filtered by parent sources.
/// </summary>
public QueryContainer Intervals(Func<IntervalsQueryDescriptor<T>, IIntervalsQuery> selector) =>
WrapInContainer(selector, (query, container) => container.Intervals = query);

/// <summary>
/// Matches spans containing a term. The span term query maps to Lucene SpanTermQuery.
/// </summary>
Expand Down
67 changes: 67 additions & 0 deletions src/Nest/QueryDsl/FullText/Intervals/IntervalsAllOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Nest
{
/// <summary>
/// A rule that returns matches that span a combination of other rules.
/// </summary>
[ReadAs(typeof(IntervalsAllOf))]
public interface IIntervalsAllOf : IIntervals
{
/// <summary>
/// An array of rules to combine. All rules must produce a match in a document for the overall source to match.
/// </summary>
[DataMember(Name = "intervals")]
IEnumerable<IntervalsContainer> Intervals { get; set; }

/// <summary>
/// Specify a maximum number of gaps between the terms in the text. Terms that appear further apart than this will not
/// match.
/// If unspecified, or set to -1, then there is no width restriction on the match.
/// If set to 0 then the terms must appear next to each other.
/// </summary>
[DataMember(Name = "max_gaps")]
int? MaxGaps { get; set; }

/// <summary>
/// Whether or not the terms must appear in their specified order. Defaults to <c>false</c>
/// </summary>
[DataMember(Name = "ordered")]
bool? Ordered { get; set; }
}

/// <inheritdoc cref="IIntervalsAllOf" />
public class IntervalsAllOf : IntervalsBase, IIntervalsAllOf
{
/// <inheritdoc />
public IEnumerable<IntervalsContainer> Intervals { get; set; }

/// <inheritdoc />
public int? MaxGaps { get; set; }

/// <inheritdoc />
public bool? Ordered { get; set; }

internal override void WrapInContainer(IIntervalsContainer container) => container.AllOf = this;
}

/// <inheritdoc cref="IIntervalsAllOf" />
public class IntervalsAllOfDescriptor : IntervalsDescriptorBase<IntervalsAllOfDescriptor, IIntervalsAllOf>, IIntervalsAllOf
{
IEnumerable<IntervalsContainer> IIntervalsAllOf.Intervals { get; set; }
int? IIntervalsAllOf.MaxGaps { get; set; }
bool? IIntervalsAllOf.Ordered { get; set; }

/// <inheritdoc cref="IIntervalsAllOf.MaxGaps" />
public IntervalsAllOfDescriptor MaxGaps(int? maxGaps) => Assign(maxGaps, (a, v) => a.MaxGaps = v);

/// <inheritdoc cref="IIntervalsAllOf.Ordered" />
public IntervalsAllOfDescriptor Ordered(bool? ordered = true) => Assign(ordered, (a, v) => a.Ordered = v);

/// <inheritdoc cref="IIntervalsAllOf.Intervals" />
public IntervalsAllOfDescriptor Intervals(Func<IntervalsListDescriptor, IPromise<List<IntervalsContainer>>> selector) =>
Assign(selector, (a, v) => a.Intervals = v.InvokeOrDefault(new IntervalsListDescriptor())?.Value);
}
}
38 changes: 38 additions & 0 deletions src/Nest/QueryDsl/FullText/Intervals/IntervalsAnyOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Nest
{
/// <summary>
/// A rule that emits intervals produced by any of its sub-rules.
/// </summary>
[ReadAs(typeof(IntervalsAnyOf))]
public interface IIntervalsAnyOf : IIntervals
{
/// <summary>
/// An array of rules to match.
/// </summary>
[DataMember(Name = "intervals")]
IEnumerable<IntervalsContainer> Intervals { get; set; }
}

/// <inheritdoc cref="IIntervalsAnyOf" />
public class IntervalsAnyOf : IntervalsBase, IIntervalsAnyOf
{
/// <inheritdoc />
public IEnumerable<IntervalsContainer> Intervals { get; set; }

internal override void WrapInContainer(IIntervalsContainer container) => container.AnyOf = this;
}

/// <inheritdoc cref="IIntervalsAnyOf" />
public class IntervalsAnyOfDescriptor : IntervalsDescriptorBase<IntervalsAnyOfDescriptor, IIntervalsAnyOf>, IIntervalsAnyOf
{
IEnumerable<IntervalsContainer> IIntervalsAnyOf.Intervals { get; set; }

/// <inheritdoc cref="IIntervalsAnyOf.Intervals" />
public IntervalsAnyOfDescriptor Intervals(Func<IntervalsListDescriptor, IPromise<List<IntervalsContainer>>> selector) =>
Assign(selector, (a, v) => a.Intervals = v.InvokeOrDefault(new IntervalsListDescriptor())?.Value);
}
}
149 changes: 149 additions & 0 deletions src/Nest/QueryDsl/FullText/Intervals/IntervalsFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System;
using System.Runtime.Serialization;

namespace Nest
{
/// <summary>
/// Filters intervals produced by any rules by their relation to the intervals produced by another rule
/// </summary>
[ReadAs(typeof(IntervalsFilter))]
public interface IIntervalsFilter
{
/// <summary>
/// Produces intervals that appear after an interval from the filter role
/// </summary>
[DataMember(Name = "after")]
IntervalsContainer After { get; set; }

/// <summary>
/// Produces intervals that appear before an interval from the filter role
/// </summary>
[DataMember(Name = "before")]
IntervalsContainer Before { get; set; }

/// <summary>
/// Produces intervals that are contained by an interval from the filter rule
/// </summary>
[DataMember(Name = "contained_by")]
IntervalsContainer ContainedBy { get; set; }

/// <summary>
/// Produces intervals that contain an interval from the filter rule
/// </summary>
[DataMember(Name = "containing")]
IntervalsContainer Containing { get; set; }

/// <summary>
/// Produces intervals that are not contained by an interval from the filter rule
/// </summary>
[DataMember(Name = "not_contained_by")]
IntervalsContainer NotContainedBy { get; set; }

/// <summary>
/// Produces intervals that do not contain an interval from the filter rule
/// </summary>
[DataMember(Name = "not_containing")]
IntervalsContainer NotContaining { get; set; }

/// <summary>
/// Produces intervals that do not overlap with an interval from the filter rule
/// </summary>
[DataMember(Name = "not_overlapping")]
IntervalsContainer NotOverlapping { get; set; }

/// <summary>
/// Produces intervals that overlap with an interval from the filter rule
/// </summary>
[DataMember(Name = "overlapping")]
IntervalsContainer Overlapping { get; set; }

/// <summary>
/// filter intervals based on their start position, end position and internal gap count, using a script.
/// The script has access to an <code>interval</code> variable, with <code>start</code>,
/// <code>end</code> and <code>gaps</code> properties
/// </summary>
[DataMember(Name = "script")]
IScript Script { get; set; }
}

/// <inheritdoc />
public class IntervalsFilter : IIntervalsFilter
{
/// <inheritdoc />
public IntervalsContainer After { get; set; }

/// <inheritdoc />
public IntervalsContainer Before { get; set; }

/// <inheritdoc />
public IntervalsContainer ContainedBy { get; set; }

/// <inheritdoc />
public IntervalsContainer Containing { get; set; }

/// <inheritdoc />
public IntervalsContainer NotContainedBy { get; set; }

/// <inheritdoc />
public IntervalsContainer NotContaining { get; set; }

/// <inheritdoc />
public IntervalsContainer NotOverlapping { get; set; }

/// <inheritdoc />
public IntervalsContainer Overlapping { get; set; }

/// <inheritdoc />
public IScript Script { get; set; }
}

/// <inheritdoc cref="IIntervalsFilter" />
public class IntervalsFilterDescriptor : DescriptorBase<IntervalsFilterDescriptor, IIntervalsFilter>, IIntervalsFilter
{
IntervalsContainer IIntervalsFilter.After { get; set; }
IntervalsContainer IIntervalsFilter.Before { get; set; }
IntervalsContainer IIntervalsFilter.ContainedBy { get; set; }
IntervalsContainer IIntervalsFilter.Containing { get; set; }
IntervalsContainer IIntervalsFilter.NotContainedBy { get; set; }
IntervalsContainer IIntervalsFilter.NotContaining { get; set; }
IntervalsContainer IIntervalsFilter.NotOverlapping { get; set; }
IntervalsContainer IIntervalsFilter.Overlapping { get; set; }
IScript IIntervalsFilter.Script { get; set; }

/// <inheritdoc cref="IIntervalsFilter.Containing" />
public IntervalsFilterDescriptor Containing(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.Containing = v);

/// <inheritdoc cref="IIntervalsFilter.ContainedBy" />
public IntervalsFilterDescriptor ContainedBy(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.ContainedBy = v);

/// <inheritdoc cref="IIntervalsFilter.NotContaining" />
public IntervalsFilterDescriptor NotContaining(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.NotContaining = v);

/// <inheritdoc cref="IIntervalsFilter.NotContainedBy" />
public IntervalsFilterDescriptor NotContainedBy(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.NotContainedBy = v);

/// <inheritdoc cref="IIntervalsFilter.Overlapping" />
public IntervalsFilterDescriptor Overlapping(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.Overlapping = v);

/// <inheritdoc cref="IIntervalsFilter.NotOverlapping" />
public IntervalsFilterDescriptor NotOverlapping(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.NotOverlapping = v);

/// <inheritdoc cref="IIntervalsFilter.Before" />
public IntervalsFilterDescriptor Before(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.Before = v);

/// <inheritdoc cref="IIntervalsFilter.After" />
public IntervalsFilterDescriptor After(Func<IntervalsDescriptor, IntervalsContainer> selector) =>
Assign(selector?.Invoke(new IntervalsDescriptor()), (a, v) => a.After = v);

/// <inheritdoc cref="IIntervalsFilter.Script" />
public IntervalsFilterDescriptor Script(Func<ScriptDescriptor, IScript> selector) =>
Assign(selector?.Invoke(new ScriptDescriptor()), (a, v) => a.Script = v);
}
}
Loading