1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
@Component public class ESIndexMappingService {
@Autowired private ESClient esClient;
public void createUserIndexMapping() { Map<String, Object> mapping = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
Map<String, Object> idMapping = new HashMap<>(); idMapping.put("type", "keyword"); properties.put("id", idMapping);
Map<String, Object> nameMapping = new HashMap<>(); nameMapping.put("type", "text"); nameMapping.put("analyzer", "ik_max_word"); nameMapping.put("search_analyzer", "ik_smart"); Map<String, Object> nameFields = new HashMap<>(); Map<String, Object> keywordField = new HashMap<>(); keywordField.put("type", "keyword"); nameFields.put("keyword", keywordField); nameMapping.put("fields", nameFields); properties.put("name", nameMapping);
Map<String, Object> emailMapping = new HashMap<>(); emailMapping.put("type", "keyword"); properties.put("email", emailMapping);
Map<String, Object> createdAtMapping = new HashMap<>(); createdAtMapping.put("type", "date"); createdAtMapping.put("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"); properties.put("createdAt", createdAtMapping);
Map<String, Object> updatedAtMapping = new HashMap<>(); updatedAtMapping.put("type", "date"); updatedAtMapping.put("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"); properties.put("updatedAt", updatedAtMapping);
mapping.put("properties", properties);
CreateIndexRequest request = new CreateIndexRequest("user") .mapping(mapping) .settings(createIndexSettings());
try { CreateIndexResponse response = esClient.indices().create(request); log.info("用户索引创建成功: {}", response.isAcknowledged()); } catch (Exception e) { log.error("用户索引创建失败", e); throw new IndexException("用户索引创建失败", e); } }
public void createOrderIndexMapping() { Map<String, Object> mapping = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
Map<String, Object> idMapping = new HashMap<>(); idMapping.put("type", "keyword"); properties.put("id", idMapping);
Map<String, Object> orderNumberMapping = new HashMap<>(); orderNumberMapping.put("type", "keyword"); properties.put("orderNumber", orderNumberMapping);
Map<String, Object> userIdMapping = new HashMap<>(); userIdMapping.put("type", "keyword"); properties.put("userId", userIdMapping);
Map<String, Object> amountMapping = new HashMap<>(); amountMapping.put("type", "double"); properties.put("amount", amountMapping);
Map<String, Object> statusMapping = new HashMap<>(); statusMapping.put("type", "integer"); properties.put("status", statusMapping);
Map<String, Object> createdAtMapping = new HashMap<>(); createdAtMapping.put("type", "date"); createdAtMapping.put("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"); properties.put("createdAt", createdAtMapping);
mapping.put("properties", properties);
CreateIndexRequest request = new CreateIndexRequest("order") .mapping(mapping) .settings(createIndexSettings());
try { CreateIndexResponse response = esClient.indices().create(request); log.info("订单索引创建成功: {}", response.isAcknowledged()); } catch (Exception e) { log.error("订单索引创建失败", e); throw new IndexException("订单索引创建失败", e); } }
private Map<String, Object> createIndexSettings() { Map<String, Object> settings = new HashMap<>();
Map<String, Object> indexSettings = new HashMap<>(); indexSettings.put("number_of_shards", 3); indexSettings.put("number_of_replicas", 1);
indexSettings.put("refresh_interval", "30s");
Map<String, Object> analysis = new HashMap<>(); Map<String, Object> analyzer = new HashMap<>();
Map<String, Object> ikMaxWord = new HashMap<>(); ikMaxWord.put("type", "custom"); ikMaxWord.put("tokenizer", "ik_max_word"); analyzer.put("ik_max_word", ikMaxWord);
Map<String, Object> ikSmart = new HashMap<>(); ikSmart.put("type", "custom"); ikSmart.put("tokenizer", "ik_smart"); analyzer.put("ik_smart", ikSmart);
analysis.put("analyzer", analyzer); indexSettings.put("analysis", analysis);
settings.put("index", indexSettings);
return settings; } }
|