Skip to content

Commit

Permalink
#446 - Search in original entry of display if no translations are ava…
Browse files Browse the repository at this point in the history
…ilable

- Modify codeable concept search query to look in original field
  • Loading branch information
michael-82 committed Jan 31, 2025
1 parent 60518bd commit 2db7d35
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,51 @@ private SearchHits<CodeableConceptDocument> findByCodeOrDisplay(String keyword,
});
}

BoolQuery boolQuery;
BoolQuery outerBoolQuery;

if (keyword.isEmpty()) {
boolQuery = new BoolQuery.Builder()
outerBoolQuery = new BoolQuery.Builder()
.filter(filterTerms.isEmpty() ? List.of() : filterTerms)
.build();

} else {
var mmQuery = new MultiMatchQuery.Builder()
// First the "upper" part of the query, when translations are present
var mustMultiMatchQueryWithTranslations = new MultiMatchQuery.Builder()
.query(keyword)
.fields(List.of("display.de", "display.en", "termcode.code^2"))
.build();

boolQuery = new BoolQuery.Builder()
.must(List.of(mmQuery._toQuery()))
var innerBoolQueryMatchTranslations = new BoolQuery.Builder()
.must(List.of(mustMultiMatchQueryWithTranslations._toQuery()))
.filter(filterTerms.isEmpty() ? List.of() : filterTerms)
.build();


// The "lower" part that will only be considered when the translations are empty
var mustMultiMatchQueryWithOriginal = new MultiMatchQuery.Builder()
.query(keyword)
.fields(List.of("display.original", "termcode.code^2"))
.build();

var innerBoolQueryMatchOriginal = new BoolQuery.Builder()
.must(List.of(mustMultiMatchQueryWithOriginal._toQuery()))
.filter(filterTerms.isEmpty() ? List.of() : filterTerms)
.build();

// Combine both parts in the top level bool query
outerBoolQuery = new BoolQuery.Builder()
.should(List.of(innerBoolQueryMatchTranslations._toQuery(), innerBoolQueryMatchOriginal._toQuery()))
.minimumShouldMatch("1")
.build();
}

var query = new NativeQueryBuilder()
.withQuery(boolQuery._toQuery())
.withQuery(outerBoolQuery._toQuery())
.withPageable(pageRequest)
.build();

log.info(query.getQuery().toString());

return operations.search(query, CodeableConceptDocument.class);
}
}

0 comments on commit 2db7d35

Please sign in to comment.