-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleIndexWriter.cs
More file actions
77 lines (70 loc) · 2.57 KB
/
SampleIndexWriter.cs
File metadata and controls
77 lines (70 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using NetCoreStack.Lucene;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Version = Lucene.Net.Util.Version;
namespace NetCoreStack.Lucene.Test
{
public class SampleIndexWriter : LuceneIndexWriterFactory<SampleIndexWriter, SampleModel>
{
static SampleIndexWriter()
{
AnalyzerFactory = new AnalyzerFactoryDelegate(CustomAnalyzerFactory);
UpdateIndexForItemAction = new Action<SampleModel>(UpdateIndexForItem);
}
private static Analyzer CustomAnalyzerFactory()
{
return new StandardAnalyzer(Version.LUCENE_30);
}
private static string ReplaceTurkishCharacters(string text)
{
return
text.Replace("Ğ", "G")
.Replace("İ", "I")
.Replace("Ü", "U")
.Replace("Ö", "O")
.Replace("Ş", "S")
.Replace("Ç", "C")
.Replace("ğ", "g")
.Replace("ı", "i")
.Replace("ü", "u")
.Replace("ö", "o")
.Replace("ş", "s")
.Replace("ç", "c");
}
private static readonly List<char> TurkishCharacters = new List<char>
{
'Ğ', 'İ', 'Ü', 'Ö', 'Ş', 'Ç', 'ğ', 'ı', 'ü', 'ö', 'ş', 'ç'
};
public static Task<IEnumerable<SearchResultItem>> SearchAsync(string searchText, int n)
{
return Task.Run(() =>
{
var searcher = new IndexSearcher(LuceneDirectory, true);
var parser = new QueryParser(Version.LUCENE_30, "text", CustomAnalyzerFactory());
var query = parser.Parse(searchText);
ScoreDoc[] hits = searcher.Search(query, n).ScoreDocs;
return hits.Select(d =>
{
var document = searcher.Doc(d.Doc);
return new SearchResultItem
{
Id = int.Parse(document.Get("id")),
SearchScore = d.Score
};
});
});
}
public static void UpdateIndexForItem(SampleModel item)
{
Writer.UpdateDocument(new Term("id", item.Id.ToString(CultureInfo.InvariantCulture)), item.ToLuceneDocument());
}
}
}