-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LuceneDocumentStructure.java
336 lines (294 loc) · 12.1 KB
/
LuceneDocumentStructure.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.schema;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.FilteredTermsEnum;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.StringHelper;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import org.neo4j.unsafe.impl.internal.dragons.FeatureToggles;
import static org.apache.lucene.document.Field.Store.YES;
public class LuceneDocumentStructure
{
private static final boolean USE_LUCENE_STANDARD_PREFIX_QUERY =
FeatureToggles.flag( LuceneDocumentStructure.class, "lucene.standard.prefix.query", false );
public static final String NODE_ID_KEY = "id";
// Absolute hard maximum length for a term, in bytes once
// encoded as UTF8. If a term arrives from the analyzer
// longer than this length, an IllegalArgumentException
// when lucene writer trying to add or update document
private static final int MAX_FIELD_LENGTH = IndexWriter.MAX_TERM_LENGTH;
private static final ThreadLocal<DocWithId> perThreadDocument = new ThreadLocal<DocWithId>()
{
@Override
protected DocWithId initialValue()
{
return new DocWithId( NODE_ID_KEY );
}
};
private LuceneDocumentStructure()
{
}
private static DocWithId reuseDocument( long nodeId )
{
DocWithId doc = perThreadDocument.get();
doc.setId( nodeId );
return doc;
}
public static Document documentRepresentingProperty( long nodeId, Object value )
{
DocWithId document = reuseDocument( nodeId );
document.setValue( ValueEncoding.forValue( value ), value );
return document.document;
}
public static String encodedStringValue( Object value )
{
ValueEncoding encoding = ValueEncoding.forValue( value );
Field field = encoding.encodeField( value );
return field.stringValue();
}
public static MatchAllDocsQuery newScanQuery()
{
return new MatchAllDocsQuery();
}
public static Query newSeekQuery( Object value )
{
ValueEncoding encoding = ValueEncoding.forValue( value );
return encoding.encodeQuery( value );
}
/**
* Range queries are always inclusive, in order to do exclusive range queries the result must be filtered after the
* fact. The reason we can't do inclusive range queries is that longs are coerced to doubles in the index.
*/
public static NumericRangeQuery<Double> newInclusiveNumericRangeSeekQuery( Number lower, Number upper )
{
Double min = lower != null ? lower.doubleValue() : null;
Double max = upper != null ? upper.doubleValue() : null;
return NumericRangeQuery.newDoubleRange( ValueEncoding.Number.key(), min, max, true, true );
}
public static Query newRangeSeekByStringQuery( String lower, boolean includeLower,
String upper, boolean includeUpper )
{
boolean includeLowerBoundary = StringUtils.EMPTY.equals( lower ) || includeLower;
boolean includeUpperBoundary = StringUtils.EMPTY.equals( upper ) || includeUpper;
TermRangeQuery termRangeQuery = TermRangeQuery.newStringRange( ValueEncoding.String.key(), lower, upper,
includeLowerBoundary, includeUpperBoundary );
if ( (includeLowerBoundary != includeLower) || (includeUpperBoundary != includeUpper) )
{
BooleanQuery.Builder builder = new BooleanQuery.Builder();
if ( includeLowerBoundary != includeLower )
{
builder.add( new TermQuery( new Term( ValueEncoding.String.key(), lower ) ), BooleanClause.Occur
.MUST_NOT );
}
if ( includeUpperBoundary != includeUpper )
{
builder.add( new TermQuery( new Term( ValueEncoding.String.key(), upper ) ), BooleanClause.Occur
.MUST_NOT );
}
builder.add( termRangeQuery, BooleanClause.Occur.SHOULD );
return builder.build();
}
return new ConstantScoreQuery( termRangeQuery );
}
public static Query newWildCardStringQuery( String searchFor )
{
String searchTerm = QueryParser.escape( searchFor );
Term term = new Term( ValueEncoding.String.key(), "*" + searchTerm + "*" );
return new WildcardQuery( term );
}
public static Query newRangeSeekByPrefixQuery( String prefix )
{
Term term = new Term( ValueEncoding.String.key(), prefix );
MultiTermQuery prefixQuery = USE_LUCENE_STANDARD_PREFIX_QUERY ? new PrefixQuery( term ) :
new PrefixMultiTermsQuery( term );
return new ConstantScoreQuery( prefixQuery );
}
public static Term newTermForChangeOrRemove( long nodeId )
{
return new Term( NODE_ID_KEY, "" + nodeId );
}
public static long getNodeId( Document from )
{
return Long.parseLong( from.get( NODE_ID_KEY ) );
}
/**
* Filters the given {@link Terms terms} to include only terms that were created using fields from
* {@link ValueEncoding#encodeField(Object)}. Internal lucene terms like those created for indexing numeric values
* (see javadoc for {@link NumericRangeQuery} class) are skipped. In other words this method returns
* {@link TermsEnum} over all terms for the given field that were created using {@link ValueEncoding}.
*
* @param terms the terms to be filtered
* @param fieldKey the corresponding {@link ValueEncoding#key() field key}
* @return terms enum over all inserted terms
* @throws IOException if it is not possible to obtain {@link TermsEnum}
* @see NumericRangeQuery
* @see org.apache.lucene.analysis.NumericTokenStream
* @see NumericUtils#PRECISION_STEP_DEFAULT
* @see NumericUtils#filterPrefixCodedLongs(TermsEnum)
*/
public static TermsEnum originalTerms( Terms terms, String fieldKey ) throws IOException
{
TermsEnum termsEnum = terms.iterator();
return ValueEncoding.forKey( fieldKey ) == ValueEncoding.Number
? NumericUtils.filterPrefixCodedLongs( termsEnum )
: termsEnum;
}
/**
* Simple implementation of prefix query that mimics old lucene way of handling prefix queries.
* According to benchmarks this implementation is faster then
* {@link org.apache.lucene.search.PhraseQuery} because we do not construct automaton which is
* extremely expensive.
*/
private static class PrefixMultiTermsQuery extends MultiTermQuery
{
private Term term;
PrefixMultiTermsQuery( Term term )
{
super(term.field());
this.term = term;
}
@Override
protected TermsEnum getTermsEnum( Terms terms, AttributeSource atts ) throws IOException
{
return term.bytes().length == 0 ? terms.iterator() : new PrefixTermsEnum( terms.iterator(), term.bytes() );
}
@Override
public String toString( String field )
{
return getClass().getSimpleName() + ", term:" + term + ", field:" + field;
}
private class PrefixTermsEnum extends FilteredTermsEnum
{
private BytesRef prefix;
PrefixTermsEnum( TermsEnum termEnum, BytesRef prefix )
{
super( termEnum );
this.prefix = prefix;
setInitialSeekTerm( this.prefix );
}
@Override
protected AcceptStatus accept( BytesRef term ) throws IOException
{
return StringHelper.startsWith( term, prefix ) ? AcceptStatus.YES : AcceptStatus.END;
}
}
}
private static class DocWithId
{
private final Document document;
private final String idFieldName;
private final Field idField;
private final Field idValueField;
private final Map<ValueEncoding,Field> valueFields = new EnumMap<>( ValueEncoding.class );
private DocWithId( String idFieldName )
{
this.idFieldName = idFieldName;
idField = new StringField( idFieldName, "", YES );
idValueField = new NumericDocValuesField( idFieldName, 0L );
document = new Document();
document.add( idField );
document.add( idValueField );
}
private void setId( long id )
{
idField.setStringValue( "" + id );
idValueField.setLongValue( id );
}
private void setValue( ValueEncoding encoding, Object value )
{
removeAllValueFields();
Field reusableField = getFieldWithValue( encoding, value );
if ( isArrayOrString( reusableField ) )
{
if ( isShorterThenMaximum( reusableField ) )
{
document.add( reusableField );
}
}
else
{
document.add( reusableField );
}
}
private boolean isShorterThenMaximum( Field reusableField )
{
return reusableField.stringValue().getBytes().length <= MAX_FIELD_LENGTH;
}
private boolean isArrayOrString( Field reusableField )
{
return ValueEncoding.Array.key().equals( reusableField.name() ) ||
ValueEncoding.String.key().equals( reusableField.name() );
}
private void removeAllValueFields()
{
Iterator<IndexableField> it = document.getFields().iterator();
while ( it.hasNext() )
{
IndexableField field = it.next();
String fieldName = field.name();
if ( !fieldName.equals( idFieldName ) )
{
it.remove();
}
}
}
private Field getFieldWithValue( ValueEncoding encoding, Object value )
{
Field reusableField = valueFields.get( encoding );
if ( reusableField == null )
{
reusableField = encoding.encodeField( value );
valueFields.put( encoding, reusableField );
}
else
{
encoding.setFieldValue( value, reusableField );
}
return reusableField;
}
}
}