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

The following examples show how to use org.apache.solr.client.solrj.request.schema.SchemaRequest#FieldType . 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: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFieldTypeAccuracy() throws Exception {
  String fieldType = "string";
  SchemaRequest.FieldType fieldTypeSchemaRequest =
      new SchemaRequest.FieldType(fieldType);
  SchemaResponse.FieldTypeResponse fieldTypeResponse = fieldTypeSchemaRequest.process(getSolrClient());
  assertValidSchemaResponse(fieldTypeResponse);
  FieldTypeRepresentation fieldTypeDefinition = fieldTypeResponse.getFieldType();
  assertThat(fieldType, is(equalTo(fieldTypeDefinition.getAttributes().get("name"))));
  assertThat("solr.StrField", is(equalTo(fieldTypeDefinition.getAttributes().get("class"))));
}
 
Example 2
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void addFieldTypeWithSimilarityAccuracy() throws Exception {
  FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
  Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
  String fieldTypeName = "fullClassNames";
  fieldTypeAttributes.put("name", fieldTypeName);
  fieldTypeAttributes.put("class", "org.apache.solr.schema.TextField");
  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);
  fieldTypeDefinition.setAnalyzer(analyzerDefinition);
  Map<String, Object> similarityAttributes = new LinkedHashMap<>();
  similarityAttributes.put("class", "org.apache.lucene.misc.SweetSpotSimilarity");
  fieldTypeDefinition.setSimilarity(similarityAttributes);

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

  // similarity is not shown by default for the fieldType
  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(similarityAttributes.get("class"), is(equalTo(newFieldTypeRepresentation.getSimilarity().get("class"))));
}
 
Example 3
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void addFieldTypeWithAnalyzerClassAccuracy() throws Exception {
  Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
  String fieldTypeName = "nameText";
  fieldTypeAttributes.put("name", fieldTypeName);
  fieldTypeAttributes.put("class", "solr.TextField");

  FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
  fieldTypeDefinition.setAttributes(fieldTypeAttributes);
  Map<String, Object> analyzerAttributes = new LinkedHashMap<>();
  analyzerAttributes.put("class", "org.apache.lucene.analysis.core.WhitespaceAnalyzer");
  analyzerAttributes.put("luceneMatchVersion", "5.0.0");
  AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
  analyzerDefinition.setAttributes(analyzerAttributes);
  fieldTypeDefinition.setAnalyzer(analyzerDefinition);


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

  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(analyzerAttributes.get("class"),
      is(equalTo(newFieldTypeRepresentation.getAnalyzer().getAttributes().get("class"))));
  assertThat(analyzerAttributes.get("luceneMatchVersion"),
      is(equalTo(newFieldTypeRepresentation.getAnalyzer().getAttributes().get("luceneMatchVersion"))));
}
 
Example 4
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteFieldTypeAccuracy() throws Exception {
  Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
  String fieldTypeName = "delInt";
  fieldTypeAttributes.put("name", fieldTypeName);
  fieldTypeAttributes.put("class",  RANDOMIZED_NUMERIC_FIELDTYPES.get(Integer.class));
  fieldTypeAttributes.put("omitNorms", true);
  fieldTypeAttributes.put("positionIncrementGap", 0);
  FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
  fieldTypeDefinition.setAttributes(fieldTypeAttributes);
  SchemaRequest.AddFieldType addFieldTypeRequest =
      new SchemaRequest.AddFieldType(fieldTypeDefinition);
  SolrClient c = getSolrClient();
  SchemaResponse.UpdateResponse addFieldTypeResponse = addFieldTypeRequest.process(c);
  assertValidSchemaResponse(addFieldTypeResponse);

  SchemaRequest.FieldType fieldTypeRequest = new SchemaRequest.FieldType(fieldTypeName);
  SchemaResponse.FieldTypeResponse initialFieldTypeResponse = fieldTypeRequest.process(getSolrClient());
  assertValidSchemaResponse(initialFieldTypeResponse);
  FieldTypeRepresentation responseFieldTypeRepresentation = initialFieldTypeResponse.getFieldType();
  assertThat(fieldTypeName, is(equalTo(responseFieldTypeRepresentation.getAttributes().get("name"))));

  SchemaRequest.DeleteFieldType deleteFieldTypeRequest =
      new SchemaRequest.DeleteFieldType(fieldTypeName);
  SchemaResponse.UpdateResponse deleteFieldTypeResponse = deleteFieldTypeRequest.process(getSolrClient());
  assertValidSchemaResponse(deleteFieldTypeResponse);

  try {
    fieldTypeRequest.process(getSolrClient());
    fail(String.format(Locale.ROOT, "after removal, the field type %s shouldn't be anymore available over Schema API",
        fieldTypeName));
  } catch (SolrException e) {
    //success
  }
}
 
Example 5
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"))));
}
 
