Java Code Examples for org.apache.solr.client.solrj.request.schema.SchemaRequest#FieldTypes

The following examples show how to use org.apache.solr.client.solrj.request.schema.SchemaRequest#FieldTypes . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MCRSolrSchemaReloader.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static void deleteFieldTypes(SolrClient solrClient)
    throws SolrServerException, IOException {
    SchemaRequest.FieldTypes fieldTypesReq = new SchemaRequest.FieldTypes();
    for (FieldTypeRepresentation fieldType : fieldTypesReq.process(solrClient).getFieldTypes()) {
        String fieldTypeName = fieldType.getAttributes().get("name").toString();
        if (!SOLR_DEFAULT_FIELDTYPES.contains(fieldTypeName)) {
            LOGGER.debug("remove SOLR FieldType " + fieldTypeName);
            SchemaRequest.DeleteFieldType delField = new SchemaRequest.DeleteFieldType(fieldTypeName);
            delField.process(solrClient);
        }
    }
}
 
Example 2
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFieldTypesAccuracy() throws Exception {
  SchemaRequest.FieldTypes fieldTypesRequest =
      new SchemaRequest.FieldTypes();
  SchemaResponse.FieldTypesResponse fieldTypesResponse = fieldTypesRequest.process(getSolrClient());
  assertValidSchemaResponse(fieldTypesResponse);
  List<FieldTypeRepresentation> fieldTypes = fieldTypesResponse.getFieldTypes();
  assertThat(fieldTypes.isEmpty(), is(false));
}
 
Example 3
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@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.WordDelimiterGraphFilterFactory");
  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"))));
}