Skip to content
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
2 changes: 2 additions & 0 deletions src/Nest/DSL/Filter/FilterContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public bool IsConditionless

ITypeFilter IFilterContainer.Type { get; set; }

IIndicesFilter IFilterContainer.Indices { get; set; }

IMatchAllFilter IFilterContainer.MatchAll { get; set; }

IHasChildFilter IFilterContainer.HasChild { get; set; }
Expand Down
28 changes: 28 additions & 0 deletions src/Nest/DSL/Filter/FilterDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,34 @@ public FilterContainer Type(string type)
filter.Value = type;
return this.New(filter, f => f.Type = filter);
}

/// <summary>
/// The indices filter can be used when executed across multiple indices, allowing to have a
/// filter that executes only when executed on an index that matches a specific list of indices,
/// and another filter that executes when it is executed on an index that does not match the listed indices.
/// </summary>
public FilterContainer Indices<K>(Action<IndicesFilterDescriptor<K>> filterSelector) where K : class
{
var filter = new IndicesFilterDescriptor<K>();
if (filterSelector != null)
filterSelector(filter);

return this.New(filter, f => f.Indices = filter);
}

/// <summary>
/// The indices filter can be used when executed across multiple indices, allowing to have a
/// filter that executes only when executed on an index that matches a specific list of indices,
/// and another filter that executes when it is executed on an index that does not match the listed indices.
/// </summary>
public FilterContainer Indices(Action<IndicesFilterDescriptor<T>> filterSelector)
{
var filter = new IndicesFilterDescriptor<T>();
if (filterSelector != null)
filterSelector(filter);

return this.New(filter, f => f.Indices = filter);
}

/// <summary>
/// Filters documents matching the provided document / mapping type.
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/DSL/Filter/IFilterContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public interface IFilterContainer
[JsonProperty(PropertyName = "limit")]
ILimitFilter Limit { get; set; }

[JsonProperty(PropertyName = "indices")]
IIndicesFilter Indices { get; set; }

[JsonProperty(PropertyName = "type")]
ITypeFilter Type { get; set; }

Expand Down
140 changes: 140 additions & 0 deletions src/Nest/DSL/Filter/IndicesFilterDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeConverter<IndicesFilterDescriptor<object>>))]
public interface IIndicesFilter : IFilter
{
[JsonProperty("indices")]
IEnumerable<string> Indices { get; set; }

[JsonProperty("filter")]
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<FilterDescriptor<object>>, CustomJsonConverter>))]
IFilterContainer Filter { get; set; }

[JsonProperty("no_match_filter")]
[JsonConverter(typeof(NoMatchFilterConverter))]
IFilterContainer NoMatchFilter { get; set; }

}

public class NoMatchFilterConverter : CompositeJsonConverter<ReadAsTypeConverter<FilterDescriptor<object>>, CustomJsonConverter>
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
var en = serializer.Deserialize<NoMatchShortcut>(reader);
return new NoMatchFilterContainer {Shortcut = en};
}

return base.ReadJson(reader, objectType, existingValue, serializer);
}
}

public class NoMatchFilterContainer : FilterContainer, ICustomJson
{
public NoMatchShortcut? Shortcut { get; set; }

object ICustomJson.GetCustomJson()
{
if (this.Shortcut.HasValue) return this.Shortcut;
var f = ((IFilterContainer)this);
if (f.RawFilter.IsNullOrEmpty()) return f;
return new RawJson(f.RawFilter);
}
}

public class IndicesFilter : PlainFilter, IIndicesFilter
{
protected internal override void WrapInContainer(IFilterContainer container)
{
container.Indices = this;
}

bool IFilter.IsConditionless { get { return false; } }
public NestedScore? Score { get; set; }
public IFilterContainer Filter { get; set; }
public IFilterContainer NoMatchFilter { get; set; }
public IEnumerable<string> Indices { get; set; }
}

