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

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

Introduction

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

Prototype

AnalyzerDefinition

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 {/*from w ww .  j a v  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();
    }
}

From source file:org.phenotips.vocabulary.translation.AbstractXliffTranslatedSolrVocabularyExtension.java

License:Open Source License

/**
 * Adds (or replaces) a type definition for the {@link #addFields() fields used by this translation}.
 *
 * @throws SolrServerException if communicating with the Solr server failed
 * @throws IOException if communicating with the Solr server failed
 *///  w  w w  .  j ava 2  s  . c  om
protected void addFieldType() throws SolrServerException, IOException {
    FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();

    Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
    String name = "text_general_" + getTargetLocale().toString();
    fieldTypeAttributes.put("name", name);
    fieldTypeAttributes.put("class", "solr.TextField");
    fieldTypeDefinition.setAttributes(fieldTypeAttributes);

    AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
    analyzerDefinition.setAttributes(Collections.<String, Object>singletonMap("class", getAnalyzerType()));
    fieldTypeDefinition.setAnalyzer(analyzerDefinition);

    try {
        // The current version (5.5) of SolrJ/EmbeddedSolrServer doesn't support getting schema information,
        // so we do this the ugly way: try to add, check for errors, try to replace
        UpdateResponse response = new SchemaRequest.AddFieldType(fieldTypeDefinition).process(getClient());
        if (response.getResponse().get("errors") != null) {
            response = new SchemaRequest.ReplaceFieldType(fieldTypeDefinition).process(getClient());
        }
        this.logger.debug(response.toString());
    } catch (Exception ex) {
    }
}