This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git


The following commit(s) were added to refs/heads/master by this push:
     new f249ca9  Implements [LUCENE-5889] (#404)
f249ca9 is described below

commit f249ca95b47bc7f217479f0f1a779e404ae2e734
Author: Ron Clabo <[email protected]>
AuthorDate: Fri Jan 29 18:04:54 2021 -0500

    Implements [LUCENE-5889] (#404)
    
    * Implements 
[LUCENE-5889](https://github.com/apache/lucene-solr/commit/4a485d68771e5e123139cc12e9ea2733a922baea)
 A vesion 5.0 feature later backported to 4.11.0
    
    * Fixed failing test and cleaned up some code formatting
    
    * Formatting and other minor changes based on PR feedback
    
    * Fixed merge conflict
---
 .editorconfig                                      |   6 +-
 CHANGES.txt                                        |   2 +-
 .../Suggest/Analyzing/AnalyzingInfixSuggester.cs   | 101 +++++++++++++++++---
 .../Suggest/Analyzing/BlendedInfixSuggester.cs     |  24 ++++-
 .../Analyzing/AnalyzingInfixSuggesterTest.cs       | 103 +++++++++++++++++----
 .../Suggest/Analyzing/BlendedInfixSuggesterTest.cs |  16 ++--
 6 files changed, 208 insertions(+), 44 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index bf63574..2f625c2 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,8 +1,8 @@
-# You can modify the rules from these initially generated values to suit your 
own policies
+# You can modify the rules from these initially generated values to suit your 
own policies
 # You can learn more about editorconfig here: 
https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
 
 [*]
-charset = utf-8
+charset = utf-8-bom
 trim_trailing_whitespace = true
 
 [*.md]
@@ -37,7 +37,7 @@ csharp_new_line_before_finally = true
 #require members of object initializers to be on the same line
 csharp_new_line_before_members_in_object_initializers = false
 #require braces to be on a new line for control_blocks, types, properties, and 
methods (also known as "Allman" style)
-csharp_new_line_before_open_brace = control_blocks, types, properties, methods
+csharp_new_line_before_open_brace = accessors, anonymous_methods, 
control_blocks, lambdas, methods, object_collection_array_initializers, 
properties, types
 
 #Formatting - organize using options
 
diff --git a/CHANGES.txt b/CHANGES.txt
index 0a9534a..8383989 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,4 @@
-=================== Release 4.8.0-beta00006 =====================
+ =================== Release 4.8.0-beta00006 =====================
 
 Bug
 •      Lucene.Net.Support.Collections::Equals<T>(): Fixed comparison to 
include a check whether Count
diff --git 
a/src/Lucene.Net.Suggest/Suggest/Analyzing/AnalyzingInfixSuggester.cs 
b/src/Lucene.Net.Suggest/Suggest/Analyzing/AnalyzingInfixSuggester.cs
index 1b532b7..eff3a8f 100644
--- a/src/Lucene.Net.Suggest/Suggest/Analyzing/AnalyzingInfixSuggester.cs
+++ b/src/Lucene.Net.Suggest/Suggest/Analyzing/AnalyzingInfixSuggester.cs
@@ -63,6 +63,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
     public class AnalyzingInfixSuggester : Lookup, IDisposable
     {
+        private readonly object syncLock = new object();            //uses 
syncLock as substitute for Java's synchronized (method) keyword
 
         /// <summary>
         /// Field name used for the indexed text. </summary>
@@ -89,6 +90,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         internal readonly LuceneVersion matchVersion;
         private readonly Directory dir;
         internal readonly int minPrefixChars;
+        private readonly bool commitOnBuild;
 
         /// <summary>
         /// Used for ongoing NRT additions/updates. </summary>
@@ -133,8 +135,33 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         ///     Prefixes shorter than this are indexed as character
         ///     ngrams (increasing index size but making lookups
         ///     faster). </param>
-        public AnalyzingInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer, 
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. calls new 
constructor with extra param.
+        // LUCENENET TODO: Remove method at version 4.11.0. Was retained for 
perfect 4.8 compatibility
+        public AnalyzingInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer,
             Analyzer queryAnalyzer, int minPrefixChars)
+            : this(matchVersion, dir, indexAnalyzer, queryAnalyzer, 
minPrefixChars, commitOnBuild: false)
+        {
+        }
+
+
+        /// <summary>
+        /// Create a new instance, loading from a previously built
+        /// <see cref="AnalyzingInfixSuggester"/> directory, if it exists.  
This directory must be
+        /// private to the infix suggester (i.e., not an external
+        /// Lucene index).  Note that <see cref="Dispose()"/>
+        /// will also dispose the provided directory.
+        /// </summary>
+        ///  <param name="minPrefixChars"> Minimum number of leading characters
+        ///     before <see cref="PrefixQuery"/> is used (default 4).
+        ///     Prefixes shorter than this are indexed as character
+        ///     ngrams (increasing index size but making lookups
+        ///     faster). </param>
+        ///  <param name="commitOnBuild"> Call commit after the index has 
finished building. This
+        ///  would persist the suggester index to disk and future instances of 
this suggester can
+        ///  use this pre-built dictionary. </param>
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. (Code moved 
from other constructor to here.)
+        public AnalyzingInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer,
+            Analyzer queryAnalyzer, int minPrefixChars, bool commitOnBuild)
         {
 
             if (minPrefixChars < 0)
@@ -147,6 +174,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             this.matchVersion = matchVersion;
             this.dir = dir;
             this.minPrefixChars = minPrefixChars;
+            this.commitOnBuild = commitOnBuild;
 
             if (DirectoryReader.IndexExists(dir))
             {
@@ -160,7 +188,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         /// Override this to customize index settings, e.g. which
         /// codec to use. 
         /// </summary>
-        protected internal virtual IndexWriterConfig 
GetIndexWriterConfig(LuceneVersion matchVersion, 
+        protected internal virtual IndexWriterConfig 
GetIndexWriterConfig(LuceneVersion matchVersion,
             Analyzer indexAnalyzer, OpenMode openMode)
         {
             IndexWriterConfig iwc = new IndexWriterConfig(matchVersion, 
indexAnalyzer)
@@ -228,7 +256,10 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                 }
 
                 //System.out.println("initial indexing time: " + 
((System.nanoTime()-t0)/1000000) + " msec");
-
+                if (commitOnBuild)                      //LUCENENET specific 
-Support for LUCENE - 5889.
+                {
+                    Commit();
+                }
                 m_searcherMgr = new SearcherManager(writer, true, null);
                 success = true;
             }
@@ -246,7 +277,17 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             }
         }
 
-        private Analyzer GetGramAnalyzer() 
+        //LUCENENET specific -Support for LUCENE - 5889.
+        public void Commit()
+        {
+            if (writer == null)
+            {
+                throw new InvalidOperationException("Cannot commit on an 
closed writer. Add documents first");
+            }
+            writer.Commit();
+        }
+
+        private Analyzer GetGramAnalyzer()
             => new AnalyzerWrapperAnonymousInnerClassHelper(this, 
Analyzer.PER_FIELD_REUSE_STRATEGY);
 
         private class AnalyzerWrapperAnonymousInnerClassHelper : 
AnalyzerWrapper
@@ -268,11 +309,11 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             {
                 if (fieldName.Equals("textgrams", StringComparison.Ordinal) && 
outerInstance.minPrefixChars > 0)
                 {
-                    return new TokenStreamComponents(components.Tokenizer, 
+                    return new TokenStreamComponents(components.Tokenizer,
                         new EdgeNGramTokenFilter(
-                            outerInstance.matchVersion, 
-                            components.TokenStream, 
-                            1, 
+                            outerInstance.matchVersion,
+                            components.TokenStream,
+                            1,
                             outerInstance.minPrefixChars));
                 }
                 else
@@ -282,6 +323,27 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             }
         }
 
+        //LUCENENET specific -Support for LUCENE - 5889.
+        private void EnsureOpen()
+        {
+            if (writer != null)
+                return;
+
+            lock (syncLock)
+            {
+                if (writer == null)
+                {
+                    if (m_searcherMgr != null)
+                    {
+                        m_searcherMgr.Dispose();
+                        m_searcherMgr = null;
+                    }
+                    writer = new IndexWriter(dir, 
GetIndexWriterConfig(matchVersion, GetGramAnalyzer(), OpenMode.CREATE));
+                    m_searcherMgr = new SearcherManager(writer, true, null);
+                }
+            }
+        }
+
         /// <summary>
         /// Adds a new suggestion.  Be sure to use <see cref="Update"/>
         /// instead if you want to replace a previous suggestion.
@@ -291,6 +353,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         /// </summary>
         public virtual void Add(BytesRef text, IEnumerable<BytesRef> contexts, 
long weight, BytesRef payload)
         {
+            EnsureOpen();    //LUCENENET specific -Support for LUCENE - 5889.  
     
             writer.AddDocument(BuildDocument(text, contexts, weight, payload));
         }
 
@@ -313,13 +376,13 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             string textString = text.Utf8ToString();
             var ft = GetTextFieldType();
             var doc = new Document
-        {
-            new Field(TEXT_FIELD_NAME, textString, ft),
-            new Field("textgrams", textString, ft),
-            new StringField(EXACT_TEXT_FIELD_NAME, textString, Field.Store.NO),
-            new BinaryDocValuesField(TEXT_FIELD_NAME, text),
-            new NumericDocValuesField("weight", weight)
-        };
+            {
+                new Field(TEXT_FIELD_NAME, textString, ft),
+                new Field("textgrams", textString, ft),
+                new StringField(EXACT_TEXT_FIELD_NAME, textString, 
Field.Store.NO),
+                new BinaryDocValuesField(TEXT_FIELD_NAME, text),
+                new NumericDocValuesField("weight", weight)
+            };
             if (payload != null)
             {
                 doc.Add(new BinaryDocValuesField("payloads", payload));
@@ -344,6 +407,10 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         /// </summary>
         public virtual void Refresh()
         {
+            if (m_searcherMgr == null)
+            {
+                throw new InvalidOperationException("suggester was not built");
+            }
             m_searcherMgr.MaybeRefreshBlocking();
         }
 
@@ -789,6 +856,10 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         {
             get
             {
+                if (m_searcherMgr == null)
+                {
+                    return 0;
+                }
                 IndexSearcher searcher = m_searcherMgr.Acquire();
                 try
                 {
diff --git a/src/Lucene.Net.Suggest/Suggest/Analyzing/BlendedInfixSuggester.cs 
b/src/Lucene.Net.Suggest/Suggest/Analyzing/BlendedInfixSuggester.cs
index f95adc4..df5ea4a 100644
--- a/src/Lucene.Net.Suggest/Suggest/Analyzing/BlendedInfixSuggester.cs
+++ b/src/Lucene.Net.Suggest/Suggest/Analyzing/BlendedInfixSuggester.cs
@@ -104,8 +104,28 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         /// <param name="blenderType"> Type of blending strategy, see 
BlenderType for more precisions </param>
         /// <param name="numFactor">   Factor to multiply the number of 
searched elements before ponderate </param>
         /// <exception cref="IOException"> If there are problems opening the 
underlying Lucene index. </exception>
-        public BlendedInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, 
BlenderType blenderType, int numFactor)
-            : base(matchVersion, dir, indexAnalyzer, queryAnalyzer, 
minPrefixChars)
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. calls new 
constructor with extra param.
+        // LUCENENET TODO: Remove method at version 4.11.0. Was retained for 
perfect 4.8 compatibility
+        public BlendedInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars,
+                                     BlenderType blenderType, int numFactor)
+            : this(matchVersion, dir, indexAnalyzer, queryAnalyzer, 
minPrefixChars, blenderType, numFactor, commitOnBuild: false)
+        {
+        }
+
+        /// <summary>
+        /// Create a new instance, loading from a previously built
+        /// directory, if it exists.
+        /// </summary>
+        /// <param name="blenderType"> Type of blending strategy, see 
BlenderType for more precisions </param>
+        /// <param name="numFactor">   Factor to multiply the number of 
searched elements before ponderate </param>
+        ///  <param name="commitOnBuild"> Call commit after the index has 
finished building. This
+        ///  would persist the suggester index to disk and future instances of 
this suggester can
+        ///  use this pre-built dictionary. </param>
+        /// <exception cref="IOException"> If there are problems opening the 
underlying Lucene index. </exception>
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. (Code moved 
from other constructor to here.)
+        public BlendedInfixSuggester(LuceneVersion matchVersion, Directory 
dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars,
+                                     BlenderType blenderType, int numFactor, 
bool commitOnBuild)
+            : base(matchVersion, dir, indexAnalyzer, queryAnalyzer, 
minPrefixChars, commitOnBuild)
         {
             this.blenderType = blenderType;
             this.numFactor = numFactor;
diff --git 
a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs 
b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
index 4d76f77..6e3da5a 100644
--- 
a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
+++ 
b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
@@ -14,6 +14,7 @@ using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
+using static Lucene.Net.Search.Suggest.Lookup;
 using Console = Lucene.Net.Util.SystemConsole;
 using JCG = J2N.Collections.Generic;
 
@@ -50,7 +51,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             };
 
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);
+            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);     
//LUCENENET TODO: add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
             IList<Lookup.LookupResult> results = 
suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random).ToString(), 10, 
true, true);
@@ -93,14 +94,14 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             DirectoryInfo tempDir = 
CreateTempDir("AnalyzingInfixSuggesterTest");
 
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3);
+            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
3);     //LUCENENET TODO: add extra false param at version 4.11.0
             try
             {
                 suggester.Build(new InputArrayEnumerator(keys));
                 assertEquals(2, suggester.Count);
                 suggester.Dispose();
 
-                suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, 
NewFSDirectory(tempDir), a, a, 3);
+                suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, 
NewFSDirectory(tempDir), a, a, 3);                            //LUCENENET TODO: 
add extra false param at version 4.11.0
                 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);
