Exemple #1
0
        public void TestAfterLoad()
        {
            Input[] keys = new Input[] {
                new Input("lend me your ear", 8, new BytesRef("foobar")),
                new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")),
            };

            DirectoryInfo tempDir = CreateTempDir("AnalyzingInfixSuggesterTest");

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3);

            suggester.Build(new InputArrayIterator(keys));
            assertEquals(2, suggester.Count);
            suggester.Dispose();

            suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3);
            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, true);

            assertEquals(2, results.size());
            assertEquals("a penny saved is a penny <b>ear</b>ned", results[0].key);
            assertEquals(10, results[0].value);
            assertEquals(new BytesRef("foobaz"), results[0].payload);
            assertEquals(2, suggester.Count);
            suggester.Dispose();
        }
Exemple #2
0
        public void TestHighlightCaseChange()
        {
            Input[] keys = new Input[] {
                new Input("a Penny saved is a penny earned", 10, new BytesRef("foobaz")),
            };

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, true);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);

            suggester.Build(new InputArrayIterator(keys));
            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random()).ToString(), 10, true, true);

            assertEquals(1, results.size());
            assertEquals("a <b>Penn</b>y saved is a <b>penn</b>y earned", results[0].key);
            suggester.Dispose();

            // Try again, but overriding addPrefixMatch to highlight
            // the entire hit:
            suggester = new TestHighlightChangeCaseAnalyzingInfixSuggester(this, a);

            suggester.Build(new InputArrayIterator(keys));
            results = suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("a <b>Penny</b> saved is a <b>penny</b> earned", results[0].key);
            suggester.Dispose();
        }
Exemple #3
0
 public override void Run()
 {
     Priority += 1;
     while (!stop.Get())
     {
         string query            = RandomText();
         int    topN             = TestUtil.NextInt(Random(), 1, 100);
         bool   allTermsRequired = Random().nextBoolean();
         bool   doHilite         = Random().nextBoolean();
         // We don't verify the results; just doing
         // simultaneous lookups while adding/updating to
         // see if there are any thread hazards:
         try
         {
             suggester.DoLookup(TestUtil.StringToCharSequence(query, Random()).ToString(),
                                topN, allTermsRequired, doHilite);
             Thread.Sleep(10);// don't starve refresh()'s CPU, which sleeps every 50 bytes for 1 ms
         }
         catch (Exception e)
         {
             error[0] = e;
             stop.Set(true);
         }
     }
 }
Exemple #4
0
        public void TestBasic()
        {
            Input[] keys = new Input[] {
                new Input("lend me your ear", 8, new BytesRef("foobar")),
                new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")),
            };

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);

            suggester.Build(new InputArrayIterator(keys));

            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, true);

            assertEquals(2, results.size());
            assertEquals("a penny saved is a penny <b>ear</b>ned", results[0].Key);
            assertEquals(10, results[0].Value);
            assertEquals(new BytesRef("foobaz"), results[0].Payload);

            assertEquals("lend me your <b>ear</b>", results[1].Key);
            assertEquals(8, results[1].Value);
            assertEquals(new BytesRef("foobar"), results[1].Payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("ear ", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("lend me your <b>ear</b>", results[0].Key);
            assertEquals(8, results[0].Value);
            assertEquals(new BytesRef("foobar"), results[0].Payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("pen", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results[0].Key);
            assertEquals(10, results[0].Value);
            assertEquals(new BytesRef("foobaz"), results[0].Payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("p", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results[0].Key);
            assertEquals(10, results[0].Value);
            assertEquals(new BytesRef("foobaz"), results[0].Payload);

            suggester.Dispose();
        }
Exemple #5
0
        public void TestHighlight()
        {
            Input[] keys = new Input[] {
                new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")),
            };

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);

            suggester.Build(new InputArrayIterator(keys));
            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random()).ToString(), 10, true, true);

            assertEquals(1, results.size());
            assertEquals("a <b>penn</b>y saved is a <b>penn</b>y earned", results[0].key);
            suggester.Dispose();
        }
Exemple #6
0
        public void TestBothExactAndPrefix()
        {
            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);

            suggester.Build(new InputArrayIterator(new Input[0]));
            suggester.Add(new BytesRef("the pen is pretty"), null, 10, new BytesRef("foobaz"));
            suggester.Refresh();

            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("pen p", Random()).ToString(), 10, true, true);

            assertEquals(1, results.size());
            assertEquals("the <b>pen</b> is <b>p</b>retty", results[0].key);
            assertEquals(10, results[0].value);
            assertEquals(new BytesRef("foobaz"), results[0].payload);
            suggester.Dispose();
        }
Exemple #7
0
        public void TestSuggestStopFilter()
        {
            CharArraySet stopWords     = StopFilter.MakeStopSet(TEST_VERSION_CURRENT, "a");
            Analyzer     indexAnalyzer = new TestSuggestStopFilterAnalyzer1(this, stopWords);
            Analyzer     queryAnalyzer = new TestSuggestStopFilterAnalyzer2(this, stopWords);

            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), indexAnalyzer, queryAnalyzer, 3);

            Input[] keys = new Input[] {
                new Input("a bob for apples", 10, new BytesRef("foobaz")),
            };

            suggester.Build(new InputArrayIterator(keys));
            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("a", Random()).ToString(), 10, true, true);

            assertEquals(1, results.size());
            assertEquals("a bob for <b>a</b>pples", results[0].key);
            suggester.Dispose();
        }
