<commit_before>package io.polyglotted.eswrapper.indexing;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.*;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableMap.of;
import static io.polyglotted.eswrapper.ElasticConstants.ALL_META;
import static io.polyglotted.eswrapper.ElasticConstants.SOURCE_META;
import static io.polyglotted.eswrapper.ElasticConstants.TYPE_META;
import static io.polyglotted.eswrapper.indexing.IndexSerializer.GSON;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@ToString(includeFieldNames = false, doNotUseGetters = true)
public final class TypeMapping {
    public final String index;
    public final String type;
    public final String parent;
    public final boolean strict;
    public final boolean storeSource;
    public final String allAnalyzer;
    public final ImmutableSet<String> sourceIncludes;
    public final ImmutableSet<FieldMapping> fieldMappings;
    public final ImmutableList<Script> scripts;
    public final ImmutableMap<String, Object> meta;

    @Override
    public boolean equals(Object o) {
        return this == o || (!(o == null || getClass() != o.getClass()) &&
           mappingJson().equals(((TypeMapping) o).mappingJson()));
    }

    @Override
    public int hashCode() {
        return 29 * mappingJson().hashCode();
    }

    public String mappingJson() {
        return GSON.toJson(this);
    }

    public static String forcedMappingJson(String type) {
        return GSON.toJson(of(type, of(SOURCE_META, of("enabled", 0), ALL_META, of("enabled", 0),
           TYPE_META, of("index", "no"), "enabled", 0)));
    }

    public static Builder typeBuilder() {
        return new Builder();
    }

    @Setter
    @Accessors(fluent = true, chain = true)
    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public static class Builder {
        private String index;
        private String type;
        private String parent;
        private boolean strict = false;
        private boolean storeSource = true;
        private String allAnalyzer = "all_analyzer";
        private final Set<String> sourceIncludes = new TreeSet<>();
        private final Set<FieldMapping> fieldMappings = new TreeSet<>();
        private final List<Script> scripts = new ArrayList<>();
        private final Map<String, Object> metaData = new LinkedHashMap<>();

        public Builder fieldMapping(FieldMapping.Builder mapping) {
            return fieldMapping(mapping.build());
        }

        public Builder fieldMapping(FieldMapping mapping) {
            fieldMappings.add(mapping);
            return this;
        }

        public Builder transform(Script.Builder script) {
            return transform(script.build());
        }

        public Builder transform(Script script) {
            scripts.add(script);
            return this;
        }

        public Builder include(String field) {
            sourceIncludes.add(field);
            return this;
        }

        public Builder metaData(String name, Object value) {
            metaData.put(name, value);
            return this;
        }

        public TypeMapping build() {
            return new TypeMapping(checkNotNull(index, "index cannot be null"), checkNotNull(type, "type cannot be null"),
               parent, strict, storeSource, allAnalyzer, ImmutableSet.copyOf(sourceIncludes),
               ImmutableSet.copyOf(fieldMappings), ImmutableList.copyOf(scripts), ImmutableMap.copyOf(metaData));
        }
    }
}<commit_msg>added fieldmappings utility<commit_after>package io.polyglotted.eswrapper.indexing;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.*;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableMap.of;
import static io.polyglotted.eswrapper.ElasticConstants.ALL_META;
import static io.polyglotted.eswrapper.ElasticConstants.SOURCE_META;
import static io.polyglotted.eswrapper.ElasticConstants.TYPE_META;
import static io.polyglotted.eswrapper.indexing.IndexSerializer.GSON;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@ToString(includeFieldNames = false, doNotUseGetters = true)
public final class TypeMapping {
    public final String index;
    public final String type;
    public final String parent;
    public final boolean strict;
    public final boolean storeSource;
    public final String allAnalyzer;
    public final ImmutableSet<String> sourceIncludes;
    public final ImmutableSet<FieldMapping> fieldMappings;
    public final ImmutableList<Script> scripts;
    public final ImmutableMap<String, Object> meta;

    @Override
    public boolean equals(Object o) {
        return this == o || (!(o == null || getClass() != o.getClass()) &&
           mappingJson().equals(((TypeMapping) o).mappingJson()));
    }

    @Override
    public int hashCode() {
        return 29 * mappingJson().hashCode();
    }

    public String mappingJson() {
        return GSON.toJson(this);
    }

    public static String forcedMappingJson(String type) {
        return GSON.toJson(of(type, of(SOURCE_META, of("enabled", 0), ALL_META, of("enabled", 0),
           TYPE_META, of("index", "no"), "enabled", 0)));
    }

    public static Builder typeBuilder() {
        return new Builder();
    }

    @Setter
    @Accessors(fluent = true, chain = true)
    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public static class Builder {
        private String index;
        private String type;
        private String parent;
        private boolean strict = false;
        private boolean storeSource = true;
        private String allAnalyzer = "all_analyzer";
        private final Set<String> sourceIncludes = new TreeSet<>();
        private final Set<FieldMapping> fieldMappings = new TreeSet<>();
        private final List<Script> scripts = new ArrayList<>();
        private final Map<String, Object> metaData = new LinkedHashMap<>();

        public Builder fieldMapping(FieldMapping.Builder mapping) {
            return fieldMapping(mapping.build());
        }

        public Builder fieldMapping(FieldMapping mapping) {
            fieldMappings.add(mapping);
            return this;
        }

        public Builder fieldMapping(Collection<FieldMapping> mappings) {
            fieldMappings.addAll(mappings);
            return this;
        }

        public Builder transform(Script.Builder script) {
            return transform(script.build());
        }

        public Builder transform(Script script) {
            scripts.add(script);
            return this;
        }

        public Builder include(String field) {
            sourceIncludes.add(field);
            return this;
        }

        public Builder metaData(String name, Object value) {
            metaData.put(name, value);
            return this;
        }

        public TypeMapping build() {
            return new TypeMapping(checkNotNull(index, "index cannot be null"), checkNotNull(type, "type cannot be null"),
               parent, strict, storeSource, allAnalyzer, ImmutableSet.copyOf(sourceIncludes),
               ImmutableSet.copyOf(fieldMappings), ImmutableList.copyOf(scripts), ImmutableMap.copyOf(metaData));
        }
    }
}
{'fraction_non_alphanumeric': 0.09598420013166557, 'fraction_numerical': 0.0013166556945358788, 'mean_word_length': 3.0447284345047922, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4263814', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1962, 'n_words': 527}