@@ -219,28 +219,35 @@ public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float bo
219219 throw new IllegalStateException ("Query " + in + " doesn't have any term" );
220220 }
221221 final String field = terms .iterator ().next ().field ();
222- final Map <Term , TermStates > termStates = new HashMap <>();
223- final List <TermStatistics > termStats = new ArrayList <>();
224- for (Term term : terms ) {
225- TermStates ts = termStates .computeIfAbsent (term , t -> {
226- try {
227- return TermStates .build (searcher .getTopReaderContext (), t , scoreMode .needsScores ());
228- } catch (IOException e ) {
229- throw new UncheckedIOException (e );
230- }
231- });
232- if (scoreMode .needsScores ()) {
233- if (ts .docFreq () > 0 ) {
234- termStats .add (searcher .termStatistics (term , ts .docFreq (), ts .totalTermFreq ()));
222+ final CollectionStatistics collectionStatistics = searcher .collectionStatistics (field );
223+ final SimScorer simScorer ;
224+ final Weight approximationWeight ;
225+ if (collectionStatistics == null ) {
226+ // field does not exist in the index
227+ simScorer = null ;
228+ approximationWeight = null ;
229+ } else {
230+ final Map <Term , TermStates > termStates = new HashMap <>();
231+ final List <TermStatistics > termStats = new ArrayList <>();
232+ for (Term term : terms ) {
233+ TermStates ts = termStates .computeIfAbsent (term , t -> {
234+ try {
235+ return TermStates .build (searcher .getTopReaderContext (), t , scoreMode .needsScores ());
236+ } catch (IOException e ) {
237+ throw new UncheckedIOException (e );
238+ }
239+ });
240+ if (scoreMode .needsScores ()) {
241+ if (ts .docFreq () > 0 ) {
242+ termStats .add (searcher .termStatistics (term , ts .docFreq (), ts .totalTermFreq ()));
243+ }
244+ } else {
245+ termStats .add (new TermStatistics (term .bytes (), 1 , 1L ));
235246 }
236- } else {
237- termStats .add (new TermStatistics (term .bytes (), 1 , 1L ));
238247 }
248+ simScorer = searcher .getSimilarity ().scorer (boost , collectionStatistics , termStats .toArray (TermStatistics []::new ));
249+ approximationWeight = searcher .createWeight (approximate (in ), ScoreMode .COMPLETE_NO_SCORES , 1f );
239250 }
240- final SimScorer simScorer = searcher .getSimilarity ()
241- .scorer (boost , searcher .collectionStatistics (field ), termStats .toArray (TermStatistics []::new ));
242- final Weight approximationWeight = searcher .createWeight (approximate (in ), ScoreMode .COMPLETE_NO_SCORES , 1f );
243-
244251 return new Weight (this ) {
245252
246253 @ Override
@@ -272,7 +279,7 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
272279
273280 @ Override
274281 public RuntimePhraseScorer scorer (LeafReaderContext context ) throws IOException {
275- final Scorer approximationScorer = approximationWeight .scorer (context );
282+ final Scorer approximationScorer = approximationWeight != null ? approximationWeight .scorer (context ) : null ;
276283 if (approximationScorer == null ) {
277284 return null ;
278285 }
0 commit comments