You can create a compound index with Spring Data MongoDB like this:
@Document
@CompoundIndex(def = "{'anIntegerProperty': 1, 'textProperty': 'text'}")
Then create a Repository:
@Repository
public interface TheDocuemntRepository extends MongoRepository<TheDocuemnt, String> {
Stream<TheDocuemnt> findByAnIntegerPropertyOrderByScoreDesc(Integer anIntegerProperty, TextCriteria criteria);
}
Consider MongoDB documentation:
Compound Index
A compound index can include a text index key in combination with ascending/descending index keys. However, these compound indexes have the following restrictions:
A compound text index cannot include any other special index types, such as multi-key or geospatial index fields.
If the compound text index includes keys preceding the text index key, to perform a $text search, the query predicate must include equality match conditions on the preceding keys.
Filter search results by language
If you also want to filter search results by language, you should try this approach: Multi-language documents example
The entity
@CompoundIndex(def = "{'anIntegerProperty': 1, 'language': 1, 'textProperty': 'text'}")
The repository
Sort TEXT_SCORE_SORT = new Sort(Direction.DESC, "score");
@Query("{'anIntegerProperty': ?0, 'language': ?1, $text: {$search: ?2, $language: ?1}}")
Stream<TheDocuemnt> findTheDocument(Integer property, String language, String criteria, Sort sort);