Skip to content

Commit ab136d0

Browse files
committed
Allow multiple queries for rescoring
See #2088
1 parent 4308316 commit ab136d0

File tree

9 files changed

+535
-34
lines changed

9 files changed

+535
-34
lines changed

docs/query-dsl/bool-dsl/bool-dsl.asciidoc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ image::hadouken-indentation.jpg[hadouken indenting]
3232

3333
=== Operator Overloading
3434

35-
For this reason, NEST introduces **operator overloading** so complex bool queries become easier to write.
35+
For this reason, NEST introduces **operator overloading** so complex bool queries become easier to write.
3636
The previous example now becomes the following with the fluent API
3737

3838
[source,csharp]
@@ -48,14 +48,14 @@ or, using the object initializer syntax
4848
----
4949
searchResults = this.Client.Search<Project>(new SearchRequest<Project>
5050
{
51-
Query = new TermQuery { Field = "name", Value= "x" }
51+
Query = new TermQuery { Field = "name", Value= "x" }
5252
|| new TermQuery { Field = Field<Project>(p=>p.Name), Value = "y" }
5353
});
5454
----
5555

56-
A naive implementation of operator overloading would rewrite
56+
A naive implementation of operator overloading would rewrite
5757

58-
`term && term && term` to
58+
`term && term && term` to
5959

6060
....
6161
bool
@@ -67,12 +67,12 @@ bool
6767
|___term
6868
....
6969

70-
As you can image this becomes unwieldy quite fast the more complex a query becomes NEST can spot these and
70+
As you can image this becomes unwieldy quite fast the more complex a query becomes NEST can spot these and
7171
join them together to become a single bool query
7272

