/
IndexAndQueryNullTest.java
118 lines (98 loc) · 4.18 KB
/
IndexAndQueryNullTest.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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.query.nullValues;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.test.query.ProjectionToMapResultTransformer;
/**
* Tests for indexing and querying {@code null} values. See HSEARCh-115
*
* @author Hardy Ferentschik
*/
public class IndexAndQueryNullTest extends SearchTestCase {
// HSEARCH-115
public void testIndexAndSearchNull() throws Exception {
Value fooValue = new Value( "foo" );
Value nullValue = new Value( null );
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
Transaction tx = fullTextSession.beginTransaction();
session.save( fooValue );
session.save( nullValue );
tx.commit();
fullTextSession.clear();
tx = fullTextSession.beginTransaction();
searchKeywordWithExpectedNumberOfResults( fullTextSession, "foo", 1 );
searchKeywordWithExpectedNumberOfResults( fullTextSession, "__null", 1 );
tx.commit();
fullTextSession.close();
}
public void testLuceneDocumentContainsNullToken() throws Exception {
Value nullValue = new Value( null );
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
Transaction tx = fullTextSession.beginTransaction();
session.save( nullValue );
tx.commit();
fullTextSession.clear();
tx = fullTextSession.beginTransaction();
QueryParser parser = new QueryParser( getTargetLuceneVersion(), "id", SearchTestCase.standardAnalyzer );
parser.setAllowLeadingWildcard( true );
Query query = parser.parse( "*" );
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class );
fullTextQuery.setProjection(
FullTextQuery.DOCUMENT
);
fullTextQuery.setResultTransformer( new ProjectionToMapResultTransformer() );
List mappedResults = fullTextQuery.list();
assertTrue( "Wrong result size", mappedResults.size() == 1 );
Map map = (Map) mappedResults.get( 0 );
Document document = (Document) map.get( FullTextQuery.DOCUMENT );
assertNotNull( document );
String indexedNullString = document.get( "value" );
assertEquals( "The null value should be indexed as __null", "__null", indexedNullString );
tx.commit();
fullTextSession.close();
}
private void searchKeywordWithExpectedNumberOfResults(FullTextSession fullTextSession, String queryString, int expectedNumberOfResults)
throws Exception {
QueryParser parser = new QueryParser( getTargetLuceneVersion(), "value", SearchTestCase.standardAnalyzer );
Query query = parser.parse( queryString );
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class );
@SuppressWarnings("unchecked")
List<Value> valueList = fullTextQuery.list();
assertEquals( "Wrong number of results", expectedNumberOfResults, valueList.size() );
}
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Value.class,
};
}
}