Example usage for org.apache.solr.client.solrj.request.schema AnalyzerDefinition setTokenizer

List of usage examples for org.apache.solr.client.solrj.request.schema AnalyzerDefinition setTokenizer

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request.schema AnalyzerDefinition setTokenizer.

Prototype

public void setTokenizer(Map<String, Object> tokenizer) 

Source Link

Usage

From source file:org.intermine.api.searchengine.solr.SolrIndexHandler.java

License:GNU General Public License

private void createFieldTypeDefinitions(SolrClient solrClient) throws IOException {
    FieldTypeDefinition analyzedFieldTypeDefinition = new FieldTypeDefinition();

    Map<String, Object> analyzedFieldTypeAttributes = new HashMap();
    analyzedFieldTypeAttributes.put("name", ANALYZED_FIELD_TYPE_NAME);
    analyzedFieldTypeAttributes.put("class", "solr.TextField");
    analyzedFieldTypeAttributes.put("positionIncrementGap", 100);
    analyzedFieldTypeAttributes.put("multiValued", true);

    AnalyzerDefinition indexAnalyzerDefinition1 = new AnalyzerDefinition();
    Map<String, Object> indexTokenizerAttributes1 = new HashMap<String, Object>();
    indexTokenizerAttributes1.put("class", "solr.WhitespaceTokenizerFactory");
    indexAnalyzerDefinition1.setTokenizer(indexTokenizerAttributes1);
    Map<String, Object> indexLowerCaseFilterAttributes1 = new HashMap<String, Object>();
    indexLowerCaseFilterAttributes1.put("class", "solr.LowerCaseFilterFactory");
    List<Map<String, Object>> indexFilterAttributes1 = new ArrayList<Map<String, Object>>();
    indexFilterAttributes1.add(indexLowerCaseFilterAttributes1);
    indexAnalyzerDefinition1.setFilters(indexFilterAttributes1);

    AnalyzerDefinition queryAnalyzerDefinition1 = new AnalyzerDefinition();
    Map<String, Object> queryTokenizerAttributes1 = new HashMap<String, Object>();
    queryTokenizerAttributes1.put("class", "solr.WhitespaceTokenizerFactory");
    queryAnalyzerDefinition1.setTokenizer(queryTokenizerAttributes1);
    Map<String, Object> queryLowerCaseFilterAttributes1 = new HashMap<String, Object>();
    queryLowerCaseFilterAttributes1.put("class", "solr.LowerCaseFilterFactory");
    List<Map<String, Object>> queryFilterAttributes1 = new ArrayList<Map<String, Object>>();
    queryFilterAttributes1.add(queryLowerCaseFilterAttributes1);
    queryAnalyzerDefinition1.setFilters(queryFilterAttributes1);

    analyzedFieldTypeDefinition.setAttributes(analyzedFieldTypeAttributes);
    analyzedFieldTypeDefinition.setIndexAnalyzer(indexAnalyzerDefinition1);
    analyzedFieldTypeDefinition.setQueryAnalyzer(queryAnalyzerDefinition1);

    try {/* w  w w .jav  a  2  s .c om*/
        SchemaRequest.AddFieldType schemaRequest = new SchemaRequest.AddFieldType(analyzedFieldTypeDefinition);

        SchemaResponse.UpdateResponse response = schemaRequest.process(solrClient);

    } catch (SolrServerException e) {
        LOG.error("Error while adding fieldtype '" + ANALYZED_FIELD_TYPE_NAME + "' to the solrclient.", e);

        e.printStackTrace();
    }

    FieldTypeDefinition rawFieldTypeDefinition = new FieldTypeDefinition();

    Map<String, Object> rawFieldTypeAttributes = new HashMap();
    rawFieldTypeAttributes.put("name", RAW_FIELD_TYPE_NAME);
    rawFieldTypeAttributes.put("class", "solr.TextField");
    rawFieldTypeAttributes.put("positionIncrementGap", 100);
    rawFieldTypeAttributes.put("multiValued", true);

    AnalyzerDefinition indexAnalyzerDefinition2 = new AnalyzerDefinition();
    Map<String, Object> indexTokenizerAttributes2 = new HashMap<String, Object>();
    indexTokenizerAttributes2.put("class", "solr.KeywordTokenizerFactory");
    indexAnalyzerDefinition2.setTokenizer(indexTokenizerAttributes2);
    Map<String, Object> indexLowerCaseFilterAttributes2 = new HashMap<String, Object>();
    indexLowerCaseFilterAttributes2.put("class", "solr.LowerCaseFilterFactory");
    List<Map<String, Object>> indexFilterAttributes2 = new ArrayList<Map<String, Object>>();
    indexFilterAttributes2.add(indexLowerCaseFilterAttributes2);
    indexAnalyzerDefinition2.setFilters(indexFilterAttributes2);

    AnalyzerDefinition queryAnalyzerDefinition2 = new AnalyzerDefinition();
    Map<String, Object> queryTokenizerAttributes2 = new HashMap<String, Object>();
    queryTokenizerAttributes2.put("class", "solr.KeywordTokenizerFactory");
    queryAnalyzerDefinition2.setTokenizer(queryTokenizerAttributes2);
    Map<String, Object> queryLowerCaseFilterAttributes2 = new HashMap<String, Object>();
    queryLowerCaseFilterAttributes2.put("class", "solr.LowerCaseFilterFactory");
    List<Map<String, Object>> queryFilterAttributes2 = new ArrayList<Map<String, Object>>();
    queryFilterAttributes2.add(queryLowerCaseFilterAttributes2);
    queryAnalyzerDefinition2.setFilters(queryFilterAttributes2);

    rawFieldTypeDefinition.setAttributes(rawFieldTypeAttributes);
    rawFieldTypeDefinition.setIndexAnalyzer(indexAnalyzerDefinition2);
    rawFieldTypeDefinition.setQueryAnalyzer(queryAnalyzerDefinition2);

    try {
        SchemaRequest.AddFieldType schemaRequest = new SchemaRequest.AddFieldType(rawFieldTypeDefinition);

        SchemaResponse.UpdateResponse response = schemaRequest.process(solrClient);

    } catch (SolrServerException e) {
        LOG.error("Error while adding fieldtype '" + RAW_FIELD_TYPE_NAME + "' to the solrclient.", e);

        e.printStackTrace();
    }
}