I try to search specific fields in multiple indices. I have two indices country
and region
. Both of the indices have a Field called name
.
I am able to specify my field name
and my indices in my query using elasticsaerchTemplate
:
@Override
public Page<SearchHit> searchAllTest(String text) {
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.queryStringQuery(text).field("name"));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withIndices("region", "country").build();
ResultsExtractor<Page<SearchHit>> rs = new ResultsExtractor<Page<SearchHit>>() {
@Override
public Page<SearchHit> extract(SearchResponse response) {
List<SearchHit> hits = Arrays.asList(response.getHits().getHits());
return new PageImpl<SearchHit>(hits, PageRequest.of(0, 10), response.getHits().getTotalHits());
}
};
return elasticsearchTemplate.query(nativeSearchQuery, rs);
}
This code works and searches for the field name in both of the indices. But I would like to specify the field name
in index region
and give for example a boost.
In simple words:
- Field
name
belongs to indexregion
and get a boost. - Field
name
belongs to indexcountry
and get no boost.
Is there a way to specify a field for a particular index?