Exemple #1
0
        public void TestBasic()
        {
            Directory                  dir            = NewDirectory();
            RandomIndexWriter          iw             = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
            Document                   doc            = new Document();
            Field                      field          = NewField("field", "", StringField.TYPE_STORED);
            ICUCollationDocValuesField collationField = new ICUCollationDocValuesField("collated", Collator.GetInstance(new CultureInfo("en")));

            doc.Add(field);
            doc.Add(collationField);

            field.SetStringValue("ABC");
            collationField.SetStringValue("ABC");
            iw.AddDocument(doc);

            field.SetStringValue("abc");
            collationField.SetStringValue("abc");
            iw.AddDocument(doc);

            IndexReader ir = iw.Reader;

            iw.Dispose();

            IndexSearcher @is = NewSearcher(ir);

            SortField sortField = new SortField("collated", SortFieldType.STRING);

            TopDocs td = @is.Search(new MatchAllDocsQuery(), 5, new Sort(sortField));

            assertEquals("abc", ir.Document(td.ScoreDocs[0].Doc).Get("field"));
            assertEquals("ABC", ir.Document(td.ScoreDocs[1].Doc).Get("field"));
            ir.Dispose();
            dir.Dispose();
        }
Exemple #2
0
        public void TestRanges()
        {
            Directory         dir = NewDirectory();
            RandomIndexWriter iw  = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, dir);
            Document doc      = new Document();
            Field    field    = NewField("field", "", StringField.TYPE_STORED);
            Collator collator = Collator.GetInstance(CultureInfo.CurrentCulture); // uses -Dtests.locale

            if (Random.nextBoolean())
            {
                collator.Strength = CollationStrength.Primary;
            }
            ICUCollationDocValuesField collationField = new ICUCollationDocValuesField("collated", collator);

            doc.Add(field);
            doc.Add(collationField);

            int numDocs = AtLeast(500);

            for (int i = 0; i < numDocs; i++)
            {
                String value = TestUtil.RandomSimpleString(Random);
                field.SetStringValue(value);
                collationField.SetStringValue(value);
                iw.AddDocument(doc);
            }

            IndexReader ir = iw.GetReader();

            iw.Dispose();
            IndexSearcher @is = NewSearcher(ir);

            int numChecks = AtLeast(100);

            for (int i = 0; i < numChecks; i++)
            {
                String   start    = TestUtil.RandomSimpleString(Random);
                String   end      = TestUtil.RandomSimpleString(Random);
                BytesRef lowerVal = new BytesRef(collator.GetCollationKey(start).ToByteArray());
                BytesRef upperVal = new BytesRef(collator.GetCollationKey(end).ToByteArray());
                Query    query    = new ConstantScoreQuery(FieldCacheRangeFilter.NewBytesRefRange("collated", lowerVal, upperVal, true, true));
                DoTestRanges(@is, start, end, query, collator);
            }

            ir.Dispose();
            dir.Dispose();
        }
        public void TestRanges()
        {
            Directory         dir      = NewDirectory();
            RandomIndexWriter iw       = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
            Document          doc      = new Document();
            Field             field    = NewField("field", "", StringField.TYPE_STORED);
            Collator          collator = Collator.Create(CultureInfo.CurrentCulture, Collator.Fallback.FallbackAllowed); // uses -Dtests.locale

            if (Random().nextBoolean())
            {
                collator.Strength = CollationStrength.Primary;
            }
            ICUCollationDocValuesField collationField = new ICUCollationDocValuesField("collated", collator);

            doc.Add(field);
            doc.Add(collationField);

            int numDocs = AtLeast(500);

            for (int i = 0; i < numDocs; i++)
            {
                String value = TestUtil.RandomSimpleString(Random());
                field.SetStringValue(value);
                collationField.SetStringValue(value);
                iw.AddDocument(doc);
            }

            IndexReader ir = iw.Reader;

            iw.Dispose();
            IndexSearcher @is = NewSearcher(ir);

            int numChecks = AtLeast(100);

            for (int i = 0; i < numChecks; i++)
            {
                String   start    = TestUtil.RandomSimpleString(Random());
                String   end      = TestUtil.RandomSimpleString(Random());
                BytesRef lowerVal = new BytesRef(collator.GetSortKey(start).KeyData);
                BytesRef upperVal = new BytesRef(collator.GetSortKey(end).KeyData);
                Query    query    = new ConstantScoreQuery(FieldCacheRangeFilter.NewBytesRefRange("collated", lowerVal, upperVal, true, true));
                DoTestRanges(@is, start, end, query, collator);
            }

            ir.Dispose();
            dir.Dispose();
        }