SchemaBuilder { public Schema build() { Map<String, Mapper> mappers = new LinkedHashMap<>(mapperBuilders.size()); for (Map.Entry<String, MapperBuilder<?, ?>> entry : mapperBuilders.entrySet()) { String name = entry.getKey(); MapperBuilder<?, ?> builder = entry.getValue(); Mapper mapper = builder.build(name); mappers.put(name, mapper); } Map<String, Analyzer> analyzers = new LinkedHashMap<>(); for (Map.Entry<String, AnalyzerBuilder> entry : analyzerBuilders.entrySet()) { String name = entry.getKey(); Analyzer analyzer = entry.getValue().analyzer(); analyzers.put(name, analyzer); } Analyzer defaultAnalyzer; if (defaultAnalyzerName == null) { defaultAnalyzer = StandardAnalyzers.DEFAULT.get(); } else { defaultAnalyzer = analyzers.get(defaultAnalyzerName); if (defaultAnalyzer == null) { defaultAnalyzer = StandardAnalyzers.get(defaultAnalyzerName); if (defaultAnalyzer == null) { try { defaultAnalyzer = (new ClasspathAnalyzerBuilder(defaultAnalyzerName)).analyzer(); } catch (Exception e) { throw new IndexException(e, "Not found analyzer: '{}'", defaultAnalyzerName); } } analyzers.put(defaultAnalyzerName, defaultAnalyzer); } } return new Schema(defaultAnalyzer, mappers, analyzers); } @JsonCreator  SchemaBuilder(@JsonProperty("default_analyzer") String defaultAnalyzerName,
                  @JsonProperty("analyzers") Map<String, AnalyzerBuilder> analyzerBuilders,
                  @JsonProperty("fields") Map<String, MapperBuilder<?, ?>> mapperBuilders); SchemaBuilder defaultAnalyzer(String name); SchemaBuilder analyzer(String name, AnalyzerBuilder analyzer); SchemaBuilder mapper(String field, MapperBuilder<?, ?> mapper); Schema build(); String toJson(); static SchemaBuilder fromJson(String json);  }
@Test public void testBuild() throws Exception { Schema schema = schema().defaultAnalyzer("custom") .analyzer("custom", classpathAnalyzer("org.apache.lucene.analysis.en.EnglishAnalyzer")) .analyzer("snowball", snowballAnalyzer("English", "the,at")) .mapper("blob", blobMapper()) .mapper("bool", booleanMapper()) .mapper("date", dateMapper()) .mapper("double", doubleMapper()) .mapper("float", floatMapper()) .mapper("inet", inetMapper()) .mapper("string", stringMapper()) .mapper("text", textMapper().analyzer("snowball")) .mapper("uuid", uuidMapper()) .build(); assertEquals("Failed schema building", EnglishAnalyzer.class, schema.defaultAnalyzer.getClass()); assertEquals("Failed schema building", BlobMapper.class, schema.mapper("blob").getClass()); assertEquals("Failed schema building", BooleanMapper.class, schema.mapper("bool").getClass()); assertEquals("Failed schema building", DateMapper.class, schema.mapper("date").getClass()); assertEquals("Failed schema building", InetMapper.class, schema.mapper("inet").getClass()); assertEquals("Failed schema building", StringMapper.class, schema.mapper("string").getClass()); assertEquals("Failed schema building", TextMapper.class, schema.mapper("text").getClass()); assertEquals("Failed schema building", SnowballAnalyzer.class, schema.analyzer("text").getClass()); assertEquals("Failed schema building", UUIDMapper.class, schema.mapper("uuid").getClass()); }