Search in sources :

Example 1 with IdFieldDef

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;
}
Also used : IdFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef) HashMap(java.util.HashMap) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonObject(com.google.gson.JsonObject) IdFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) Field(com.yelp.nrtsearch.server.grpc.Field) FieldDefResponse(com.yelp.nrtsearch.server.grpc.FieldDefResponse) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

JsonObject (com.google.gson.JsonObject)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Field (com.yelp.nrtsearch.server.grpc.Field)1 FieldDefResponse (com.yelp.nrtsearch.server.grpc.FieldDefResponse)1 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)1 IdFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef)1 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)1 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1