public class IndicesFilterDescriptor<T> : FilterBase, IIndicesFilter where T : class
{
IFilterContainer IIndicesFilter.Filter { get; set; }

IFilterContainer IIndicesFilter.NoMatchFilter { get; set; }

IEnumerable<string> IIndicesFilter.Indices { get; set; }

bool IFilter.IsConditionless
{
get
{
return ((IIndicesFilter)this).NoMatchFilter == null && ((IIndicesFilter)this).Filter == null;
}
}

public IndicesFilterDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
{
var qd = new FilterDescriptor<T>();
var q = filterSelector(qd);
if (q.IsConditionless)
return this;


((IIndicesFilter)this).Filter = q;
return this;
}

public IndicesFilterDescriptor<T> Filter<K>(Func<FilterDescriptor<K>, FilterContainer> filterSelector) where K : class
{
var qd = new FilterDescriptor<K>();
var q = filterSelector(qd);
if (q.IsConditionless)
return this;

((IIndicesFilter)this).Filter = q;
return this;
}

public IndicesFilterDescriptor<T> NoMatchFilter(NoMatchShortcut shortcut)
{
((IIndicesFilter)this).NoMatchFilter = new NoMatchFilterContainer { Shortcut = shortcut };
return this;
}

public IndicesFilterDescriptor<T> NoMatchFilter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
{
var qd = new FilterDescriptor<T>();
var q = filterSelector(qd);
if (q.IsConditionless)
return this;

((IIndicesFilter)this).NoMatchFilter = q;
return this;
}
public IndicesFilterDescriptor<T> NoMatchFilter<K>(Func<FilterDescriptor<K>, IFilterContainer> filterSelector) where K : class
{
var qd = new FilterDescriptor<K>();
var q = filterSelector(qd);
if (q.IsConditionless)
return this;

((IIndicesFilter)this).NoMatchFilter = q;
return this;
}
public IndicesFilterDescriptor<T> Indices(IEnumerable<string> indices)
{
((IIndicesFilter)this).Indices = indices;
return this;
}
}
}
38 changes: 36 additions & 2 deletions src/Nest/DSL/Query/IndicesQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Nest
[JsonConverter(typeof(ReadAsTypeConverter<IndicesQueryDescriptor<object>>))]
public interface IIndicesQuery : IQuery
{
[JsonProperty("indices")]
IEnumerable<string> Indices { get; set; }

[JsonProperty("score_mode"), JsonConverter(typeof (StringEnumConverter))]
NestedScore? Score { get; set; }

Expand All @@ -20,10 +23,35 @@ public interface IIndicesQuery : IQuery
IQueryContainer Query { get; set; }

[JsonProperty("no_match_query")]
[JsonConverter(typeof(NoMatchQueryConverter))]
IQueryContainer NoMatchQuery { get; set; }
}

[JsonProperty("indices")]
IEnumerable<string> Indices { get; set; }
public class NoMatchQueryConverter : CompositeJsonConverter<ReadAsTypeConverter<QueryDescriptor<object>>, CustomJsonConverter>
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
var en = serializer.Deserialize<NoMatchShortcut>(reader);
return new NoMatchQueryContainer {Shortcut = en};
}

return base.ReadJson(reader, objectType, existingValue, serializer);
}
}

public class NoMatchQueryContainer : QueryContainer, ICustomJson
{
public NoMatchShortcut? Shortcut { get; set; }

object ICustomJson.GetCustomJson()
{
if (this.Shortcut.HasValue) return this.Shortcut;
var f = ((IQueryContainer)this);
if (f.RawQuery.IsNullOrEmpty()) return f;
return new RawJson(f.RawQuery);
}
}

public class IndicesQuery : PlainQuery, IIndicesQuery
Expand Down Expand Up @@ -81,6 +109,12 @@ public IndicesQueryDescriptor<T> Query<K>(Func<QueryDescriptor<K>, QueryContaine
return this;
}

public IndicesQueryDescriptor<T> NoMatchQuery(NoMatchShortcut shortcut)
{
((IIndicesQuery)this).NoMatchQuery = new NoMatchQueryContainer { Shortcut = shortcut };
return this;
}

public IndicesQueryDescriptor<T> NoMatchQuery(Func<QueryDescriptor<T>, QueryContainer> querySelector)
{
var qd = new QueryDescriptor<T>();
Expand Down
13 changes: 13 additions & 0 deletions src/Nest/Domain/DSL/NoMatchShortcut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Nest
{
[JsonConverter(typeof (StringEnumConverter))]
public enum NoMatchShortcut
{
[EnumMember(Value = "none")] None,
[EnumMember(Value = "all")] All
}
}
4 changes: 3 additions & 1 deletion src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
<Compile Include="DSL\Filter\IndicesFilterDescriptor.cs" />
<Compile Include="Domain\DSL\NoMatchShortcut.cs" />
<Compile Include="DSL\UpgradeDescriptor.cs" />
<Compile Include="DSL\UpgradeStatusDescriptor.cs" />
<Compile Include="DSL\VerifyRepositoryDescriptor.cs" />
Expand Down Expand Up @@ -1096,4 +1098,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
4 changes: 3 additions & 1 deletion src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
<Compile Include="QueryParsers\Visitor\VisitorDemoUseCase.cs" />
<Compile Include="QueryParsers\Visitor\VisitorTests.cs" />
<Compile Include="Reproduce\Reproduce1146Tests.cs" />
<Compile Include="Reproduce\Reproduce1187Tests.cs" />
<Compile Include="Reproduce\Reproduce990Tests.cs" />
<Compile Include="Reproduce\Reproduce974Tests.cs" />
<Compile Include="Reproduce\Reproduce928Tests.cs" />
Expand All @@ -418,6 +419,7 @@
<Compile Include="Search\Filter\Modes\ConditionlessFilterJson.cs" />
<Compile Include="Search\Filter\Modes\FilterModesTests.cs" />
<Compile Include="Search\Filter\Singles\GeoHashCellFilterJson.cs" />
<Compile Include="Search\Filter\Singles\IndicesFilterJson.cs" />
<Compile Include="Search\Filter\Singles\TermsLookupFilterJson.cs" />
<Compile Include="Search\Filter\Singles\GeoIndexedShapeFilterJson.cs" />
<Compile Include="Search\Filter\Singles\GeoShapeFilterJson.cs" />
Expand Down Expand Up @@ -1318,4 +1320,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Loading