Skip to content

Commit 0d679e2

Browse files
committed
rerank buildRankFeaturePhaseShardContext add SearchContext parameter
TextSimilarityRankBuilder add check field
1 parent d4ad759 commit 0d679e2

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/rank/MockedRequestActionBasedRerankerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId)
542542
}
543543
};
544544
else {
545-
return super.buildRankFeaturePhaseShardContext();
545+
return super.buildRankFeaturePhaseShardContext(searchContext);
546546
}
547547
}
548548

server/src/test/java/org/elasticsearch/search/SearchServiceSingleNodeTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public RankShardResult combineQueryPhaseResults(List<TopDocs> rankResults) {
511511
}
512512

513513
@Override
514-
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext() {
514+
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
515515
return new RankFeaturePhaseRankShardContext(rankFeatureFieldName) {
516516
@Override
517517
public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId) {
@@ -748,7 +748,7 @@ public RankShardResult combineQueryPhaseResults(List<TopDocs> rankResults) {
748748
}
749749

750750
@Override
751-
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext() {
751+
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
752752
return new RankFeaturePhaseRankShardContext(rankFeatureFieldName) {
753753
@Override
754754
public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId) {
@@ -875,7 +875,7 @@ public RankShardResult combineQueryPhaseResults(List<TopDocs> rankResults) {
875875
}
876876

877877
@Override
878-
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext() {
878+
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
879879
return new RankFeaturePhaseRankShardContext(rankFeatureFieldName) {
880880
@Override
881881
public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId) {
@@ -1008,7 +1008,7 @@ public RankShardResult combineQueryPhaseResults(List<TopDocs> rankResults) {
10081008
}
10091009

10101010
@Override
1011-
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext() {
1011+
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
10121012
return new RankFeaturePhaseRankShardContext(rankFeatureFieldName) {
10131013
@Override
10141014
public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId) {
@@ -1136,7 +1136,7 @@ public RankShardResult combineQueryPhaseResults(List<TopDocs> rankResults) {
11361136
}
11371137

11381138
@Override
1139-
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext() {
1139+
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
11401140
return new RankFeaturePhaseRankShardContext(rankFeatureFieldName) {
11411141
@Override
11421142
public RankShardResult buildRankFeatureShardResult(SearchHits hits, int shardId) {

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rank/textsimilarity/TextSimilarityRankBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ public QueryPhaseRankCoordinatorContext buildQueryPhaseCoordinatorContext(int si
170170
public RankFeaturePhaseRankShardContext buildRankFeaturePhaseShardContext(SearchContext searchContext) {
171171

172172
// check field in mapping
173-
Mapper mapper = searchContext.indexShard().mapperService().mappingLookup().getMapper(field);
173+
Mapper mapper;
174+
try {
175+
mapper = searchContext.indexShard().mapperService().mappingLookup().getMapper(field);
176+
} catch (NullPointerException e) {
177+
mapper = null;
178+
}
179+
174180
if (mapper == null) {
175181
throw new IllegalArgumentException("field [" + field + "] does not exist in mapping");
176182
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/rank/textsimilarity/TextSimilarityRankTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ public void testRerankInputSizeAndInferenceResultsMismatch() {
262262
assertThat(ex.getDetailedMessage(), containsString("Reranker input document count and returned score count mismatch"));
263263
}
264264

265+
public void testRerankInputSizeAndInferenceResultsFieldMissing() {
266+
SearchPhaseExecutionException ex = expectThrows(
267+
SearchPhaseExecutionException.class,
268+
// Execute search with text similarity reranking
269+
client.prepareSearch()
270+
.setRankBuilder(new TextSimilarityRankBuilder("missing_field", "my-rerank-model", "my query", 100, 0.0f, false))
271+
.setQuery(QueryBuilders.matchAllQuery())
272+
);
273+
assertThat(ex.status(), equalTo(RestStatus.BAD_REQUEST));
274+
assertThat(ex.getDetailedMessage(), containsString("field [missing_field] does not exist in mapping"));
275+
}
276+
265277
private static Matcher<SearchHit> searchHitWith(int expectedRank, float expectedScore, String expectedText) {
266278
return allOf(
267279
hasRank(expectedRank),

0 commit comments

Comments
 (0)