ICU plugin: use root locale by default for collators (#26413)
Calls to Collator.getInstance without arguments returns a collator that uses the system's default locale, which we don't want because it makes behavior harder to reproduce. Change it to always use the root locale instead. For #25587
This commit is contained in:
parent
557d2b1eef
commit
e00366ba95
|
@ -84,7 +84,7 @@ public class IcuCollationTokenFilterFactory extends AbstractTokenFilterFactory {
|
||||||
}
|
}
|
||||||
collator = Collator.getInstance(locale);
|
collator = Collator.getInstance(locale);
|
||||||
} else {
|
} else {
|
||||||
collator = Collator.getInstance();
|
collator = Collator.getInstance(ULocale.ROOT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -389,7 +389,7 @@ public class ICUCollationKeywordFieldMapper extends FieldMapper {
|
||||||
}
|
}
|
||||||
collator = Collator.getInstance(locale);
|
collator = Collator.getInstance(locale);
|
||||||
} else {
|
} else {
|
||||||
collator = Collator.getInstance();
|
collator = Collator.getInstance(ULocale.ROOT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,19 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
// Tests borrowed from Solr's Icu collation key filter factory test.
|
// Tests borrowed from Solr's Icu collation key filter factory test.
|
||||||
public class SimpleIcuCollationTokenFilterTests extends ESTestCase {
|
public class SimpleIcuCollationTokenFilterTests extends ESTestCase {
|
||||||
|
/*
|
||||||
|
* Tests usage where we do not provide a language or locale
|
||||||
|
*/
|
||||||
|
public void testDefaultUsage() throws Exception {
|
||||||
|
Settings settings = Settings.builder()
|
||||||
|
.put("index.analysis.filter.myCollator.type", "icu_collation")
|
||||||
|
.put("index.analysis.filter.myCollator.strength", "primary")
|
||||||
|
.build();
|
||||||
|
TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin());
|
||||||
|
|
||||||
|
TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator");
|
||||||
|
assertCollatesToSame(filterFactory, "FOO", "foo");
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* Turkish has some funny casing.
|
* Turkish has some funny casing.
|
||||||
* This test shows how you can solve this kind of thing easily with collation.
|
* This test shows how you can solve this kind of thing easily with collation.
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class CollationFieldTypeTests extends FieldTypeTestCase {
|
||||||
ft.setName("field");
|
ft.setName("field");
|
||||||
ft.setIndexOptions(IndexOptions.DOCS);
|
ft.setIndexOptions(IndexOptions.DOCS);
|
||||||
|
|
||||||
Collator collator = Collator.getInstance().freeze();
|
Collator collator = Collator.getInstance(ULocale.ROOT).freeze();
|
||||||
((CollationFieldType) ft).setCollator(collator);
|
((CollationFieldType) ft).setCollator(collator);
|
||||||
|
|
||||||
RawCollationKey fooKey = collator.getRawCollationKey("foo", null);
|
RawCollationKey fooKey = collator.getRawCollationKey("foo", null);
|
||||||
|
@ -126,7 +126,7 @@ public class CollationFieldTypeTests extends FieldTypeTestCase {
|
||||||
ft.setName("field");
|
ft.setName("field");
|
||||||
ft.setIndexOptions(IndexOptions.DOCS);
|
ft.setIndexOptions(IndexOptions.DOCS);
|
||||||
|
|
||||||
Collator collator = Collator.getInstance().freeze();
|
Collator collator = Collator.getInstance(ULocale.ROOT).freeze();
|
||||||
((CollationFieldType) ft).setCollator(collator);
|
((CollationFieldType) ft).setCollator(collator);
|
||||||
|
|
||||||
RawCollationKey aKey = collator.getRawCollationKey("a", null);
|
RawCollationKey aKey = collator.getRawCollationKey("a", null);
|
||||||
|
|
|
@ -82,7 +82,7 @@ public class ICUCollationKeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
IndexableField[] fields = doc.rootDoc().getFields("field");
|
IndexableField[] fields = doc.rootDoc().getFields("field");
|
||||||
assertEquals(2, fields.length);
|
assertEquals(2, fields.length);
|
||||||
|
|
||||||
Collator collator = Collator.getInstance();
|
Collator collator = Collator.getInstance(ULocale.ROOT);
|
||||||
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
||||||
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ public class ICUCollationKeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
IndexableField[] fields = doc.rootDoc().getFields("field");
|
IndexableField[] fields = doc.rootDoc().getFields("field");
|
||||||
assertEquals(2, fields.length);
|
assertEquals(2, fields.length);
|
||||||
|
|
||||||
Collator collator = Collator.getInstance();
|
Collator collator = Collator.getInstance(ULocale.ROOT);
|
||||||
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
||||||
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ public class ICUCollationKeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
.bytes(),
|
.bytes(),
|
||||||
XContentType.JSON));
|
XContentType.JSON));
|
||||||
|
|
||||||
Collator collator = Collator.getInstance();
|
Collator collator = Collator.getInstance(ULocale.ROOT);
|
||||||
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
||||||
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
||||||
|
|
||||||
|
@ -284,7 +284,7 @@ public class ICUCollationKeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
IndexableField[] fields = doc.rootDoc().getFields("field");
|
IndexableField[] fields = doc.rootDoc().getFields("field");
|
||||||
assertEquals(4, fields.length);
|
assertEquals(4, fields.length);
|
||||||
|
|
||||||
Collator collator = Collator.getInstance();
|
Collator collator = Collator.getInstance(ULocale.ROOT);
|
||||||
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
RawCollationKey key = collator.getRawCollationKey("1234", null);
|
||||||
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
BytesRef expected = new BytesRef(key.bytes, 0, key.size);
|
||||||
|
|
||||||
|
@ -305,7 +305,7 @@ public class ICUCollationKeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
assertThat(fieldType.indexOptions(), equalTo(IndexOptions.NONE));
|
assertThat(fieldType.indexOptions(), equalTo(IndexOptions.NONE));
|
||||||
assertEquals(DocValuesType.SORTED_SET, fieldType.docValuesType());
|
assertEquals(DocValuesType.SORTED_SET, fieldType.docValuesType());
|
||||||
|
|
||||||
collator = Collator.getInstance();
|
collator = Collator.getInstance(ULocale.ROOT);
|
||||||
key = collator.getRawCollationKey("5678", null);
|
key = collator.getRawCollationKey("5678", null);
|
||||||
expected = new BytesRef(key.bytes, 0, key.size);
|
expected = new BytesRef(key.bytes, 0, key.size);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue