0

I need to create an autosuggestor, so I am using Lucene Suggest module. My requirement is

        IndexWriter indexWriter = new IndexWriter(directory, config);

        Document doc = new Document();
        doc.Add(new StringField("content", "Humpty Dumpty sat on a wall", Field.Store.YES));
        doc.Add(new StringField("kid", "1", Field.Store.YES));
        indexWriter.AddDocument(doc);
        doc = new Document();
        doc.Add(new StringField("content", "Humpty Dumpty had a great fall", Field.Store.YES));
        doc.Add(new StringField("kid", "2", Field.Store.YES));
        indexWriter.AddDocument(doc);
        doc = new Document();
        doc.Add(new StringField("content", "All the king's horses and all the king's men", Field.Store.YES));
        doc.Add(new StringField("kid", "1", Field.Store.YES));
        indexWriter.AddDocument(doc);
        doc = new Document();
        doc.Add(new StringField("content", "Couldn't put Humpty together again", Field.Store.YES));
        doc.Add(new StringField("kid", "2", Field.Store.YES));
        indexWriter.AddDocument(doc);

        indexWriter.Commit();
        

        IndexReader indexReader = DirectoryReader.Open(directory);

        var dictionary = new LuceneDictionary(indexReader, "content");

        var jkk = dictionary.GetEntryEnumerator();
        
        

        FuzzySuggester fuzzySuggester = new FuzzySuggester(new StandardAnalyzer(LuceneVersion.LUCENE_48));
        fuzzySuggester.Build(dictionary);

My requirement is to create separate Suggestor for different values of Kid , in above example basically 2 FuzzySelector one for kid=1 and other kid=2, I am unable to understand , how can I do that. Other way , is to just use the normal lucene search

user2799564
  • 147
  • 2
  • 8

1 Answers1

0

Here is some sample code from Lucene.Net that comes from one of the unit tests that you may find helpful. Your code will be a bit different since it wont't be a unit test, but it should point you in the right direction. You will, for example, need to supply a real Analyzer like the perhaps a AnalyzingInfixSuggester:

/*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
public class AnalyzingSuggesterTest : LuceneTestCase
    {
        /** this is basically the WFST test ported to KeywordAnalyzer. so it acts the same */
        [Test]
        public void TestKeyword()
        {
            IEnumerable<Input> keys = Shuffle(
                new Input("foo", 50),
                new Input("bar", 10),
                new Input("barbar", 10),
                new Input("barbar", 12),
                new Input("barbara", 6),
                new Input("bar", 5),
                new Input("barbara", 1)
            );

            AnalyzingSuggester suggester = new AnalyzingSuggester(new MockAnalyzer(Random, MockTokenizer.KEYWORD, false));
            suggester.Build(new InputArrayEnumerator(keys));

            // top N of 2, but only foo is available
            IList<Lookup.LookupResult> results = suggester.DoLookup(TestUtil.StringToCharSequence("f", Random).ToString(), false, 2);
            assertEquals(1, results.size());
            assertEquals("foo", results[0].Key.toString());
            assertEquals(50, results[0].Value, 0.01F);

            // top N of 1 for 'bar': we return this even though
            // barbar is higher because exactFirst is enabled:
            results = suggester.DoLookup(TestUtil.StringToCharSequence("bar", Random).ToString(), false, 1);
            assertEquals(1, results.size());
            assertEquals("bar", results[0].Key.toString());
            assertEquals(10, results[0].Value, 0.01F);

            // top N Of 2 for 'b'
            results = suggester.DoLookup(TestUtil.StringToCharSequence("b", Random).ToString(), false, 2);
            assertEquals(2, results.size());
            assertEquals("barbar", results[0].Key.toString());
            assertEquals(12, results[0].Value, 0.01F);
            assertEquals("bar", results[1].Key.toString());
            assertEquals(10, results[1].Value, 0.01F);

            // top N of 3 for 'ba'
            results = suggester.DoLookup(TestUtil.StringToCharSequence("ba", Random).ToString(), false, 3);
            assertEquals(3, results.size());
            assertEquals("barbar", results[0].Key.toString());
            assertEquals(12, results[0].Value, 0.01F);
            assertEquals("bar", results[1].Key.toString());
            assertEquals(10, results[1].Value, 0.01F);
            assertEquals("barbara", results[2].Key.toString());
            assertEquals(6, results[2].Value, 0.01F);
        }

The above code comes from the Lucene.Net github repository

Additionly, you may find the example in this StackOverflow answer useful. It provides a java example of using a AnalyzingInfixSuggester including advanced features like payloads.

RonC
  • 31,330
  • 19
  • 94
  • 139
  • I have looked at this code, I was trying to find a way by which I can use --var dictionary = new LuceneDictionary(indexReader, "content","XXXXXX"); specify some condition here. Otherwise , like you said , will use that – user2799564 Mar 09 '21 at 03:16
  • The `LuceneDictionary` class does not have a constructor that can take a third argument. You are probably aware of this [Lucene4 cookbook example](https://github.com/edng/lucene4_cookbook_examples/blob/master/src/main/java/org/edng/lucene4/example/AutoSuggestTest.java#L35) for using a `LuceneDictionary` to build a suggester, but if not, give it a look. – RonC Mar 09 '21 at 20:45