Example 6
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testReplaceFieldTypeAccuracy() throws Exception {
  // a fixed value for comparison after update, be contraian from the randomized 'default'
  final boolean useDv = Boolean.getBoolean(NUMERIC_DOCVALUES_SYSPROP);
  
  // Given
  Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
  String fieldTypeName = "replaceInt";
  fieldTypeAttributes.put("name", fieldTypeName);
  fieldTypeAttributes.put("class",  RANDOMIZED_NUMERIC_FIELDTYPES.get(Integer.class));
  fieldTypeAttributes.put("docValues", useDv);
  fieldTypeAttributes.put("omitNorms", true);
  fieldTypeAttributes.put("positionIncrementGap", 0);
  FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
  fieldTypeDefinition.setAttributes(fieldTypeAttributes);
  SchemaRequest.AddFieldType addFieldTypeRequest =
      new SchemaRequest.AddFieldType(fieldTypeDefinition);
  SchemaResponse.UpdateResponse addFieldTypeResponse = addFieldTypeRequest.process(getSolrClient());
  assertValidSchemaResponse(addFieldTypeResponse);

  // When : update the field definition
  fieldTypeAttributes.put("positionIncrementGap", 42);
  fieldTypeAttributes.put("omitNorms", false);
  FieldTypeDefinition replaceFieldTypeDefinition = new FieldTypeDefinition();
  replaceFieldTypeDefinition.setAttributes(fieldTypeAttributes);
  SchemaRequest.ReplaceFieldType replaceFieldTypeRequest =
      new SchemaRequest.ReplaceFieldType(replaceFieldTypeDefinition);
  SchemaResponse.UpdateResponse replaceFieldTypeResponse = replaceFieldTypeRequest.process(getSolrClient());
  assertValidSchemaResponse(replaceFieldTypeResponse);

  // Then
  SchemaRequest.FieldType fieldTypeRequest = new SchemaRequest.FieldType(fieldTypeName);
  SchemaResponse.FieldTypeResponse newFieldTypeResponse = fieldTypeRequest.process(getSolrClient());
  assertValidSchemaResponse(newFieldTypeResponse);
  FieldTypeRepresentation replacedFieldTypeRepresentation = newFieldTypeResponse.getFieldType();
  Map<String, Object> replacedFieldTypeAttributes = replacedFieldTypeRepresentation.getAttributes();
  assertThat(fieldTypeName, is(equalTo(replacedFieldTypeAttributes.get("name"))));
  assertThat( RANDOMIZED_NUMERIC_FIELDTYPES.get(Integer.class),
              is(equalTo(replacedFieldTypeAttributes.get("class"))));
  assertThat(false, is(equalTo(replacedFieldTypeAttributes.get("omitNorms"))));
  assertThat("42", is(equalTo(replacedFieldTypeAttributes.get("positionIncrementGap"))));
  // should be unchanged...
  assertThat(useDv, is(equalTo(replacedFieldTypeAttributes.get("docValues"))));
}
 
Example 7
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleUpdateRequestAccuracy() throws Exception {
  String fieldTypeName = "accuracyTextField";
  SchemaRequest.AddFieldType addFieldTypeRequest = createFieldTypeRequest(fieldTypeName);

  String field1Name = "accuracyField1";
  String field2Name = "accuracyField2";
  Map<String, Object> fieldAttributes = new LinkedHashMap<>();
  fieldAttributes.put("name", field1Name);
  fieldAttributes.put("type", fieldTypeName);
  fieldAttributes.put("stored", true);
  fieldAttributes.put("indexed", true);
  SchemaRequest.AddField addFieldName1Request = new SchemaRequest.AddField(fieldAttributes);
  fieldAttributes.put("name", field2Name);
  SchemaRequest.AddField addFieldName2Request = new SchemaRequest.AddField(fieldAttributes);

  List<SchemaRequest.Update> list = new ArrayList<>(3);
  list.add(addFieldTypeRequest);
  list.add(addFieldName1Request);
  list.add(addFieldName2Request);
  SchemaRequest.MultiUpdate multiUpdateRequest = new SchemaRequest.MultiUpdate(list);
  SchemaResponse.UpdateResponse multipleUpdatesResponse = multiUpdateRequest.process(getSolrClient());
  assertValidSchemaResponse(multipleUpdatesResponse);

  SchemaRequest.FieldType fieldTypeSchemaRequest =
      new SchemaRequest.FieldType(fieldTypeName);
  SchemaResponse.FieldTypeResponse fieldTypeResponse = fieldTypeSchemaRequest.process(getSolrClient());
  assertValidSchemaResponse(fieldTypeResponse);
  FieldTypeRepresentation fieldTypeRepresentation = fieldTypeResponse.getFieldType();
  assertThat(fieldTypeName, is(equalTo(fieldTypeRepresentation.getAttributes().get("name"))));

  SchemaRequest.Field field1SchemaRequest = new SchemaRequest.Field(field1Name);
  SchemaResponse.FieldResponse field1Response = field1SchemaRequest.process(getSolrClient());
  assertValidSchemaResponse(field1Response);
  Map<String, ?> field1Attributes = field1Response.getField();
  assertThat(field1Name, is(equalTo(field1Attributes.get("name"))));
  assertThat(fieldTypeName, is(equalTo(field1Attributes.get("type"))));
  assertThat(true, is(equalTo(field1Attributes.get("stored"))));
  assertThat(true, is(equalTo(field1Attributes.get("indexed"))));

  SchemaRequest.Field field2SchemaRequest = new SchemaRequest.Field(field1Name);
  SchemaResponse.FieldResponse field2Response = field2SchemaRequest.process(getSolrClient());
  assertValidSchemaResponse(field2Response);
  Map<String, ?> field2Attributes = field2Response.getField();
  assertThat(field1Name, is(equalTo(field2Attributes.get("name"))));
  assertThat(fieldTypeName, is(equalTo(field2Attributes.get("type"))));
  assertThat(true, is(equalTo(field2Attributes.get("stored"))));
  assertThat(true, is(equalTo(field2Attributes.get("indexed"))));
}