3535import org .apache .logging .log4j .LogManager ;
3636import org .apache .logging .log4j .Logger ;
3737import org .apache .lucene .util .CollectionUtil ;
38+ import org .apache .lucene .util .RamUsageEstimator ;
3839import org .opensearch .action .AliasesRequest ;
3940import org .opensearch .cluster .ClusterState ;
4041import org .opensearch .cluster .ClusterState .FeatureAware ;
@@ -820,18 +821,18 @@ public Map<String, IndexMetadata> indices() {
820821 }
821822
822823 /**
823- * Estimates the serialized size of the indices metadata in bytes.
824+ * Estimates the serialized size of the indices mappings in bytes.
824825 * This is useful for circuit breaker calculations.
825826 *
826827 * @param indexPatterns array of index names/patterns to estimate, empty array means all indices
827828 * @return estimated size in bytes
828829 */
829- public long estimateIndicesSize (String [] indexPatterns ) {
830+ public long estimateIndicesMappingsSize (String [] indexPatterns ) {
830831 long size = 0 ;
831832 if (indexPatterns .length == 0 ) {
832833 // Estimate all indices
833834 for (IndexMetadata indexMetadata : indices .values ()) {
834- size += estimateIndexSize (indexMetadata );
835+ size += estimateIndexMappingsSize (indexMetadata );
835836 }
836837 } else {
837838 // Estimate matching indices
@@ -841,20 +842,20 @@ public long estimateIndicesSize(String[] indexPatterns) {
841842 // _all pattern matches everything
842843 for (IndexMetadata indexMetadata : indices .values ()) {
843844 if (matchedIndices .add (indexMetadata .getIndex ().getName ())) {
844- size += estimateIndexSize (indexMetadata );
845+ size += estimateIndexMappingsSize (indexMetadata );
845846 }
846847 }
847848 } else {
848849 // Check for exact match first, then wildcard
849850 IndexMetadata exactMatch = indices .get (pattern );
850851 if (exactMatch != null && matchedIndices .add (pattern )) {
851- size += estimateIndexSize (exactMatch );
852+ size += estimateIndexMappingsSize (exactMatch );
852853 } else {
853854 // Wildcard matching
854855 for (IndexMetadata indexMetadata : indices .values ()) {
855856 String indexName = indexMetadata .getIndex ().getName ();
856857 if (Regex .simpleMatch (pattern , indexName ) && matchedIndices .add (indexName )) {
857- size += estimateIndexSize (indexMetadata );
858+ size += estimateIndexMappingsSize (indexMetadata );
858859 }
859860 }
860861 }
@@ -864,21 +865,16 @@ public long estimateIndicesSize(String[] indexPatterns) {
864865 return size ;
865866 }
866867
867- private long estimateIndexSize (IndexMetadata indexMetadata ) {
868- long size = 2048 ; // ~2KB for basic index metadata
868+ private long estimateIndexMappingsSize (IndexMetadata indexMetadata ) {
869+ long size = 0L ;
869870
870- // Mapping size estimate
871+ // 1) Mapping: count the actual compressed bytes (what GET _mapping typically serializes from)
871872 if (indexMetadata .mapping () != null ) {
872- size += indexMetadata .mapping ().source ().compressed ().length ;
873+ final byte [] compressed = indexMetadata .mapping ().source ().compressed ();
874+ size += RamUsageEstimator .sizeOf (compressed );
873875 }
874876
875- // Settings size estimate
876- size += indexMetadata .getSettings ().size () * 64 ; // ~64 bytes per setting
877-
878- // Aliases size estimate
879- size += indexMetadata .getAliases ().size () * 256 ; // ~256 bytes per alias
880-
881- return size ;
877+ return RamUsageEstimator .alignObjectSize (size );
882878 }
883879
884880 public Map <String , IndexMetadata > getIndices () {
0 commit comments