use of com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef in project nrtsearch by Yelp.
the class RegisterFieldsHandler method handle.
/* Sets the FieldDef for every field specified in FieldDefRequest and saves it in IndexState.fields member
* returns the String representation of the same */
@Override
public FieldDefResponse handle(final IndexState indexState, FieldDefRequest fieldDefRequest) throws RegisterFieldsException {
assert indexState != null;
final Map<String, FieldDef> pendingFieldDefs = new HashMap<>();
final Map<String, String> saveStates = new HashMap<>();
Set<String> seen = new HashSet<>();
// request (or, from the saved json):
for (int pass = 0; pass < 2; pass++) {
List<Field> fields = fieldDefRequest.getFieldList();
for (Field currentField : fields) {
String fieldName = currentField.getName();
if (pass == 1 && seen.contains(fieldName)) {
continue;
}
;
if (pass == 0 && FieldType.VIRTUAL.equals(currentField.getType())) {
// request
continue;
}
if (!IndexState.isSimpleName(fieldName)) {
throw new RegisterFieldsException("invalid field name \"" + fieldName + "\": must be [a-zA-Z_][a-zA-Z0-9]*");
}
if (fieldName.endsWith("_boost")) {
throw new RegisterFieldsException("invalid field name \"" + fieldName + "\": field names cannot end with _boost");
}
if (seen.contains(fieldName)) {
throw new RegisterFieldsException("field \"" + fieldName + "\" appears at least twice in this request");
}
seen.add(fieldName);
try {
// convert Proto object to Json String
String currentFieldAsJsonString = JsonFormat.printer().print(currentField);
saveStates.put(fieldName, currentFieldAsJsonString);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
FieldDef fieldDef = parseOneFieldType(indexState, pendingFieldDefs, fieldName, currentField);
if (fieldDef instanceof IdFieldDef) {
verifyOnlyOneIdFieldExists(indexState, pendingFieldDefs, fieldDef);
}
pendingFieldDefs.put(fieldName, fieldDef);
if (fieldDef instanceof IndexableFieldDef) {
addChildFields((IndexableFieldDef) fieldDef, pendingFieldDefs);
}
}
}
// add FieldDef and its corresponding JsonObject to states variable in IndexState
for (Map.Entry<String, FieldDef> ent : pendingFieldDefs.entrySet()) {
if (IndexState.isChildName(ent.getKey())) {
// child field will be present in top level field json
indexState.addField(ent.getValue(), null);
} else {
JsonObject fieldAsJsonObject = jsonParser.parse(saveStates.get(ent.getKey())).getAsJsonObject();
indexState.addField(ent.getValue(), fieldAsJsonObject);
}
}
String response = indexState.getAllFieldsJSON();
FieldDefResponse reply = FieldDefResponse.newBuilder().setResponse(response).build();
return reply;
}
Aggregations