2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.searchdbabstraction.util;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.aai.sa.rest.DocumentFieldSchema;
27 import org.onap.aai.sa.rest.DocumentSchema;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.concurrent.atomic.AtomicBoolean;
33 public class DocumentSchemaUtil {
35 public static String generateDocumentMappings(String documentSchema)
36 throws JsonParseException, JsonMappingException, IOException {
38 // Unmarshal the json content into a document schema object.
39 ObjectMapper mapper = new ObjectMapper();
40 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
42 return generateDocumentMappings(schema);
45 public static String generateDocumentMappings(DocumentSchema schema) {
47 // Now, generate the Elastic Search mapping json and return it.
48 StringBuilder sb = new StringBuilder();
50 sb.append("\"properties\": {");
52 generateFieldMappings(schema.getFields(), sb);
61 private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
63 AtomicBoolean firstField = new AtomicBoolean(true);
65 for (DocumentFieldSchema field : fields) {
67 // If this isn't the first field in the list, prepend it with a ','
68 if (!firstField.compareAndSet(true, false)) {
72 // Now, append the translated field contents.
73 generateFieldMapping(field, sb);
77 private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
79 sb.append("\"").append(fieldSchema.getName()).append("\": {");
81 // The field type is mandatory.
82 sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
84 // For date type fields we may optionally supply a format specifier.
85 if (fieldSchema.getDataType().equals("date")) {
86 if (fieldSchema.getFormat() != null) {
87 sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
91 // If the index field was specified, then append it.
92 if (fieldSchema.getSearchable() != null) {
93 sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
94 ? "analyzed" : "not_analyzed").append("\"");
97 // If a search analyzer was specified, then append it.
98 if (fieldSchema.getSearchAnalyzer() != null) {
99 sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
102 // If an indexing analyzer was specified, then append it.
103 if (fieldSchema.getIndexAnalyzer() != null) {
104 sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
108 if (fieldSchema.getDataType().equals("nested")) {
110 sb.append(", \"properties\": {");
111 generateFieldMappings(fieldSchema.getSubFields(), sb);