Exemple #8
0
        public void TestBasicNRT()
        {
            Input[] keys = new Input[] {
                new Input("lend me your ear", 8, new BytesRef("foobar")),
            };

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);

            suggester.Build(new InputArrayIterator(keys));

            IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, true);

            assertEquals(1, results.size());
            assertEquals("lend me your <b>ear</b>", results.ElementAt(0).key);
            assertEquals(8, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobar"), results.ElementAt(0).payload);

            // Add a new suggestion:
            suggester.Add(new BytesRef("a penny saved is a penny earned"), null, 10, new BytesRef("foobaz"));

            // Must refresh to see any newly added suggestions:
            suggester.Refresh();

            results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, true);
            assertEquals(2, results.size());
            assertEquals("a penny saved is a penny <b>ear</b>ned", results.ElementAt(0).key);
            assertEquals(10, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobaz"), results.ElementAt(0).payload);

            assertEquals("lend me your <b>ear</b>", results.ElementAt(1).key);
            assertEquals(8, results.ElementAt(1).value);
            assertEquals(new BytesRef("foobar"), results.ElementAt(1).payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("ear ", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("lend me your <b>ear</b>", results.ElementAt(0).key);
            assertEquals(8, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobar"), results.ElementAt(0).payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("pen", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results.ElementAt(0).key);
            assertEquals(10, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobaz"), results.ElementAt(0).payload);

            results = suggester.DoLookup(TestUtil.StringToCharSequence("p", Random()).ToString(), 10, true, true);
            assertEquals(1, results.size());
            assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results.ElementAt(0).key);
            assertEquals(10, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobaz"), results.ElementAt(0).payload);

            // Change the weight:
            suggester.Update(new BytesRef("lend me your ear"), null, 12, new BytesRef("foobox"));

            // Must refresh to see any newly added suggestions:
            suggester.Refresh();

            results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, true);
            assertEquals(2, results.size());
            assertEquals("lend me your <b>ear</b>", results.ElementAt(0).key);
            assertEquals(12, results.ElementAt(0).value);
            assertEquals(new BytesRef("foobox"), results.ElementAt(0).payload);
            assertEquals("a penny saved is a penny <b>ear</b>ned", results.ElementAt(1).key);
            assertEquals(10, results.ElementAt(1).value);
            assertEquals(new BytesRef("foobaz"), results.ElementAt(1).payload);
            suggester.Dispose();
        }
Exemple #9
0
        public void TestRandomMinPrefixLength()
        {
            Input[] keys = new Input[] {
                new Input("lend me your ear", 8, new BytesRef("foobar")),
                new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")),
            };
            DirectoryInfo tempDir = CreateTempDir("AnalyzingInfixSuggesterTest");

            Analyzer a = new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false);
            int      minPrefixLength          = Random().nextInt(10);
            AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, minPrefixLength);

            suggester.Build(new InputArrayIterator(keys));

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    bool doHighlight = j == 0;

                    IList <Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random()).ToString(), 10, true, doHighlight);
                    assertEquals(2, results.size());
                    if (doHighlight)
                    {
                        assertEquals("a penny saved is a penny <b>ear</b>ned", results[0].key);
                    }
                    else
                    {
                        assertEquals("a penny saved is a penny earned", results[0].key);
                    }
                    assertEquals(10, results[0].value);
                    if (doHighlight)
                    {
                        assertEquals("lend me your <b>ear</b>", results[1].key);
                    }
                    else
                    {
                        assertEquals("lend me your ear", results[1].key);
                    }
                    assertEquals(new BytesRef("foobaz"), results[0].payload);
                    assertEquals(8, results[1].value);
                    assertEquals(new BytesRef("foobar"), results[1].payload);

                    results = suggester.DoLookup(TestUtil.StringToCharSequence("ear ", Random()).ToString(), 10, true, doHighlight);
                    assertEquals(1, results.size());
                    if (doHighlight)
                    {
                        assertEquals("lend me your <b>ear</b>", results[0].key);
                    }
                    else
                    {
                        assertEquals("lend me your ear", results[0].key);
                    }
                    assertEquals(8, results[0].value);
                    assertEquals(new BytesRef("foobar"), results[0].payload);

                    results = suggester.DoLookup(TestUtil.StringToCharSequence("pen", Random()).ToString(), 10, true, doHighlight);
                    assertEquals(1, results.size());
                    if (doHighlight)
                    {
                        assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results[0].key);
                    }
                    else
                    {
                        assertEquals("a penny saved is a penny earned", results[0].key);
                    }
                    assertEquals(10, results[0].value);
                    assertEquals(new BytesRef("foobaz"), results[0].payload);

                    results = suggester.DoLookup(TestUtil.StringToCharSequence("p", Random()).ToString(), 10, true, doHighlight);
                    assertEquals(1, results.size());
                    if (doHighlight)
                    {
                        assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results[0].key);
                    }
                    else
                    {
                        assertEquals("a penny saved is a penny earned", results[0].key);
                    }
                    assertEquals(10, results[0].value);
                    assertEquals(new BytesRef("foobaz"), results[0].payload);
                }

                // Make sure things still work after close and reopen:
                suggester.Dispose();
                suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, minPrefixLength);
            }
            suggester.Dispose();
        }