-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSpanQueryDescriptor.cs
144 lines (121 loc) · 4.52 KB
/
SpanQueryDescriptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeConverter<SpanQuery<object>>))]
public interface ISpanQuery : IQuery
{
[JsonProperty(PropertyName = "span_term")]
ISpanTermQuery SpanTermQueryDescriptor { get; set; }
[JsonProperty(PropertyName = "span_first")]
ISpanFirstQuery SpanFirst { get; set; }
[JsonProperty(PropertyName = "span_near")]
ISpanNearQuery SpanNear { get; set; }
[JsonProperty(PropertyName = "span_or")]
ISpanOrQuery SpanOr { get; set; }
[JsonProperty(PropertyName = "span_not")]
ISpanNotQuery SpanNot { get; set; }
[JsonProperty(PropertyName = "span_multi")]
ISpanMultiTermQuery SpanMultiTerm { get; set; }
}
public class SpanQuery : ISpanQuery
{
bool IQuery.IsConditionless { get { return false; } }
public ISpanTermQuery SpanTermQueryDescriptor { get; set; }
public ISpanFirstQuery SpanFirst { get; set; }
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
{
ISpanTermQuery ISpanQuery.SpanTermQueryDescriptor { get; set; }
ISpanFirstQuery ISpanQuery.SpanFirst { get; set; }
ISpanNearQuery ISpanQuery.SpanNear { get; set; }
ISpanOrQuery ISpanQuery.SpanOr { get; set; }
ISpanNotQuery ISpanQuery.SpanNot { get; set; }
ISpanMultiTermQuery ISpanQuery.SpanMultiTerm { get; set; }
bool IQuery.IsConditionless
{
get
{
var queries = new[]
{
((ISpanQuery)this).SpanTermQueryDescriptor as IQuery,
((ISpanQuery)this).SpanFirst as IQuery,
((ISpanQuery)this).SpanNear as IQuery,
((ISpanQuery)this).SpanOr as IQuery,
((ISpanQuery)this).SpanNot as IQuery,
((ISpanQuery)this).SpanMultiTerm as IQuery
};
return queries.All(q => q == null || q.IsConditionless);
}
}
public SpanQuery<T> SpanTerm(Expression<Func<T, object>> fieldDescriptor
, string value
, double? Boost = null)
{
if (fieldDescriptor == null || value.IsNullOrEmpty())
return this;
var spanTerm = new SpanTermQueryDescriptor<T>();
((ITermQuery)spanTerm).Field = fieldDescriptor;
((ITermQuery)spanTerm).Value = value;
((ITermQuery)spanTerm).Boost = Boost;
return CreateQuery(spanTerm, (sq) => ((ISpanQuery)sq).SpanTermQueryDescriptor = spanTerm);
}
public SpanQuery<T> SpanTerm(string field, string value, double? Boost = null)
{
if (field.IsNullOrEmpty() || value.IsNullOrEmpty())
return this;
var spanTerm = new SpanTermQueryDescriptor<T>();
((ITermQuery)spanTerm).Field = field;
((ITermQuery)spanTerm).Value = value;
((ITermQuery)spanTerm).Boost = Boost;
return CreateQuery(spanTerm, (sq) => ((ISpanQuery)sq).SpanTermQueryDescriptor = spanTerm);
}
public SpanQuery<T> SpanFirst(Func<SpanFirstQueryDescriptor<T>, SpanFirstQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var q = selector(new SpanFirstQueryDescriptor<T>());
return CreateQuery(q, (sq) => ((ISpanQuery)sq).SpanFirst = q);
}
public SpanQuery<T> SpanNear(Func<SpanNearQueryDescriptor<T>, SpanNearQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var q = selector(new SpanNearQueryDescriptor<T>());
return CreateQuery(q, (sq) => ((ISpanQuery)sq).SpanNear = q);
}
public SpanQuery<T> SpanOr(Func<SpanOrQueryDescriptor<T>, SpanOrQueryDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var q = selector(new SpanOrQueryDescriptor<T>());
return CreateQuery(q, (sq) => ((ISpanQuery)sq).SpanOr = q);
}
public SpanQuery<T> SpanNot(Func<SpanNotQuery<T>, SpanNotQuery<T>> selector)
{
selector.ThrowIfNull("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
{
if (((IQuery)(query)).IsConditionless)
return this;
var newSpanQuery = new SpanQuery<T>();
setProperty(newSpanQuery);
return newSpanQuery;
}
}
}