@@ -143,7 +144,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         internal class TestHighlightAnalyzingInfixSuggester : 
AnalyzingInfixSuggester
         {
             public 
TestHighlightAnalyzingInfixSuggester(AnalyzingInfixSuggesterTest outerInstance, 
Analyzer a)
-                : base(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3)
+                : base(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3)          
                                                     //LUCENENET TODO: add 
extra false param at version 4.11.0
             {
             }
 
@@ -252,7 +253,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             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);
+            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixLength);     //LUCENENET TODO: add extra false param at version 4.11.0
             try
             {
                 suggester.Build(new InputArrayEnumerator(keys));
@@ -328,7 +329,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
                     // Make sure things still work after close and reopen:
                     suggester.Dispose();
-                    suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixLength);
+                    suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixLength);     //LUCENENET TODO: add extra false param at version 4.11.0
                 }
             }
             finally
@@ -346,7 +347,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             };
 
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);
+            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);     
//LUCENENET TODO: add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
             IList<Lookup.LookupResult> results = 
suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random).ToString(), 
10, true, true);
             assertEquals(1, results.size());
@@ -357,7 +358,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         {
             private readonly AnalyzingInfixSuggesterTest outerInstance;
             public 
TestHighlightChangeCaseAnalyzingInfixSuggester(AnalyzingInfixSuggesterTest 
outerInstance, Analyzer a)
-                : base(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3)
+                : base(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3)          
                                                     //LUCENENET TODO: add 
extra false param at version 4.11.0
             {
                 this.outerInstance = outerInstance;
             }
@@ -379,7 +380,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
true);
             IList<Lookup.LookupResult> results;
