# Danish analyzer for Lucene.Net


<!--kg-card-begin: markdown-->
I've been looking for a **Danish analyzer** for Lucene.Net to use with Sitecore ContentSearch.
 
There exists one [in the original Java version](https://lucene.apache.org/core/6_1_0/analyzers-common/org/apache/lucene/analysis/da/DanishAnalyzer.html) of Lucene but it **hasn't been ported for Lucene.Net yet** - the .NET port of Lucene.
 
Actually that's not completely true. The Danish analyzer [seems to have been [ported](https://github.com/apache/lucenenet/blob/master/src/Lucene.Net.Analysis.Common/Analysis/Da/DanishAnalyzer.cs) in **October 2016**, but there hasn't been a new relase of Lucene.Net since [version 3.0.3](https://github.com/apache/lucenenet/releases/tag/Lucene.Net_3_0_3_RC2_final) in **October 2012**.
 
So, I decided to "create" one myself. "Create" because it's basically just combining a tokenizer, filters and the (already ported) Danish stemmer.
 
You can see the source code on [GitHub](https://github.com/Krusen/Additio.Lucene.Analyzers) where you can also [report issues](https://github.com/Krusen/Additio.Lucene.Analyzers/issues) or request new features.
 
There exists two **NuGet packages**:
 
- [Additio.Lucene.Analyzers.Danish](https://www.nuget.org/packages/Additio.Lucene.Analyzers.Danish/) with just the analyzer
- [Additio.Lucene.Analyzers.Danish.Sitecore](https://www.nuget.org/packages/Additio.Lucene.Analyzers.Danish.Sitecore/) which includes a config file to add the analyzer to the default Lucene index configuration.

## Sitecore ContentSearch
 
The NuGet package meant for **Sitecore** contains the below config file as well, to add the Danish analyzer to the **culture execution contexts** for the Danish culture `da`.
 
With this the Danish analyzer will be used when **indexing items** in language `da` and create **better terms** that should result in **better search results.**

```xml
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration>

          <analyzer>
            <param>
              <map>

                <!-- Add danish analyzer for 'da' culture -->
                <mapEntry patch:before="*[1]" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.PerExecutionContextAnalyzerMapEntry, Sitecore.ContentSearch.LuceneProvider">
                  <param hint="executionContext" type="Sitecore.ContentSearch.CultureExecutionContext, Sitecore.ContentSearch">
                    <param hint="cultureInfo" type="System.Globalization.CultureInfo, mscorlib">
                      <param hint="name">da</param>
                    </param>
                  </param>
                  <param hint="analyzer" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.DefaultPerFieldAnalyzer, Sitecore.ContentSearch.LuceneProvider">
                    <param hint="defaultAnalyzer" type="Additio.Lucene.Analyzers.DanishAnalyzer, Additio.Lucene.Analyzers">
                      <param hint="version">Lucene_30</param>
                    </param>
                  </param>
                </mapEntry>

              </map>
            </param>
          </analyzer>

        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>
```
 
Remember to also use the `CultureExecutionContext` when searching so your query get analyzed the same way as when the item's fields were indexed.

```csharp
using (var context = ContentSearchManager.GetIndex("INDEX_NAME").CreateSearchContext())
{
    var cultureExecutionContext = new CultureExecutionContext(new CultureInfo("da"));
    var query = context.GetQueryable<SearchResultItem>(cultureExecutionContext);

    // configure query and get results
}
```
 <!--kg-card-end: markdown-->
