Search in sources :

Example 1 with ICUCollationKeyAnalyzer

use of org.apache.lucene.collation.ICUCollationKeyAnalyzer in project lucene-solr by apache.

the class ICUCollationField method setup.

/**
   * Setup the field according to the provided parameters
   */
private void setup(ResourceLoader loader, Map<String, String> args) {
    String custom = args.remove("custom");
    String localeID = args.remove("locale");
    String strength = args.remove("strength");
    String decomposition = args.remove("decomposition");
    String alternate = args.remove("alternate");
    String caseLevel = args.remove("caseLevel");
    String caseFirst = args.remove("caseFirst");
    String numeric = args.remove("numeric");
    String variableTop = args.remove("variableTop");
    if (custom == null && localeID == null)
        throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or locale is required.");
    if (custom != null && localeID != null)
        throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both locale and custom. " + "To tailor rules for a built-in language, see the javadocs for RuleBasedCollator. " + "Then save the entire customized ruleset to a file, and use with the custom parameter");
    final Collator collator;
    if (localeID != null) {
        // create from a system collator, based on Locale.
        collator = createFromLocale(localeID);
    } else {
        // create from a custom ruleset
        collator = createFromRules(custom, loader);
    }
    // set the strength flag, otherwise it will be the default.
    if (strength != null) {
        if (strength.equalsIgnoreCase("primary"))
            collator.setStrength(Collator.PRIMARY);
        else if (strength.equalsIgnoreCase("secondary"))
            collator.setStrength(Collator.SECONDARY);
        else if (strength.equalsIgnoreCase("tertiary"))
            collator.setStrength(Collator.TERTIARY);
        else if (strength.equalsIgnoreCase("quaternary"))
            collator.setStrength(Collator.QUATERNARY);
        else if (strength.equalsIgnoreCase("identical"))
            collator.setStrength(Collator.IDENTICAL);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid strength: " + strength);
    }
    // set the decomposition flag, otherwise it will be the default.
    if (decomposition != null) {
        if (decomposition.equalsIgnoreCase("no"))
            collator.setDecomposition(Collator.NO_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("canonical"))
            collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
    }
    // expert options: concrete subclasses are always a RuleBasedCollator
    RuleBasedCollator rbc = (RuleBasedCollator) collator;
    if (alternate != null) {
        if (alternate.equalsIgnoreCase("shifted")) {
            rbc.setAlternateHandlingShifted(true);
        } else if (alternate.equalsIgnoreCase("non-ignorable")) {
            rbc.setAlternateHandlingShifted(false);
        } else {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid alternate: " + alternate);
        }
    }
    if (caseLevel != null) {
        rbc.setCaseLevel(Boolean.parseBoolean(caseLevel));
    }
    if (caseFirst != null) {
        if (caseFirst.equalsIgnoreCase("lower")) {
            rbc.setLowerCaseFirst(true);
        } else if (caseFirst.equalsIgnoreCase("upper")) {
            rbc.setUpperCaseFirst(true);
        } else {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid caseFirst: " + caseFirst);
        }
    }
    if (numeric != null) {
        rbc.setNumericCollation(Boolean.parseBoolean(numeric));
    }
    if (variableTop != null) {
        rbc.setVariableTop(variableTop);
    }
    analyzer = new ICUCollationKeyAnalyzer(collator);
}
Also used : RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) SolrException(org.apache.solr.common.SolrException) ICUCollationKeyAnalyzer(org.apache.lucene.collation.ICUCollationKeyAnalyzer) Collator(com.ibm.icu.text.Collator) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator)

Aggregations

Collator (com.ibm.icu.text.Collator)1 RuleBasedCollator (com.ibm.icu.text.RuleBasedCollator)1 ICUCollationKeyAnalyzer (org.apache.lucene.collation.ICUCollationKeyAnalyzer)1 SolrException (org.apache.solr.common.SolrException)1