Search in sources :

Example 6 with RuleBasedCollator

use of com.ibm.icu.text.RuleBasedCollator in project lucene-solr by apache.

the class TestICUCollationField method setupSolrHome.

/**
   * Ugly: but what to do? We want to test custom sort, which reads rules in as a resource.
   * These are largish files, and jvm-specific (as our documentation says, you should always
   * look out for jvm differences with collation).
   * So it's preferable to create this file on-the-fly.
   */
public static String setupSolrHome() throws Exception {
    String tmpFile = createTempDir().toFile().getAbsolutePath();
    // make data and conf dirs
    new File(tmpFile + "/collection1", "data").mkdirs();
    File confDir = new File(tmpFile + "/collection1", "conf");
    confDir.mkdirs();
    // copy over configuration files
    FileUtils.copyFile(getFile("analysis-extras/solr/collection1/conf/solrconfig-icucollate.xml"), new File(confDir, "solrconfig.xml"));
    FileUtils.copyFile(getFile("analysis-extras/solr/collection1/conf/schema-icucollate.xml"), new File(confDir, "schema.xml"));
    // generate custom collation rules (DIN 5007-2), saving to customrules.dat
    RuleBasedCollator baseCollator = (RuleBasedCollator) Collator.getInstance(new ULocale("de", "DE"));
    String DIN5007_2_tailorings = "& ae , ä & AE , Ä" + "& oe , ö & OE , Ö" + "& ue , ü & UE , ü";
    RuleBasedCollator tailoredCollator = new RuleBasedCollator(baseCollator.getRules() + DIN5007_2_tailorings);
    String tailoredRules = tailoredCollator.getRules();
    final String osFileName = "customrules.dat";
    final FileOutputStream os = new FileOutputStream(new File(confDir, osFileName));
    IOUtils.write(tailoredRules, os, "UTF-8");
    os.close();
    final ResourceLoader loader;
    if (random().nextBoolean()) {
        loader = new StringMockResourceLoader(tailoredRules);
    } else {
        loader = new FilesystemResourceLoader(confDir.toPath());
    }
    final Collator readCollator = ICUCollationField.createFromRules(osFileName, loader);
    assertEquals(tailoredCollator, readCollator);
    return tmpFile;
}
Also used : ResourceLoader(org.apache.lucene.analysis.util.ResourceLoader) StringMockResourceLoader(org.apache.lucene.analysis.util.StringMockResourceLoader) FilesystemResourceLoader(org.apache.lucene.analysis.util.FilesystemResourceLoader) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) FilesystemResourceLoader(org.apache.lucene.analysis.util.FilesystemResourceLoader) ULocale(com.ibm.icu.util.ULocale) FileOutputStream(java.io.FileOutputStream) File(java.io.File) StringMockResourceLoader(org.apache.lucene.analysis.util.StringMockResourceLoader) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) Collator(com.ibm.icu.text.Collator)

Aggregations

RuleBasedCollator (com.ibm.icu.text.RuleBasedCollator)6 ULocale (com.ibm.icu.util.ULocale)4 Collator (com.ibm.icu.text.Collator)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 SolrException (org.apache.solr.common.SolrException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FilesystemResourceLoader (org.apache.lucene.analysis.util.FilesystemResourceLoader)1 ResourceLoader (org.apache.lucene.analysis.util.ResourceLoader)1 StringMockResourceLoader (org.apache.lucene.analysis.util.StringMockResourceLoader)1 ICUCollationKeyAnalyzer (org.apache.lucene.collation.ICUCollationKeyAnalyzer)1 Settings (org.elasticsearch.common.settings.Settings)1 Index (org.elasticsearch.index.Index)1 AnalysisICUPlugin (org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin)1