2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.searchdbabstraction.util;
25 import com.fasterxml.jackson.core.JsonParseException;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.openecomp.sa.rest.DocumentFieldSchema;
29 import org.openecomp.sa.rest.DocumentSchema;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.concurrent.atomic.AtomicBoolean;
35 public class DocumentSchemaUtil {
37 public static String generateDocumentMappings(String documentSchema)
38 throws JsonParseException, JsonMappingException, IOException {
40 // Unmarshal the json content into a document schema object.
41 ObjectMapper mapper = new ObjectMapper();
42 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
44 return generateDocumentMappings(schema);
47 public static String generateDocumentMappings(DocumentSchema schema) {
49 // Now, generate the Elastic Search mapping json and return it.
50 StringBuilder sb = new StringBuilder();
52 sb.append("\"properties\": {");
54 generateFieldMappings(schema.getFields(), sb);
63 private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
65 AtomicBoolean firstField = new AtomicBoolean(true);
67 for (DocumentFieldSchema field : fields) {
69 // If this isn't the first field in the list, prepend it with a ','
70 if (!firstField.compareAndSet(true, false)) {
74 // Now, append the translated field contents.
75 generateFieldMapping(field, sb);
79 private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
81 sb.append("\"").append(fieldSchema.getName()).append("\": {");
83 // The field type is mandatory.
84 sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
86 // For date type fields we may optionally supply a format specifier.
87 if (fieldSchema.getDataType().equals("date")) {
88 if (fieldSchema.getFormat() != null) {
89 sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
93 // If the index field was specified, then append it.
94 if (fieldSchema.getSearchable() != null) {
95 sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
96 ? "analyzed" : "not_analyzed").append("\"");
99 // If a search analyzer was specified, then append it.
100 if (fieldSchema.getSearchAnalyzer() != null) {
101 sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
104 // If an indexing analyzer was specified, then append it.
105 if (fieldSchema.getIndexAnalyzer() != null) {
106 sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
110 if (fieldSchema.getDataType().equals("nested")) {
112 sb.append(", \"properties\": {");
113 generateFieldMappings(fieldSchema.getSubFields(), sb);