-            using (AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3))
+            using (AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3))         
         //LUCENENET TODO: add extra false param at version 4.11.0
             {
                 suggester.Build(new InputArrayEnumerator(keys));
                 results = 
suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random).ToString(), 
10, true, true);
@@ -390,7 +391,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             // Try again, but overriding addPrefixMatch to highlight
             // the entire hit:
             using (var suggester = new 
TestHighlightChangeCaseAnalyzingInfixSuggester(this, a))
-            { 
+            {
                 suggester.Build(new InputArrayEnumerator(keys));
                 results = 
suggester.DoLookup(TestUtil.StringToCharSequence("penn", Random).ToString(), 
10, true, true);
                 assertEquals(1, results.size());
@@ -443,7 +444,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         public void TestEmptyAtStart()
         {
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);
+            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);         
              //LUCENENET TODO: add extra false param at version 4.11.0         
     
             suggester.Build(new InputArrayEnumerator(new Input[0]));
             suggester.Add(new BytesRef("a penny saved is a penny earned"), 
null, 10, new BytesRef("foobaz"));
             suggester.Add(new BytesRef("lend me your ear"), null, 8, new 
BytesRef("foobar"));
@@ -481,7 +482,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
         public void TestBothExactAndPrefix()
         {
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);
+            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);     
//LUCENENET TODO: add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(new Input[0]));
             suggester.Add(new BytesRef("the pen is pretty"), null, 10, new 
BytesRef("foobaz"));
             suggester.Refresh();
