2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.util;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.openecomp.sa.rest.DocumentFieldSchema;
31 import org.openecomp.sa.rest.DocumentSchema;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
37 public class DocumentSchemaUtil {
39 public static String generateDocumentMappings(String documentSchema)
40 throws JsonParseException, JsonMappingException, IOException {
42 // Unmarshal the json content into a document schema object.
43 ObjectMapper mapper = new ObjectMapper();
44 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
46 return generateDocumentMappings(schema);
49 public static String generateDocumentMappings(DocumentSchema schema) {
51 // Now, generate the Elastic Search mapping json and return it.
52 StringBuilder sb = new StringBuilder();
54 sb.append("\"properties\": {");
56 generateFieldMappings(schema.getFields(), sb);
65 private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
67 AtomicBoolean firstField = new AtomicBoolean(true);
69 for (DocumentFieldSchema field : fields) {
71 // If this isn't the first field in the list, prepend it with a ','
72 if (!firstField.compareAndSet(true, false)) {
76 // Now, append the translated field contents.
77 generateFieldMapping(field, sb);
81 private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
83 sb.append("\"").append(fieldSchema.getName()).append("\": {");
85 // The field type is mandatory.
86 sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
88 // For date type fields we may optionally supply a format specifier.
89 if (fieldSchema.getDataType().equals("date")) {
90 if (fieldSchema.getFormat() != null) {
91 sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
95 // If the index field was specified, then append it.
96 if (fieldSchema.getSearchable() != null) {
97 sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
98 ? "analyzed" : "not_analyzed").append("\"");
101 // If a search analyzer was specified, then append it.
102 if (fieldSchema.getSearchAnalyzer() != null) {
103 sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
106 // If an indexing analyzer was specified, then append it.
107 if (fieldSchema.getIndexAnalyzer() != null) {
108 sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
112 if (fieldSchema.getDataType().equals("nested")) {
114 sb.append(", \"properties\": {");
115 generateFieldMappings(fieldSchema.getSubFields(), sb);