-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LuceneDocumentStructureTest.java
188 lines (161 loc) · 6.64 KB
/
LuceneDocumentStructureTest.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
/*
* 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.index;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.junit.Test;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertEquals;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.NODE_ID_KEY;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.ValueEncoding.Array;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.ValueEncoding.Bool;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.ValueEncoding.Number;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.ValueEncoding.String;
public class LuceneDocumentStructureTest
{
@Test
public void tooLongStringShouldBeSkipped()
{
String string = RandomStringUtils.randomAscii( 358749 );
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, string );
assertNull( document.getField( String.key() ) );
}
@Test
public void tooLongArrayShouldBeSkipped()
{
byte[] bytes = RandomStringUtils.randomAscii( IndexWriter.MAX_TERM_LENGTH + 10).getBytes();
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, bytes );
assertNull( document.getField( Array.key() ) );
}
@Test
public void stringWithMaximumLengthShouldBeAllowed()
{
String longestString = RandomStringUtils.randomAscii( IndexWriter.MAX_TERM_LENGTH );
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, longestString );
assertEquals( longestString, document.getField( String.key() ).stringValue() );
}
@Test
public void shouldBuildDocumentRepresentingStringProperty() throws Exception
{
// given
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, "hello" );
// then
assertEquals( "123", document.get( NODE_ID_KEY ) );
assertEquals( "hello", document.get( String.key() ) );
}
@Test
public void shouldBuildDocumentRepresentingBoolProperty() throws Exception
{
// given
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, true );
// then
assertEquals( "123", document.get( NODE_ID_KEY ) );
assertEquals( "true", document.get( Bool.key() ) );
}
@Test
public void shouldBuildDocumentRepresentingNumberProperty() throws Exception
{
// given
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, 12 );
// then
assertEquals( "123", document.get( NODE_ID_KEY ) );
assertEquals( 12.0, document.getField( Number.key() ).numericValue().doubleValue() );
}
@Test
public void shouldBuildDocumentRepresentingArrayProperty() throws Exception
{
// given
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, new Integer[]{1, 2, 3} );
// then
assertEquals( "123", document.get( NODE_ID_KEY ) );
assertEquals( "D1.0|2.0|3.0|", document.get( Array.key() ) );
}
@Test
public void shouldBuildQueryRepresentingBoolProperty() throws Exception
{
// given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( true );
// then
assertEquals( "true", query.getTerm().text() );
}
@Test
public void shouldBuildQueryRepresentingStringProperty() throws Exception
{
// given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( "Characters" );
// then
assertEquals( "Characters", query.getTerm().text() );
}
@SuppressWarnings( "unchecked" )
@Test
public void shouldBuildQueryRepresentingNumberProperty() throws Exception
{
// given
NumericRangeQuery<Double> query = (NumericRangeQuery<Double>) LuceneDocumentStructure.newSeekQuery( 12 );
// then
assertEquals( 12.0, query.getMin() );
assertEquals( 12.0, query.getMax() );
}
@Test
public void shouldBuildQueryRepresentingArrayProperty() throws Exception
{
// given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( new Integer[]{1, 2, 3} );
// then
assertEquals( "D1.0|2.0|3.0|", query.getTerm().text() );
}
@Test
public void shouldBuildRangeSeekByNumberQueryForStrings() throws Exception
{
// given
NumericRangeQuery<Double> query = LuceneDocumentStructure.newInclusiveNumericRangeSeekQuery( 12.0d, null );
// then
assertEquals( "number", query.getField() );
assertEquals( 12.0, query.getMin() );
assertEquals( true, query.includesMin() );
assertEquals( null, query.getMax() );
assertEquals( true, query.includesMax() );
}
@Test
public void shouldBuildRangeSeekByStringQueryForStrings() throws Exception
{
// given
TermRangeQuery query = (TermRangeQuery) LuceneDocumentStructure.newRangeSeekByStringQuery( "foo", false, null, true );
// then
assertEquals( "string", query.getField() );
assertEquals( "foo", query.getLowerTerm().utf8ToString() );
assertEquals( false, query.includesLower() );
assertEquals( null, query.getUpperTerm() );
assertEquals( true, query.includesUpper() );
}
@Test
public void shouldBuildRangeSeekByPrefixQueryForStrings() throws Exception
{
// given
PrefixQuery query = LuceneDocumentStructure.newRangeSeekByPrefixQuery( "Prefix" );
// then
assertEquals( "Prefix", query.getPrefix().text() );
}
}