title: RavenDb - How to use Custom Analyzer (NGram)
body: <p>i'm learning ravenDb, and i'm trying to use it's full power with lucene's custom analyzers.</p>
  
  <p>according to the <a href="https://ravendb.net/docs/article-page/3.5/csharp/indexes/using-analyzers" rel="nofollow noreferrer">docs</a> - </p>
  
  <blockquote>
    <p>The analyzer you are referencing to has to be available to the RavenDB server instance. When using analyzers that do not come with the default Lucene.NET distribution, you need to drop all the necessary DLLs into the "Analyzers" folder of the RavenDB server directory, and use their fully qualified type name (including the assembly name).</p>
  </blockquote>
  
  <p>seems pretty simple, even too simple but i tried with no luck.
  i used this code (CustomAnalyzers project) to implement NGramAnalyzer (and filter) in CustomAnalyzer :</p>
  
  <p>[This specific implementation isn't SO important, for now i'm trying to use any custom analyzer, and proceed. I included it anyway if it will help.]</p>
  
  <pre><code>    public class NGramTokenFilter : TokenFilter
  {
      public static int DEFAULT_MIN_NGRAM_SIZE = 1;
      public static int DEFAULT_MAX_NGRAM_SIZE = 2;
      private int minGram, maxGram;
      private char[] curTermBuffer;
      private int curTermLength;
      private int curGramSize;
      private int curPos;
      private int tokStart;
      private TermAttribute termAtt;
      private OffsetAttribute offsetAtt;
      public NGramTokenFilter(TokenStream input, int minGram, int maxGram)
          : base(input)
      {
  
          if (minGram &lt; 1)
          {
              throw new System.ArgumentException("minGram must be greater than zero");
          }
          if (minGram &gt; maxGram)
          {
              throw new System.ArgumentException("minGram must not be greater than maxGram");
          }
          this.minGram = minGram;
          this.maxGram = maxGram;
  
          this.termAtt = AddAttribute&lt;TermAttribute&gt;();
          this.offsetAtt = AddAttribute&lt;OffsetAttribute&gt;();
      }
  
      public NGramTokenFilter(TokenStream input)
          : this(input, DEFAULT_MIN_NGRAM_SIZE, DEFAULT_MAX_NGRAM_SIZE)
      {
      }
  
      public override bool IncrementToken()
      {
          while (true)
          {
              if (curTermBuffer == null)
              {
                  if (!input.IncrementToken())
                  {
                      return false;
                  }
                  else
                  {
                      curTermBuffer = (char[])termAtt.TermBuffer().Clone();
                      curTermLength = termAtt.TermLength();
                      curGramSize = minGram;
                      curPos = 0;
                      tokStart = offsetAtt.StartOffset;
                  }
              }
              while (curGramSize &lt;= maxGram)
              {
                  while (curPos + curGramSize &lt;= curTermLength)
                  {     // while there is input
                      ClearAttributes();
                      termAtt.SetTermBuffer(curTermBuffer, curPos, curGramSize);
                      offsetAtt.SetOffset(tokStart + curPos, tokStart + curPos + curGramSize);
                      curPos++;
                      return true;
                  }
                  curGramSize++;                         // increase n-gram size
                  curPos = 0;
              }
              curTermBuffer = null;
          }
      }
      public override void Reset()
      {
          base.Reset();
          curTermBuffer = null;
      }
  }
  public class NGramAnalyzer : Analyzer
  {
      public override TokenStream TokenStream(string fieldName, TextReader reader)
      {
          var tokenizer = new StandardTokenizer(Version.LUCENE_29, reader) { MaxTokenLength = 255 };
          TokenStream filter = new StandardFilter(tokenizer);
          filter = new LowerCaseFilter(filter);
          filter = new StopFilter(false, filter, StandardAnalyzer.STOP_WORDS_SET);
          return new NGramTokenFilter(filter, 2, 6);
      }
  }
  </code></pre>
  
  <p>And added the dlls(all the dlls from the class library) to the Analyzers directory (the Analyzers wasn't there so i added new folder, couldn't find any other analyzers folder...)</p>
  
  <p><a href="https://i.stack.imgur.com/sepav.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sepav.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/e9lud.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e9lud.jpg" alt="enter image description here"></a></p>
  
  <p>in another project (which is referencing the 'CustomAnalyzers' Project) i'm trying to build an index :</p>
  
  <pre><code>public class NGramIndex : AbstractIndexCreationTask&lt;Book&gt;
  {
      public NGramIndex()
      {
          Map = books =&gt; from book in books
                          select new
                          {
                              book.Body
                          };
  
          Indexes.Add(x =&gt; x.Body, FieldIndexing.Analyzed);
          Analyzers.Add(x =&gt; x.Body, typeof(NGramAnalyzer).FullName);
      }
  }
  </code></pre>
  
  <p>when i run this code </p>
  
  <pre><code>var store = new DocumentStore { Url = "MY_URL", DefaultDatabase = "MY_DB" }.Initialize();
          new NGramIndex().Execute(store);
  </code></pre>
  
  <p>i get this exception - </p>
  
  <blockquote>
    <p>'Raven.Abstractions.Exceptions.IndexCompilationException' occurred in mscorlib.dll but was not handled in user code
    Additional information: Cannot find analyzer type 'CustomAnalyzers.NGramAnalyzer, CustomAnalyzers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' for field: Body</p>
  </blockquote>
  
  <p>i tried also using 'AssemblyQualifiedName' or hard code the full name.
  i looked in these stackoverflow questinos :
  <a href="https://stackoverflow.com/questions/44564094/custom-analyzer-for-ravendb">1</a>
  <a href="https://stackoverflow.com/questions/19502900/ravendb-how-do-i-find-use-custom-analyzers-when-in-embedded-runinmemory-mode">2</a>
  and couldn't find an answer.</p>
  
  <p>and <a href="https://groups.google.com/forum/#!topic/ravendb/n1hYPOKAJxs" rel="nofollow noreferrer">this</a> and <a href="https://groups.google.com/forum/#!topic/ravendb/dyEidpm-LYE" rel="nofollow noreferrer">this</a>
  and tried to restart the ravendb.</p>
  
  <p>please show how you used custom analyzer in ravendb.
  thx.</p>