@@ -607,7 +608,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                 Console.WriteLine("  minPrefixChars=" + minPrefixChars);
             }
 
-            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixChars);
+            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixChars);     //LUCENENET TODO: add extra false param at version 4.11.0
             try
             {
 
@@ -710,7 +711,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                         lookupThread.Join();
                         Assert.Null(error[0], "Unexpcted exception at retry : 
\n" + stackTraceStr(error[0]));
                         suggester.Dispose();
-                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixChars);
+                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
minPrefixChars);     //LUCENENET TODO: add extra false param at version 4.11.0
                         lookupThread = new LookupThread(this, suggester, stop, 
error);
                         lookupThread.Start();
 
@@ -917,7 +918,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             };
 
             Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
-            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);
+            using AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewDirectory(), a, a, 3);     
//LUCENENET TODO: add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
             IList<Lookup.LookupResult> results = 
suggester.DoLookup(TestUtil.StringToCharSequence("ear", Random).ToString(), 10, 
true, true);
@@ -976,6 +977,74 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             assertEquals(new BytesRef("foobaz"), results[1].Payload);
         }
 
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature.
+        [Test]
+        public void TestNRTWithParallelAdds()
+        {
+            String[] keys = new String[] { "python", "java", "c", "scala", 
"ruby", "clojure", "erlang", "go", "swift", "lisp" };
+            Analyzer a = new MockAnalyzer(Random, MockTokenizer.WHITESPACE, 
false);
+            DirectoryInfo tempDir = CreateTempDir("AIS_NRT_PERSIST_TEST");
+            AnalyzingInfixSuggester suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3, 
false);
+            Thread[] multiAddThreads = new Thread[10];
+
+            try
+            {
+                suggester.Refresh();
+                fail("Cannot call refresh on an suggester when no docs are 
added to the index");
+            }
+            catch (InvalidOperationException)
+            {
+                //Expected
+            }
+
+            for (int i = 0; i < 10; i++)
+            {
+                string key = keys[i];
+                multiAddThreads[i] = new Thread(() => IndexDocument(suggester, 
key));   // LUCENENET specific: use of closure rather than object.
+            }
+
+            for (int i = 0; i < 10; i++)
+            {
+                multiAddThreads[i].Start();
+            }
+
+            //Make sure all threads have completed indexing
+            for (int i = 0; i < 10; i++)
+            {
+                multiAddThreads[i].Join();
+            }
+
+            suggester.Refresh();
+            IList<LookupResult> results = 
suggester.DoLookup(TestUtil.StringToCharSequence("python", Random).ToString(), 
10, true, false);
+            assertEquals(1, results.size());
+            assertEquals("python", results[0].Key);
+
+            //Test if the index is getting persisted correctly and can be 
reopened.
+            suggester.Commit();
+            suggester.Dispose();
+
+            suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, 
NewFSDirectory(tempDir), a, a, 3, false);
+            results = 
suggester.DoLookup(TestUtil.StringToCharSequence("python", Random).ToString(), 
10, true, false);
+            assertEquals(1, results.size());
+            assertEquals("python", results[0].Key);
+
+            suggester.Dispose();
+        }
+
+        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. Use of method 
not class due to Thread parameter needs in .Net.
+        public void IndexDocument(AnalyzingInfixSuggester suggester, String 
key)
+        {
+            try
+            {
+                suggester.Add(new BytesRef(key), null, 10, null);
+            }
+            catch (IOException e)
+            {
+                fail("Could not build suggest dictionary correctly");
+            }
+        }
+
+
         private ISet<BytesRef> AsSet(params string[] values)
         {
             ISet<BytesRef> result = new JCG.HashSet<BytesRef>();
@@ -1006,13 +1075,13 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                     Analyzer a = new MockAnalyzer(Random, 
MockTokenizer.WHITESPACE, false);
                     if (iter == 0)
                     {
-                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3);
+                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
3);     //LUCENENET TODO: add extra false param at version 4.11.0
                         suggester.Build(new InputArrayEnumerator(keys));
                     }
                     else
                     {
                         // Test again, after close/reopen:
-                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 3);
+                        suggester = new 
AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, 
3);     //LUCENENET TODO: add extra false param at version 4.11.0
                     }
 
                     // No context provided, all results returned
