Ejemplo n.º 1
0
  @Test
  public void addDynamicFieldShouldntBeCalledTwiceWithTheSameName() throws Exception {
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", "*_failure");
    fieldAttributes.put("type", "string");
    SchemaRequest.AddDynamicField addDFieldUpdateSchemaRequest =
        new SchemaRequest.AddDynamicField(fieldAttributes);
    SolrClient client = getSolrClient();
    SchemaResponse.UpdateResponse addDFieldFirstResponse =
        addDFieldUpdateSchemaRequest.process(client);
    assertValidSchemaResponse(addDFieldFirstResponse);

    SchemaResponse.UpdateResponse addDFieldSecondResponse =
        addDFieldUpdateSchemaRequest.process(getSolrClient());
    assertEquals(0, addDFieldSecondResponse.getStatus());
    assertNotNull(addDFieldSecondResponse.getResponse().get("errors"));
  }
Ejemplo n.º 2
0
  @Test
  public void testReplaceDynamicFieldAccuracy() throws Exception {
    // Given
    String fieldName = "*_replace";
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", fieldName);
    fieldAttributes.put("type", "string");
    fieldAttributes.put("stored", false);
    fieldAttributes.put("indexed", true);
    SchemaRequest.AddDynamicField addDFieldUpdateSchemaRequest =
        new SchemaRequest.AddDynamicField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse =
        addDFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    // When : update the field definition
    Map<String, Object> replaceFieldAttributes = new LinkedHashMap<>(fieldAttributes);
    replaceFieldAttributes.put("stored", true);
    replaceFieldAttributes.put("indexed", false);
    SchemaRequest.ReplaceDynamicField replaceFieldRequest =
        new SchemaRequest.ReplaceDynamicField(replaceFieldAttributes);
    SchemaResponse.UpdateResponse replaceFieldResponse =
        replaceFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(replaceFieldResponse);

    // Then
    SchemaRequest.DynamicField dynamicFieldSchemaRequest =
        new SchemaRequest.DynamicField(fieldName);
    SchemaResponse.DynamicFieldResponse newFieldResponse =
        dynamicFieldSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(newFieldResponse);
    Map<String, Object> newFieldAttributes = newFieldResponse.getDynamicField();
    assertThat(fieldName, is(equalTo(newFieldAttributes.get("name"))));
    assertThat("string", is(equalTo(newFieldAttributes.get("type"))));
    assertThat(true, is(equalTo(newFieldAttributes.get("stored"))));
    assertThat(false, is(equalTo(newFieldAttributes.get("indexed"))));
  }
Ejemplo n.º 3
0
  @Test
  public void testDeleteDynamicFieldAccuracy() throws Exception {
    String dynamicFieldName = "*_del";
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", dynamicFieldName);
    fieldAttributes.put("type", "string");
    SchemaRequest.AddDynamicField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddDynamicField(fieldAttributes);
    SchemaResponse.UpdateResponse addDynamicFieldResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addDynamicFieldResponse);

    SchemaRequest.DynamicField dynamicFieldSchemaRequest =
        new SchemaRequest.DynamicField(dynamicFieldName);
    SchemaResponse.DynamicFieldResponse initialDFieldResponse =
        dynamicFieldSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(initialDFieldResponse);
    Map<String, Object> fieldAttributesResponse = initialDFieldResponse.getDynamicField();
    assertThat(dynamicFieldName, is(equalTo(fieldAttributesResponse.get("name"))));

    SchemaRequest.DeleteDynamicField deleteFieldRequest =
        new SchemaRequest.DeleteDynamicField(dynamicFieldName);
    SchemaResponse.UpdateResponse deleteDynamicFieldResponse =
        deleteFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(deleteDynamicFieldResponse);

    try {
      dynamicFieldSchemaRequest.process(getSolrClient());
      fail(
          String.format(
              Locale.ROOT,
              "after removal, the dynamic field %s shouldn't be anymore available over Schema API",
              dynamicFieldName));
    } catch (SolrException e) {
      // success
    }
  }
Ejemplo n.º 4
0
  @Test
  public void testAddDynamicFieldAccuracy() throws Exception {
    SchemaRequest.DynamicFields dynamicFieldsSchemaRequest = new SchemaRequest.DynamicFields();
    SchemaResponse.DynamicFieldsResponse initialDFieldsResponse =
        dynamicFieldsSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(initialDFieldsResponse);
    List<Map<String, Object>> initialDFields = initialDFieldsResponse.getDynamicFields();

    String dFieldName = "*_acc";
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", dFieldName);
    fieldAttributes.put("type", "string");
    fieldAttributes.put("stored", false);
    fieldAttributes.put("indexed", true);
    // Dynamic fields cannot be required or have a default value
    SchemaRequest.AddDynamicField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddDynamicField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    SchemaResponse.DynamicFieldsResponse currentDFieldsResponse =
        dynamicFieldsSchemaRequest.process(getSolrClient());
    assertEquals(0, currentDFieldsResponse.getStatus());
    List<Map<String, Object>> currentFields = currentDFieldsResponse.getDynamicFields();
    assertEquals(initialDFields.size() + 1, currentFields.size());

    SchemaRequest.DynamicField dFieldRequest = new SchemaRequest.DynamicField(dFieldName);
    SchemaResponse.DynamicFieldResponse newFieldResponse = dFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(newFieldResponse);
    Map<String, Object> newFieldAttributes = newFieldResponse.getDynamicField();
    assertThat(dFieldName, is(equalTo(newFieldAttributes.get("name"))));
    assertThat("string", is(equalTo(newFieldAttributes.get("type"))));
    assertThat(false, is(equalTo(newFieldAttributes.get("stored"))));
    assertThat(true, is(equalTo(newFieldAttributes.get("indexed"))));
  }
