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
25 changes: 20 additions & 5 deletions src/Nest/QueryDsl/Span/Term/SpanTermQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,33 @@ namespace Nest
{
[InterfaceDataContract]
[JsonFormatter(typeof(FieldNameQueryFormatter<SpanTermQuery, ISpanTermQuery>))]
public interface ISpanTermQuery : ITermQuery, ISpanSubQuery { }
public interface ISpanTermQuery : ISpanSubQuery, IFieldNameQuery
{
[DataMember(Name = "value")]
[JsonFormatter(typeof(SourceWriteFormatter<object>))]
object Value { get; set; }
}

[DataContract]
public class SpanTermQuery : FieldNameQueryBase, ISpanTermQuery
{
public object Value { get; set; }

protected override bool Conditionless => TermQuery.IsConditionless(this);
protected override bool Conditionless => IsConditionless(this);

internal override void InternalWrapInContainer(IQueryContainer c) => c.SpanTerm = this;

internal static bool IsConditionless(ISpanTermQuery q) => q.Value == null || q.Value.ToString().IsNullOrEmpty() || q.Field.IsConditionless();
}

public class SpanTermQueryDescriptor<T> : TermQueryDescriptorBase<SpanTermQueryDescriptor<T>, ISpanTermQuery, T>, ISpanTermQuery
where T : class { }
public class SpanTermQueryDescriptor<T> : FieldNameQueryDescriptorBase<SpanTermQueryDescriptor<T>, ISpanTermQuery, T>, ISpanTermQuery
where T : class
{
protected override bool Conditionless => SpanTermQuery.IsConditionless(this);

object ISpanTermQuery.Value { get; set; }

public SpanTermQueryDescriptor<T> Value(object value) =>
Assign(value, (a, v) => a.Value = v);
}
}
1 change: 1 addition & 0 deletions src/Nest/QueryDsl/TermLevel/Prefix/PrefixQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PrefixQuery : FieldNameQueryBase, IPrefixQuery
{
public MultiTermQueryRewrite Rewrite { get; set; }
public object Value { get; set; }
public bool? CaseInsensitive { get; set; }
protected override bool Conditionless => TermQuery.IsConditionless(this);

internal override void InternalWrapInContainer(IQueryContainer c) => c.Prefix = this;
Expand Down
12 changes: 12 additions & 0 deletions src/Nest/QueryDsl/TermLevel/Term/TermQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ public interface ITermQuery : IFieldNameQuery
[DataMember(Name = "value")]
[JsonFormatter(typeof(SourceWriteFormatter<object>))]
object Value { get; set; }

[DataMember(Name = "case_insensitive")]
bool? CaseInsensitive { get; set; }
}

[DataContract]
public class TermQuery : FieldNameQueryBase, ITermQuery
{
public object Value { get; set; }

public bool? CaseInsensitive { get; set; }

protected override bool Conditionless => IsConditionless(this);

Expand All @@ -37,12 +42,19 @@ public abstract class TermQueryDescriptorBase<TDescriptor, TInterface, T>
{
protected override bool Conditionless => TermQuery.IsConditionless(this);
object ITermQuery.Value { get; set; }
bool? ITermQuery.CaseInsensitive { get; set; }

public TDescriptor Value(object value)
{
Self.Value = value;
return (TDescriptor)this;
}

public TDescriptor CaseInsensitive(bool? caseInsensitive = true)
{
Self.CaseInsensitive = caseInsensitive;
return (TDescriptor)this;
}
}

public class TermQueryDescriptor<T> : TermQueryDescriptorBase<TermQueryDescriptor<T>, ITermQuery, T>
Expand Down
1 change: 1 addition & 0 deletions src/Nest/QueryDsl/TermLevel/Wildcard/WildcardQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class WildcardQuery : FieldNameQueryBase, IWildcardQuery
{
public MultiTermQueryRewrite Rewrite { get; set; }
public object Value { get; set; }
public bool? CaseInsensitive { get; set; }
protected override bool Conditionless => TermQuery.IsConditionless(this);

internal override void InternalWrapInContainer(IQueryContainer c) => c.Wildcard = this;
Expand Down
57 changes: 57 additions & 0 deletions tests/Tests/QueryDsl/TermLevel/Term/TermQueryUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,61 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
.Value(string.Empty)
);
}

//hide
[SkipVersion("<7.10.0", "Case insensitive flag added in 7.10.0")]
public class TermQueryWithCaseInsensitiveUsageTests : QueryDslUsageTestsBase
{
public TermQueryWithCaseInsensitiveUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<ITermQuery>(q => q.Term)
{
q => q.Field = null,
q => q.Value = " ",
q => q.Value = null
};

protected override QueryContainer QueryInitializer => new TermQuery
{
Name = "named_query",
Boost = 1.1,
Field = "description",
Value = "project description",
CaseInsensitive = true
};

protected override object QueryJson => new
{
term = new
{
description = new
{
_name = "named_query",
boost = 1.1,
value = "project description",
case_insensitive = true
}
}
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.Term(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Description)
.Value("project description")
.CaseInsensitive(true)
);

//hide
[U]
public void DeserializeShortForm()
{
using var stream = new MemoryStream(ShortFormQuery);
var query = Client.RequestResponseSerializer.Deserialize<ITermQuery>(stream);
query.Should().NotBeNull();
query.Field.Should().Be(new Field("description"));
query.Value.Should().Be("project description");
}
}
}