diff --git 
a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/BlendedInfixSuggesterTest.cs 
b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/BlendedInfixSuggesterTest.cs
index 63ec72d..3d82edc 100644
--- 
a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/BlendedInfixSuggesterTest.cs
+++ 
b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/BlendedInfixSuggesterTest.cs
@@ -49,7 +49,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             BlendedInfixSuggester suggester = new 
BlendedInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a,
                                                                         
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
                                                                         
BlendedInfixSuggester.BlenderType.POSITION_LINEAR,
-                                                                        
BlendedInfixSuggester.DEFAULT_NUM_FACTOR);
+                                                                        
BlendedInfixSuggester.DEFAULT_NUM_FACTOR);     //LUCENENET TODO: add extra 
false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
             // we query for star wars and check that the weight
@@ -99,7 +99,8 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             // BlenderType.RECIPROCAL is using 1/(1+p) * w where w is weight 
and p the position of the word
             suggester = new BlendedInfixSuggester(TEST_VERSION_CURRENT, 
NewFSDirectory(tempDir), a, a,
-                                                  
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, 
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 1);
+                                                  
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
+                                                  
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 1);    //LUCENENET TODO: 
add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
             assertEquals(w, GetInResults(suggester, "top", pl, 1));
@@ -132,7 +133,8 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             // if factor is small, we don't get the expected element
             BlendedInfixSuggester suggester = new 
BlendedInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a,
-                                                                        
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, 
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 1);
+                                                                        
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
+                                                                        
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 1);     //LUCENENET 
TODO: add extra false param at version 4.11.0
 
             suggester.Build(new InputArrayEnumerator(keys));
 
@@ -151,7 +153,8 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             // if we increase the factor we have it
             suggester = new BlendedInfixSuggester(TEST_VERSION_CURRENT, 
NewFSDirectory(tempDir), a, a,
-                                                  
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, 
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 2);
+                                                  
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
+                                                  
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL, 2);     //LUCENENET 
TODO: add extra false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
             // we have it
@@ -184,8 +187,9 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
             // if factor is small, we don't get the expected element
             BlendedInfixSuggester suggester = new 
BlendedInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a,
-                                                                        
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, 
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL,
-                                                                        
BlendedInfixSuggester.DEFAULT_NUM_FACTOR);
+                                                                        
AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS,
+                                                                        
BlendedInfixSuggester.BlenderType.POSITION_RECIPROCAL,
+                                                                        
BlendedInfixSuggester.DEFAULT_NUM_FACTOR);     //LUCENENET TODO: add extra 
false param at version 4.11.0
             suggester.Build(new InputArrayEnumerator(keys));
 
 

Reply via email to