-
Notifications
You must be signed in to change notification settings - Fork 820
/
TermsQueryRunner.cs
65 lines (58 loc) · 1.82 KB
/
TermsQueryRunner.cs
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
//-----------------------------------------------------------------------
// <copyright file="TermsQueryRunner.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Lucene.Net.Index;
using Lucene.Net.Search;
namespace Raven.Database.Queries
{
public class TermsQueryRunner
{
private readonly DocumentDatabase database;
public TermsQueryRunner(DocumentDatabase database)
{
this.database = database;
}
public ISet<string> GetTerms(string index, string field, string fromValue, int pageSize)
{
if(field == null) throw new ArgumentNullException("field");
if(index == null) throw new ArgumentNullException("index");
var result = new HashSet<string>();
IndexSearcher currentIndexSearcher;
using(database.IndexStorage.GetCurrentIndexSearcher(index, out currentIndexSearcher))
{
var termEnum = currentIndexSearcher.GetIndexReader().Terms(new Term(field, fromValue ?? string.Empty));
try
{
if (string.IsNullOrEmpty(fromValue) == false) // need to skip this value
{
while (termEnum.Term() == null || fromValue.Equals(termEnum.Term().Text()))
{
if (termEnum.Next() == false)
return result;
}
}
while (termEnum.Term() == null ||
field.Equals(termEnum.Term().Field()))
{
if (termEnum.Term() != null)
result.Add(termEnum.Term().Text());
if (result.Count >= pageSize)
break;
if (termEnum.Next() == false)
break;
}
}
finally
{
termEnum.Close();
}
}
return result;
}
}
}