Sau một tìm kiếm dài, tôi nghĩ rằng hiện tại không phải là posible bằng cách sử dụng chú thích, vì vậy tôi tạo ra một cách lập trình để quản lý kịch bản này.
tạo chỉ mục bình thường và chỉ mục tổng hợp
Trước tiên tôi đã xác định một đối tượng để ánh xạ cấu trúc chỉ mục.
public class CollectionDefinition {
private String id;
private String collectionName;
private LinkedHashMap<String, Sort.Direction> normalIndexMap;
private ArrayList<String> compoundIndexMap;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public LinkedHashMap<String, Sort.Direction> getNormalIndexMap() {
return normalIndexMap;
}
public void setNormalIndexMap(LinkedHashMap<String, Sort.Direction> normalIndexMap) {
this.normalIndexMap = normalIndexMap;
}
public ArrayList<String> getCompoundIndexMap() {
return compoundIndexMap;
}
public void setCompoundIndexMap(ArrayList<String> compoundIndexMap) {
this.compoundIndexMap = compoundIndexMap;
}
Bộ sưu tập Json sẽ trông như thế này
{
"collectionName" : "example",
"normalIndexMap" : {
"sessionId" : "ASC",
"hash" : "DESC"
},
"compoundIndexMap" : [
"{\"hash\":1,\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"loginWasSuccessful\":1,\"loginDate\":-1}"
]}
Sau đó, chúng ta có thể tạo ra các bộ sưu tập và các chỉ số
private List<IndexDefinition> createIndexList(Map<String, Sort.Direction> normalIndexMap) {
Set<String> keySet = normalIndexMap.keySet();
List<IndexDefinition> indexArray = new ArrayList<>();
for (String key : keySet) {
indexArray.add(new Index(key, normalIndexMap.get(key)).background());
}
return indexArray;
}
Kết quả là danh sách chỉ số duy nhất có thể được sử dụng để tạo chỉ mục trường duy nhất
private void createIndexes(List<IndexDefinition> indexDefinitionList, MongoOperations mongoOperations, String collectionName) {
indexDefinitionList.forEach(indexDefinition -> mongoOperations.indexOps(collectionName).ensureIndex(indexDefinition));
}
Chỉ số hợp chất hoặc chỉ số multifield là khôn lanh, Chúng ta cần tạo DBObjects để xác định chỉ số
private List<DBObject> createCompountIndexList(List<String> compoundIndexStringMapList) {//NOSONAR IS IN USE
if (compoundIndexStringMapList == null || compoundIndexStringMapList.isEmpty())
return new ArrayList<>(); //NOSONAR
ObjectMapper mapper = new ObjectMapper();
List<DBObject> basicDBObjectList = new ArrayList<>();
for (String stringMapList : compoundIndexStringMapList) {
LinkedHashMap<String, Integer> parsedMap;
try {
parsedMap = mapper.readValue(stringMapList, new TypeReference<Map<String, Integer>>() {
});
BasicDBObjectBuilder dbObjectBuilder = BasicDBObjectBuilder.start();
Iterator it = parsedMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry indexElement = (Map.Entry) it.next();
dbObjectBuilder.add((String) indexElement.getKey(), indexElement.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
basicDBObjectList.add(dbObjectBuilder.get());
} catch (IOException e) {//NOSONAR I´m logging it, we can not do anything more here cause it is part of the db configuration settings that need to be correct
Logger.getLogger(JsonCollectionCreation.class).error("The compound index definition " + stringMapList + " is not correct it can not be mapped to LinkHashMap");
}
}
return basicDBObjectList;
}
Và kết quả có thể được sử dụng để tạo ra các chỉ số trong bộ sưu tập
private void createCompoundIndex(List<DBObject> compoundIndexList, MongoOperations mongoOperations, String collectionName) {
if (compoundIndexList.isEmpty()) return;
for (DBObject compoundIndexDefinition : compoundIndexList) {
mongoOperations.indexOps(collectionName).ensureIndex(new CompoundIndexDefinition(compoundIndexDefinition).background());
}
}
câu hỏi tương tự http://stackoverflow.com/questions/23942469/create-index-in-correct-collection – Alex