Exemplo n.º 1
0
  @Test
  public void testDeleteFieldAccuracy() throws Exception {
    String fieldName = "fieldToBeDeleted";
    Map<String, Object> fieldAttributesRequest = new LinkedHashMap<>();
    fieldAttributesRequest.put("name", fieldName);
    fieldAttributesRequest.put("type", "string");
    SchemaRequest.AddField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddField(fieldAttributesRequest);
    SchemaResponse.UpdateResponse addFieldResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    SchemaRequest.Field fieldSchemaRequest = new SchemaRequest.Field(fieldName);
    SchemaResponse.FieldResponse initialFieldResponse = fieldSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(initialFieldResponse);
    Map<String, Object> fieldAttributesResponse = initialFieldResponse.getField();
    assertThat(fieldName, is(equalTo(fieldAttributesResponse.get("name"))));

    SchemaRequest.DeleteField deleteFieldRequest = new SchemaRequest.DeleteField(fieldName);
    SchemaResponse.UpdateResponse deleteFieldResponse = deleteFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(deleteFieldResponse);

    try {
      fieldSchemaRequest.process(getSolrClient());
      fail(
          String.format(
              Locale.ROOT,
              "after removal, the field %s shouldn't be anymore available over Schema API",
              fieldName));
    } catch (SolrException e) {
      // success
    }
  }
Exemplo n.º 2
0
 private static void createStoredStringField(String fieldName, SolrClient solrClient)
     throws Exception {
   Map<String, Object> fieldAttributes = new LinkedHashMap<>();
   fieldAttributes.put("name", fieldName);
   fieldAttributes.put("type", "string");
   fieldAttributes.put("stored", true);
   SchemaRequest.AddField addFieldRequest = new SchemaRequest.AddField(fieldAttributes);
   addFieldRequest.process(solrClient);
 }
Exemplo n.º 3
0
  @Test
  public void addFieldShouldntBeCalledTwiceWithTheSameName() throws Exception {
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", "failureField");
    fieldAttributes.put("type", "string");
    SchemaRequest.AddField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldFirstResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldFirstResponse);

    SchemaResponse.UpdateResponse addFieldSecondResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertEquals(0, addFieldSecondResponse.getStatus());
    assertNotNull(addFieldSecondResponse.getResponse().get("errors"));
  }
Exemplo n.º 4
0
  @Test
  public void testAddFieldAccuracy() throws Exception {
    SchemaRequest.Fields fieldsSchemaRequest = new SchemaRequest.Fields();
    SchemaResponse.FieldsResponse initialFieldsResponse =
        fieldsSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(initialFieldsResponse);
    List<Map<String, Object>> initialFields = initialFieldsResponse.getFields();

    String fieldName = "accuracyField";
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", fieldName);
    fieldAttributes.put("type", "string");
    fieldAttributes.put("stored", false);
    fieldAttributes.put("indexed", true);
    fieldAttributes.put("default", "accuracy");
    fieldAttributes.put("required", true);
    SchemaRequest.AddField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    SchemaResponse.FieldsResponse currentFieldsResponse =
        fieldsSchemaRequest.process(getSolrClient());
    assertEquals(0, currentFieldsResponse.getStatus());
    List<Map<String, Object>> currentFields = currentFieldsResponse.getFields();
    assertEquals(initialFields.size() + 1, currentFields.size());

    SchemaRequest.Field fieldSchemaRequest = new SchemaRequest.Field(fieldName);
    SchemaResponse.FieldResponse newFieldResponse = fieldSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(newFieldResponse);
    Map<String, Object> newFieldAttributes = newFieldResponse.getField();
    assertThat(fieldName, 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"))));
    assertThat("accuracy", is(equalTo(newFieldAttributes.get("default"))));
    assertThat(true, is(equalTo(newFieldAttributes.get("required"))));
  }
Exemplo n.º 5
0
  @Test
  public void testReplaceFieldAccuracy() throws Exception {
    // Given
    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    String fieldName = "accuracyField";
    fieldAttributes.put("name", fieldName);
    fieldAttributes.put("type", "string");
    fieldAttributes.put("stored", false);
    fieldAttributes.put("indexed", true);
    fieldAttributes.put("required", true);
    SchemaRequest.AddField addFieldUpdateSchemaRequest =
        new SchemaRequest.AddField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse =
        addFieldUpdateSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(addFieldResponse);

    // When : update the field definition
    fieldAttributes.put("stored", true);
    fieldAttributes.put("indexed", false);
    SchemaRequest.ReplaceField replaceFieldRequest =
        new SchemaRequest.ReplaceField(fieldAttributes);
    SchemaResponse.UpdateResponse replaceFieldResponse =
        replaceFieldRequest.process(getSolrClient());
    assertValidSchemaResponse(replaceFieldResponse);

    // Then
    SchemaRequest.Field fieldSchemaRequest = new SchemaRequest.Field(fieldName);
    SchemaResponse.FieldResponse newFieldResponse = fieldSchemaRequest.process(getSolrClient());
    assertValidSchemaResponse(newFieldResponse);
    Map<String, Object> newFieldAttributes = newFieldResponse.getField();
    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"))));
    assertThat(true, is(equalTo(newFieldAttributes.get("required"))));
  }
Exemplo n.º 6
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"))));
  }