Ejemplo n.º 5
0
  @Test
  public void testAddFieldTypeAccuracy() throws Exception {
    SchemaRequest.FieldTypes fieldTypesRequest = new SchemaRequest.FieldTypes();
    SchemaResponse.FieldTypesResponse initialFieldTypesResponse =
        fieldTypesRequest.process(getSolrClient());
    assertValidSchemaResponse(initialFieldTypesResponse);
    List<FieldTypeRepresentation> initialFieldTypes = initialFieldTypesResponse.getFieldTypes();

    FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
    Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
    String fieldTypeName = "accuracyTextField";
    fieldTypeAttributes.put("name", fieldTypeName);
    fieldTypeAttributes.put("class", "solr.TextField");
    fieldTypeAttributes.put("positionIncrementGap", "100");
    fieldTypeDefinition.setAttributes(fieldTypeAttributes);

    AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
    Map<String, Object> charFilterAttributes = new LinkedHashMap<>();
    charFilterAttributes.put("class", "solr.PatternReplaceCharFilterFactory");
    charFilterAttributes.put("replacement", "$1$1");
    charFilterAttributes.put("pattern", "([a-zA-Z])\\\\1+");
    analyzerDefinition.setCharFilters(Collections.singletonList(charFilterAttributes));
    Map<String, Object> tokenizerAttributes = new LinkedHashMap<>();
    tokenizerAttributes.put("class", "solr.WhitespaceTokenizerFactory");
    analyzerDefinition.setTokenizer(tokenizerAttributes);
    Map<String, Object> filterAttributes = new LinkedHashMap<>();
    filterAttributes.put("class", "solr.WordDelimiterFilterFactory");
    filterAttributes.put("preserveOriginal", "0");
    analyzerDefinition.setFilters(Collections.singletonList(filterAttributes));
    fieldTypeDefinition.setAnalyzer(analyzerDefinition);

    SchemaRequest.AddFieldType addFieldTypeRequest =
        new SchemaRequest.AddFieldType(fieldTypeDefinition);
    SchemaResponse.UpdateResponse addFieldTypeResponse =
        addFieldTypeRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldTypeResponse);

    SchemaResponse.FieldTypesResponse currentFieldTypesResponse =
        fieldTypesRequest.process(getSolrClient());
    assertEquals(0, currentFieldTypesResponse.getStatus());
    List<FieldTypeRepresentation> currentFieldTypes = currentFieldTypesResponse.getFieldTypes();
    assertEquals(initialFieldTypes.size() + 1, currentFieldTypes.size());

    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    String fieldName = "accuracyField";
    fieldAttributes.put("name", fieldName);
    fieldAttributes.put("type", fieldTypeName);
    SchemaRequest.AddField addFieldRequest = new SchemaRequest.AddField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse = addFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    Map<String, Object> dynamicFieldAttributes = new LinkedHashMap<>();
    String dynamicFieldName = "*_accuracy";
    dynamicFieldAttributes.put("name", dynamicFieldName);
    dynamicFieldAttributes.put("type", fieldTypeName);
    SchemaRequest.AddDynamicField addDynamicFieldRequest =
        new SchemaRequest.AddDynamicField(dynamicFieldAttributes);
    SchemaResponse.UpdateResponse addDynamicFieldResponse =
        addDynamicFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(addDynamicFieldResponse);

    SchemaRequest.FieldType fieldTypeRequest = new SchemaRequest.FieldType(fieldTypeName);
    SchemaResponse.FieldTypeResponse newFieldTypeResponse =
        fieldTypeRequest.process(getSolrClient());
    assertValidSchemaResponse(newFieldTypeResponse);
    FieldTypeRepresentation newFieldTypeRepresentation = newFieldTypeResponse.getFieldType();
    assertThat(fieldTypeName, is(equalTo(newFieldTypeRepresentation.getAttributes().get("name"))));
    assertThat(
        "solr.TextField", is(equalTo(newFieldTypeRepresentation.getAttributes().get("class"))));
    assertThat(
        analyzerDefinition.getTokenizer().get("class"),
        is(equalTo(newFieldTypeRepresentation.getAnalyzer().getTokenizer().get("class"))));
  }