Search in sources :

Example 1 with AnalyzerDefinition

use of org.apache.solr.client.solrj.request.schema.AnalyzerDefinition in project lucene-solr by apache.

the class SchemaTest method addFieldTypeWithSimilarityAccuracy.

@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"))));
}
Also used : SchemaRequest(org.apache.solr.client.solrj.request.schema.SchemaRequest) FieldTypeRepresentation(org.apache.solr.client.solrj.response.schema.FieldTypeRepresentation) LinkedHashMap(java.util.LinkedHashMap) FieldTypeDefinition(org.apache.solr.client.solrj.request.schema.FieldTypeDefinition) AnalyzerDefinition(org.apache.solr.client.solrj.request.schema.AnalyzerDefinition) SchemaResponse(org.apache.solr.client.solrj.response.schema.SchemaResponse) Test(org.junit.Test)

Example 2 with AnalyzerDefinition

use of org.apache.solr.client.solrj.request.schema.AnalyzerDefinition in project lucene-solr by apache.

the class SchemaResponse method createAnalyzerDefinition.

@SuppressWarnings("unchecked")
private static AnalyzerDefinition createAnalyzerDefinition(NamedList<Object> analyzerNamedList) {
    AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
    Map<String, Object> analyzerAttributes = extractAttributeMap(analyzerNamedList);
    analyzerDefinition.setAttributes(analyzerAttributes);
    List<NamedList<Object>> charFiltersList = (List<NamedList<Object>>) analyzerNamedList.get("charFilters");
    if (charFiltersList != null) {
        List<Map<String, Object>> charFiltersAttributesList = new LinkedList<>();
        for (NamedList<Object> charFilterNamedList : charFiltersList) {
            Map<String, Object> charFilterAttributes = extractAttributeMap(charFilterNamedList);
            charFiltersAttributesList.add(charFilterAttributes);
        }
        analyzerDefinition.setCharFilters(charFiltersAttributesList);
    }
    NamedList<Object> tokenizerNamedList = (NamedList<Object>) analyzerNamedList.get("tokenizer");
    if (tokenizerNamedList != null) {
        Map<String, Object> tokenizerAttributes = extractAttributeMap(tokenizerNamedList);
        analyzerDefinition.setTokenizer(tokenizerAttributes);
    }
    List<NamedList<Object>> filtersList = (List<NamedList<Object>>) analyzerNamedList.get("filters");
    List<Map<String, Object>> filtersAttributesList = new LinkedList<>();
    if (filtersList != null) {
        for (NamedList<Object> filterNamedList : filtersList) {
            Map<String, Object> filterAttributes = extractAttributeMap(filterNamedList);
            filtersAttributesList.add(filterAttributes);
        }
        analyzerDefinition.setFilters(filtersAttributesList);
    }
    return analyzerDefinition;
}
Also used : AnalyzerDefinition(org.apache.solr.client.solrj.request.schema.AnalyzerDefinition) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) LinkedList(java.util.LinkedList) NamedList(org.apache.solr.common.util.NamedList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 3 with AnalyzerDefinition

use of org.apache.solr.client.solrj.request.schema.AnalyzerDefinition in project lucene-solr by apache.

the class SchemaResponse method fillFieldTypeDefinition.

@SuppressWarnings("unchecked")
private static void fillFieldTypeDefinition(FieldTypeDefinition fieldTypeDefinition, NamedList<Object> fieldTypeNamedList) {
    Map<String, Object> fieldTypeAttributes = extractAttributeMap(fieldTypeNamedList);
    fieldTypeDefinition.setAttributes(fieldTypeAttributes);
    NamedList<Object> analyzerNamedList = (NamedList<Object>) fieldTypeNamedList.get("analyzer");
    if (analyzerNamedList != null) {
        AnalyzerDefinition analyzerDefinition = createAnalyzerDefinition(analyzerNamedList);
        fieldTypeDefinition.setAnalyzer(analyzerDefinition);
    }
    NamedList<Object> indexAnalyzerNamedList = (NamedList<Object>) fieldTypeNamedList.get("indexAnalyzer");
    if (indexAnalyzerNamedList != null) {
        AnalyzerDefinition indexAnalyzerDefinition = createAnalyzerDefinition(indexAnalyzerNamedList);
        fieldTypeDefinition.setIndexAnalyzer(indexAnalyzerDefinition);
    }
    NamedList<Object> queryAnalyzerNamedList = (NamedList<Object>) fieldTypeNamedList.get("queryAnalyzer");
    if (queryAnalyzerNamedList != null) {
        AnalyzerDefinition queryAnalyzerDefinition = createAnalyzerDefinition(queryAnalyzerNamedList);
        fieldTypeDefinition.setQueryAnalyzer(queryAnalyzerDefinition);
    }
    NamedList<Object> multiTermAnalyzerNamedList = (NamedList<Object>) fieldTypeNamedList.get("multiTermAnalyzer");
    if (multiTermAnalyzerNamedList != null) {
        AnalyzerDefinition multiTermAnalyzerDefinition = createAnalyzerDefinition(multiTermAnalyzerNamedList);
        fieldTypeDefinition.setMultiTermAnalyzer(multiTermAnalyzerDefinition);
    }
    NamedList<Object> similarityNamedList = (NamedList<Object>) fieldTypeNamedList.get("similarity");
    if (similarityNamedList != null) {
        Map<String, Object> similarityAttributes = extractAttributeMap(similarityNamedList);
        fieldTypeDefinition.setSimilarity(similarityAttributes);
    }
}
Also used : AnalyzerDefinition(org.apache.solr.client.solrj.request.schema.AnalyzerDefinition) NamedList(org.apache.solr.common.util.NamedList)

Example 4 with AnalyzerDefinition

use of org.apache.solr.client.solrj.request.schema.AnalyzerDefinition in project lucene-solr by apache.

the class SchemaTest method createFieldTypeRequest.

private static SchemaRequest.AddFieldType createFieldTypeRequest(String fieldTypeName) {
    FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
    Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
    fieldTypeAttributes.put("name", fieldTypeName);
    fieldTypeAttributes.put("class", "solr.TextField");
    fieldTypeDefinition.setAttributes(fieldTypeAttributes);
    AnalyzerDefinition indexAnalyzerDefinition = new AnalyzerDefinition();
    Map<String, Object> iTokenizerAttributes = new LinkedHashMap<>();
    iTokenizerAttributes.put("class", "solr.PathHierarchyTokenizerFactory");
    iTokenizerAttributes.put("delimiter", "/");
    indexAnalyzerDefinition.setTokenizer(iTokenizerAttributes);
    fieldTypeDefinition.setIndexAnalyzer(indexAnalyzerDefinition);
    AnalyzerDefinition queryAnalyzerDefinition = new AnalyzerDefinition();
    Map<String, Object> qTokenizerAttributes = new LinkedHashMap<>();
    qTokenizerAttributes.put("class", "solr.KeywordTokenizerFactory");
    queryAnalyzerDefinition.setTokenizer(qTokenizerAttributes);
    fieldTypeDefinition.setQueryAnalyzer(queryAnalyzerDefinition);
    return new SchemaRequest.AddFieldType(fieldTypeDefinition);
}
Also used : FieldTypeDefinition(org.apache.solr.client.solrj.request.schema.FieldTypeDefinition) AnalyzerDefinition(org.apache.solr.client.solrj.request.schema.AnalyzerDefinition) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with AnalyzerDefinition

use of org.apache.solr.client.solrj.request.schema.AnalyzerDefinition in project lucene-solr by apache.

the class SchemaTest method testAddFieldTypeAccuracy.

@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"))));
}
Also used : SchemaRequest(org.apache.solr.client.solrj.request.schema.SchemaRequest) FieldTypeRepresentation(org.apache.solr.client.solrj.response.schema.FieldTypeRepresentation) LinkedHashMap(java.util.LinkedHashMap) FieldTypeDefinition(org.apache.solr.client.solrj.request.schema.FieldTypeDefinition) AnalyzerDefinition(org.apache.solr.client.solrj.request.schema.AnalyzerDefinition) SchemaResponse(org.apache.solr.client.solrj.response.schema.SchemaResponse) Test(org.junit.Test)

Aggregations

AnalyzerDefinition (org.apache.solr.client.solrj.request.schema.AnalyzerDefinition)6 LinkedHashMap (java.util.LinkedHashMap)5 FieldTypeDefinition (org.apache.solr.client.solrj.request.schema.FieldTypeDefinition)4 SchemaRequest (org.apache.solr.client.solrj.request.schema.SchemaRequest)3 FieldTypeRepresentation (org.apache.solr.client.solrj.response.schema.FieldTypeRepresentation)3 SchemaResponse (org.apache.solr.client.solrj.response.schema.SchemaResponse)3 Test (org.junit.Test)3 NamedList (org.apache.solr.common.util.NamedList)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1