7373
....
7474
bool
75-
|___must
75+
|___must
7676
|___term
7777
|___term
7878
|___term
@@ -119,7 +119,7 @@ When combining multiple queries some or all possibly marked as `must_not` or `fi
119119

120120
....
121121
bool
122-
|___must
122+
|___must
123123
| |___term
124124
| |___term
125125
| |___term
@@ -148,14 +148,14 @@ Even more involved `term && term && term && !term && +term && +term` still only
148148

149149
....
150150
bool
151-
|___must
151+
|___must
152152
| |___term
153153
| |___term
154154
| |___term
155155
|
156156
|___must_not
157157
| |___term
158-
|
158+
|
159159
|___filter
160160
|___term
161161
|___term
@@ -180,7 +180,7 @@ c.Bool.MustNot.Should().HaveCount(1);
180180
c.Bool.Filter.Should().HaveCount(2);
181181
----
182182

183-
You can still mix and match actual bool queries with the bool DSL e.g`bool(must=term, term, term) && !term` would still merge into a single `bool` query.
183+
You can still mix and match actual bool queries with the bool DSL e.g`bool(must=term, term, term) && !term` would still merge into a single `bool` query.
184184

185185
[source,csharp]
186186
----
@@ -225,15 +225,15 @@ lastClause.Bool.Should.Should().HaveCount(3);
225225

226226
TIP: *add parentheses to force evaluation order*
227227

228-
Also note that using shoulds as boosting factors can be really powerful so if you need this
228+
Also note that using shoulds as boosting factors can be really powerful so if you need this
229229
always remember that you can mix and match an actual bool query with the bool dsl.
230230

231231
There is another subtle situation where NEST will not blindly merge 2 bool queries with only should clauses. Imagine the following:
232232

233-
`bool(should=term1, term2, term3, term4, minimum_should_match=2) || term5 || term6`
233+
`bool(should=term1, term2, term3, term4, minimum_should_match=2) || term5 || term6`
234234

235-
if NEST identified both sides of the OR operation as only containing `should` clauses and it would
236-
join them together it would give a different meaning to the `minimum_should_match` parameter of the first boolean query.
235+
if NEST identified both sides of the OR operation as only containing `should` clauses and it would
236+
join them together it would give a different meaning to the `minimum_should_match` parameter of the first boolean query.
237237
Rewriting this to a single bool with 5 `should` clauses would break because only matching on `term5` or `term6` should still be a hit.
238238

239239
[source,csharp]
@@ -265,8 +265,8 @@ nestedBool.Bool.Should.Should().HaveCount(4);
265265

266266
=== Locked bool queries
267267

268-
NEST will not combine `bool` queries if any of the query metadata is set e.g if metadata such as `boost` or `name` are set,
269-
NEST will treat these as locked
268+
NEST will not combine `bool` queries if any of the query metadata is set e.g if metadata such as `boost` or `name` are set,
269+
NEST will treat these as locked
270270

271271
Here we demonstrate that two locked `bool` queries are not combined
272272

@@ -317,7 +317,7 @@ nestedBool.Bool.Name.Should().Be(firstName);
317317

318318
[source,csharp]
319319
----
320-
assert(fluent.InvokeQuery(new QueryContainerDescriptor<Project>()));
320+
assert(fluent.Invoke(new QueryContainerDescriptor<Project>()));
321321
322322
assert((QueryContainer)ois);
323323
----
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/master
2+
3+
:github: https://github.com/elastic/elasticsearch-net
4+
5+
:nuget: https://www.nuget.org/packages
6+
7+
[[verbatim-and-strict-query-usage]]
8+
== Verbatim And Strict Query Usage
9+
10+
`IsVerbatim` should be set on individual queries to take effect
11+
12+
=== Fluent DSL Example
13+
14+
[source,csharp]
15+
----
16+
q
17+
.Bool(b => b
18+
.Must(qt => qt
19+
.Term(t => t
20+
.Verbatim()
21+
.Field(p => p.Description)
22+
.Value("")
23+
), qt => qt
24+
.Term(t => t
25+
.Field(p => p.Name)
26+
.Value("foo")
27+
)
28+
)
29+
)
30+
----
31+
32+
=== Object Initializer Syntax Example
33+
34+
[source,csharp]
35+
----
36+
new TermQuery
37+
{
38+
IsVerbatim = true,
39+
Field = "description",
40+
Value = ""
41+
}
42+
&& new TermQuery
43+
{
44+
Field = "name",
45+
Value = "foo"
46+
}
47+
----
48+
49+
[source,javascript]
50+
.Example json output
51+
----
52+
{
53+
"bool": {
54+
"must": [
55+
{
56+
"term": {
57+
"description": {
58+
"value": ""
59+
}
60+
}
61+
},
62+
{
63+
"term": {
64+
"name": {
65+
"value": "foo"
66+
}
67+
}
68+
}
69+
]
70+
}
71+
}
72+
----
73+
74+
Setting `IsVerbatim` on a compound query is still supported though
75+
76+
=== Fluent DSL Example
77+
78+
[source,csharp]
79+
----
80+
q
81+
.Bool(b => b
82+
.Verbatim()
83+
)
84+
----
85+
86+
=== Object Initializer Syntax Example
87+
88+
[source,csharp]
89+
----
90+
new BoolQuery
91+
{
92+
IsVerbatim = true,
93+
}
94+
----
95+
96+
[source,javascript]
97+
.Example json output
98+
----
99+
{
100+
"bool": {}
101+
}
102+
----
103+
104+
=== Fluent DSL Example
105+
106+
[source,csharp]
107+
----
108+
q
109+
.Term(t => t
110+
.Verbatim()
111+
.Field(p => p.Description)
112+
.Value("")
113+
)
114+
----
115+
116+
=== Object Initializer Syntax Example
117+
118+
[source,csharp]
119+
----
120+
new TermQuery
121+
{
122+
IsVerbatim = true,
123+
Field = "description",
124+
Value = ""
125+
}
126+
----
127+
128+
[source,javascript]
129+
.Example json output
130+
----
131+
{
132+
"term": {
133+
"description": {
134+
"value": ""
135+
}
136+
}
137+
}
138+
----
139+
140+
[source,csharp]
141+
----
142+
var e = Assert.Throws<ArgumentException>(() =>
143+
new SearchRequest<Project>
144+
{
145+
Query = new TermQuery
146+
{
147+
IsStrict = true,
148+
Field = "myfield",
149+
Value = ""
150+
}
151+
}
152+
);
153+
154+
e.Message.Should().Be("Query is conditionless but strict is turned on");
155+
----
156+
157+
[source,csharp]
158+
----
159+
var e = Assert.Throws<ArgumentException>(() =>
160+
new SearchDescriptor<Project>()
161+
.Query(q => q
162+
.Term(t => t
163+
.Strict()
164+
.Field("myfield")
165+
.Value("")
166+
)
167+
)
168+
);
169+
e.Message.Should().Be("Query is conditionless but strict is turned on");
170+
----
171+

0 commit comments

Comments
 (0)