forked from ravendb/ravendb
/
IndexingExtensions.cs
170 lines (155 loc) · 5.69 KB
/
IndexingExtensions.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//-----------------------------------------------------------------------
// <copyright file="IndexingExtensions.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Linq;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Database.Indexing;
using Raven.Database.Indexing.Sorting;
using Raven.Database.Server;
using Constants = Raven.Abstractions.Data.Constants;
namespace Raven.Database.Extensions
{
public static class IndexingExtensions
{
public static Analyzer CreateAnalyzerInstance(string name, string analyzerTypeAsString)
{
var analyzerType = typeof(StandardAnalyzer).Assembly.GetType(analyzerTypeAsString) ??
Type.GetType(analyzerTypeAsString);
if (analyzerType == null)
throw new InvalidOperationException("Cannot find analzyer type '" + analyzerTypeAsString + "' for field: " + name);
try
{
// try to get paramerless ctor
var ctors = analyzerType.GetConstructor(Type.EmptyTypes);
if (ctors != null)
return (Analyzer)Activator.CreateInstance(analyzerType);
ctors = analyzerType.GetConstructor(new[] {typeof (Lucene.Net.Util.Version)});
if (ctors != null)
return (Analyzer)Activator.CreateInstance(analyzerType, Lucene.Net.Util.Version.LUCENE_30);
}
catch (Exception e)
{
throw new InvalidOperationException(
"Could not create new analyzer instance '" + name + "' for field: " +
name, e);
}
throw new InvalidOperationException(
"Could not create new analyzer instance '" + name + "' for field: " + name + ". No recognizable constructor found.");
}
public static Field.Index GetIndex(this IndexDefinition self, string name, Field.Index? defaultIndex)
{
if (self.Indexes == null)
return defaultIndex ?? Field.Index.ANALYZED_NO_NORMS;
FieldIndexing value;
if (self.Indexes.TryGetValue(name, out value) == false)
{
if(self.Indexes.TryGetValue(Constants.AllFields, out value) == false)
{
string ignored;
if (self.Analyzers.TryGetValue(name, out ignored) ||
self.Analyzers.TryGetValue(Constants.AllFields, out ignored))
{
return Field.Index.ANALYZED; // if there is a custom analyzer, the value should be analyzed
}
return defaultIndex ?? Field.Index.ANALYZED_NO_NORMS;
}
}
switch (value)
{
case FieldIndexing.No:
return Field.Index.NO;
case FieldIndexing.Analyzed:
return Field.Index.ANALYZED_NO_NORMS;
case FieldIndexing.NotAnalyzed:
return Field.Index.NOT_ANALYZED_NO_NORMS;
case FieldIndexing.Default:
return defaultIndex ?? Field.Index.ANALYZED_NO_NORMS;
default:
throw new ArgumentOutOfRangeException();
}
}
public static Field.Store GetStorage(this IndexDefinition self, string name, Field.Store defaultStorage)
{
if (self.Stores == null)
return defaultStorage;
FieldStorage value;
if (self.Stores.TryGetValue(name, out value) == false)
{
// do we have a overriding default?
if (self.Stores.TryGetValue(Constants.AllFields, out value) == false)
return defaultStorage;
}
switch (value)
{
case FieldStorage.Yes:
return Field.Store.YES;
case FieldStorage.No:
return Field.Store.NO;
default:
throw new ArgumentOutOfRangeException();
}
}
public static Sort GetSort(this IndexQuery self, IndexDefinition indexDefinition)
{
if (self.SortedFields == null || self.SortedFields.Length <= 0)
return null;
var spatialQuery = self as SpatialIndexQuery;
return new Sort(self.SortedFields
.Select(sortedField =>
{
if(sortedField.Field.StartsWith(Constants.RandomFieldName))
{
var parts = sortedField.Field.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) // truly random
return new RandomSortField(Guid.NewGuid().ToString());
return new RandomSortField(parts[1]);
}
if (spatialQuery != null && sortedField.Field == Constants.DistanceFieldName)
{
var shape = SpatialIndex.ShapeReadWriter.ReadShape(spatialQuery.QueryShape);
var dsort = new SpatialDistanceFieldComparatorSource(shape.GetCenter());
return new SortField(Constants.DistanceFieldName, dsort, sortedField.Descending);
}
var sortOptions = GetSortOption(indexDefinition, sortedField.Field);
if (sortOptions == null || sortOptions == SortOptions.None)
return new SortField(sortedField.Field, CultureInfo.InvariantCulture, sortedField.Descending);
return new SortField(sortedField.Field, (int)sortOptions.Value, sortedField.Descending);
})
.ToArray());
}
public static SortOptions? GetSortOption(this IndexDefinition self, string name)
{
SortOptions value;
if (self.SortOptions.TryGetValue(name, out value))
{
return value;
}
if (self.SortOptions.TryGetValue(Constants.AllFields, out value))
return value;
if (name.EndsWith("_Range"))
{
string nameWithoutRange = name.Substring(0, name.Length - "_Range".Length);
if (self.SortOptions.TryGetValue(nameWithoutRange, out value))
return value;
if (self.SortOptions.TryGetValue(Constants.AllFields, out value))
return value;
}
if (CurrentOperationContext.Headers.Value == null)
return value;
var hint = CurrentOperationContext.Headers.Value["SortHint-" + name];
if (string.IsNullOrEmpty(hint))
return value;
Enum.TryParse(hint, true, out value);
return value;
}
}
}