23
23
import org .apache .lucene .analysis .TokenStream ;
24
24
import org .apache .lucene .analysis .tokenattributes .CharTermAttribute ;
25
25
import org .apache .lucene .index .Term ;
26
- import org .apache .lucene .search .BooleanClause ;
27
- import org .apache .lucene .search .DisjunctionMaxQuery ;
28
- import org .apache .lucene .search .FuzzyQuery ;
29
- import org .apache .lucene .search .MatchNoDocsQuery ;
30
- import org .apache .lucene .search .MultiPhraseQuery ;
31
- import org .apache .lucene .search .PhraseQuery ;
32
- import org .apache .lucene .search .Query ;
26
+ import org .apache .lucene .search .*;
33
27
import org .apache .lucene .util .IOUtils ;
34
28
import org .apache .lucene .util .automaton .RegExp ;
35
29
import org .elasticsearch .common .lucene .search .Queries ;
41
35
import org .elasticsearch .index .query .support .QueryParsers ;
42
36
43
37
import java .io .IOException ;
44
- import java .util .ArrayList ;
45
- import java .util .Collection ;
46
- import java .util .HashMap ;
47
- import java .util .List ;
48
- import java .util .Map ;
49
- import java .util .Objects ;
38
+ import java .util .*;
50
39
51
40
import static java .util .Collections .unmodifiableMap ;
52
41
import static org .elasticsearch .common .lucene .search .Queries .fixNegativeQueryIfNeeded ;
@@ -148,8 +137,7 @@ public Query getFieldQuery(String field, String queryText, boolean quoted) throw
148
137
Query q = getFieldQuerySingle (mField , queryText , quoted );
149
138
if (q != null ) {
150
139
added = true ;
151
- applyBoost (mField , q );
152
- disMaxQuery .add (q );
140
+ disMaxQuery .add (applyBoost (mField , q ));
153
141
}
154
142
}
155
143
if (!added ) {
@@ -161,8 +149,7 @@ public Query getFieldQuery(String field, String queryText, boolean quoted) throw
161
149
for (String mField : fields ) {
162
150
Query q = getFieldQuerySingle (mField , queryText , quoted );
163
151
if (q != null ) {
164
- applyBoost (mField , q );
165
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
152
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
166
153
}
167
154
}
168
155
if (clauses .size () == 0 ) // happens for stopwords
@@ -250,9 +237,8 @@ protected Query getFieldQuery(String field, String queryText, int slop) throws P
250
237
Query q = super .getFieldQuery (mField , queryText , slop );
251
238
if (q != null ) {
252
239
added = true ;
253
- applyBoost (mField , q );
254
240
q = applySlop (q , slop );
255
- disMaxQuery .add (q );
241
+ disMaxQuery .add (applyBoost ( mField , q ) );
256
242
}
257
243
}
258
244
if (!added ) {
@@ -264,9 +250,8 @@ protected Query getFieldQuery(String field, String queryText, int slop) throws P
264
250
for (String mField : fields ) {
265
251
Query q = super .getFieldQuery (mField , queryText , slop );
266
252
if (q != null ) {
267
- applyBoost (mField , q );
268
253
q = applySlop (q , slop );
269
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
254
+ clauses .add (new BooleanClause (applyBoost ( mField , q ) , BooleanClause .Occur .SHOULD ));
270
255
}
271
256
}
272
257
if (clauses .size () == 0 ) // happens for stopwords
@@ -305,8 +290,7 @@ protected Query getRangeQuery(String field, String part1, String part2, boolean
305
290
Query q = getRangeQuerySingle (mField , part1 , part2 , startInclusive , endInclusive );
306
291
if (q != null ) {
307
292
added = true ;
308
- applyBoost (mField , q );
309
- disMaxQuery .add (q );
293
+ disMaxQuery .add (applyBoost (mField , q ));
310
294
}
311
295
}
312
296
if (!added ) {
@@ -318,8 +302,7 @@ protected Query getRangeQuery(String field, String part1, String part2, boolean
318
302
for (String mField : fields ) {
319
303
Query q = getRangeQuerySingle (mField , part1 , part2 , startInclusive , endInclusive );
320
304
if (q != null ) {
321
- applyBoost (mField , q );
322
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
305
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
323
306
}
324
307
}
325
308
if (clauses .size () == 0 ) // happens for stopwords
@@ -371,8 +354,7 @@ protected Query getFuzzyQuery(String field, String termStr, String minSimilarity
371
354
Query q = getFuzzyQuerySingle (mField , termStr , minSimilarity );
372
355
if (q != null ) {
373
356
added = true ;
374
- applyBoost (mField , q );
375
- disMaxQuery .add (q );
357
+ disMaxQuery .add (applyBoost (mField , q ));
376
358
}
377
359
}
378
360
if (!added ) {
@@ -383,8 +365,9 @@ protected Query getFuzzyQuery(String field, String termStr, String minSimilarity
383
365
List <BooleanClause > clauses = new ArrayList <>();
384
366
for (String mField : fields ) {
385
367
Query q = getFuzzyQuerySingle (mField , termStr , minSimilarity );
386
- applyBoost (mField , q );
387
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
368
+ if (q != null ) {
369
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
370
+ }
388
371
}
389
372
return getBooleanQuery (clauses , true );
390
373
}
@@ -434,8 +417,7 @@ protected Query getPrefixQuery(String field, String termStr) throws ParseExcepti
434
417
Query q = getPrefixQuerySingle (mField , termStr );
435
418
if (q != null ) {
436
419
added = true ;
437
- applyBoost (mField , q );
438
- disMaxQuery .add (q );
420
+ disMaxQuery .add (applyBoost (mField , q ));
439
421
}
440
422
}
441
423
if (!added ) {
@@ -447,8 +429,7 @@ protected Query getPrefixQuery(String field, String termStr) throws ParseExcepti
447
429
for (String mField : fields ) {
448
430
Query q = getPrefixQuerySingle (mField , termStr );
449
431
if (q != null ) {
450
- applyBoost (mField , q );
451
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
432
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
452
433
}
453
434
}
454
435
if (clauses .size () == 0 ) // happens for stopwords
@@ -566,8 +547,7 @@ protected Query getWildcardQuery(String field, String termStr) throws ParseExcep
566
547
Query q = getWildcardQuerySingle (mField , termStr );
567
548
if (q != null ) {
568
549
added = true ;
569
- applyBoost (mField , q );
570
- disMaxQuery .add (q );
550
+ disMaxQuery .add (applyBoost (mField , q ));
571
551
}
572
552
}
573
553
if (!added ) {
@@ -579,8 +559,7 @@ protected Query getWildcardQuery(String field, String termStr) throws ParseExcep
579
559
for (String mField : fields ) {
580
560
Query q = getWildcardQuerySingle (mField , termStr );
581
561
if (q != null ) {
582
- applyBoost (mField , q );
583
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
562
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
584
563
}
585
564
}
586
565
if (clauses .size () == 0 ) // happens for stopwords
@@ -697,8 +676,7 @@ protected Query getRegexpQuery(String field, String termStr) throws ParseExcepti
697
676
Query q = getRegexpQuerySingle (mField , termStr );
698
677
if (q != null ) {
699
678
added = true ;
700
- applyBoost (mField , q );
701
- disMaxQuery .add (q );
679
+ disMaxQuery .add (applyBoost (mField , q ));
702
680
}
703
681
}
704
682
if (!added ) {
@@ -710,8 +688,7 @@ protected Query getRegexpQuery(String field, String termStr) throws ParseExcepti
710
688
for (String mField : fields ) {
711
689
Query q = getRegexpQuerySingle (mField , termStr );
712
690
if (q != null ) {
713
- applyBoost (mField , q );
714
- clauses .add (new BooleanClause (q , BooleanClause .Occur .SHOULD ));
691
+ clauses .add (new BooleanClause (applyBoost (mField , q ), BooleanClause .Occur .SHOULD ));
715
692
}
716
693
}
717
694
if (clauses .size () == 0 ) // happens for stopwords
@@ -761,11 +738,12 @@ protected Query getBooleanQuery(List<BooleanClause> clauses, boolean disableCoor
761
738
return fixNegativeQueryIfNeeded (q );
762
739
}
763
740
764
- private void applyBoost (String field , Query q ) {
741
+ private Query applyBoost (String field , Query q ) {
765
742
Float fieldBoost = settings .fieldsAndWeights ().get (field );
766
- if (fieldBoost != null ) {
767
- q . setBoost ( fieldBoost );
743
+ if (fieldBoost != null && fieldBoost != 1f ) {
744
+ return new BoostQuery ( q , fieldBoost );
768
745
}
746
+ return q ;
769
747
}
770
748
771
749
private Query applySlop (Query q , int slop ) {
@@ -779,7 +757,9 @@ private Query applySlop(Query q, int slop) {
779
757
builder .add (terms [i ], positions [i ]);
780
758
}
781
759
pq = builder .build ();
782
- pq .setBoost (q .getBoost ());
760
+ //make sure that the boost hasn't been set beforehand, otherwise we'd lose it
761
+ assert q .getBoost () == 1f ;
762
+ assert q instanceof BoostQuery == false ;
783
763
return pq ;
784
764
} else if (q instanceof MultiPhraseQuery ) {
785
765
((MultiPhraseQuery ) q ).setSlop (slop );
0 commit comments