1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import static org.junit.Assert.assertNotNull;
|
13
|
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.IOException;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Arrays;
|
18
|
import java.util.EnumSet;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.apache.commons.lang.RandomStringUtils;
|
26
|
import org.apache.log4j.Level;
|
27
|
import org.apache.log4j.Logger;
|
28
|
import org.apache.lucene.document.Document;
|
29
|
import org.apache.lucene.index.CorruptIndexException;
|
30
|
import org.apache.lucene.queryparser.classic.ParseException;
|
31
|
import org.junit.Assert;
|
32
|
import org.junit.Before;
|
33
|
import org.junit.Ignore;
|
34
|
import org.junit.Test;
|
35
|
import org.unitils.dbunit.annotation.DataSet;
|
36
|
import org.unitils.spring.annotation.SpringBeanByType;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
|
39
|
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
|
40
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
41
|
import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
|
42
|
import eu.etaxonomy.cdm.api.service.search.LuceneMultiSearchException;
|
43
|
import eu.etaxonomy.cdm.api.service.search.SearchResult;
|
44
|
import eu.etaxonomy.cdm.common.UTF8;
|
45
|
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
|
46
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
47
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
48
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
49
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
50
|
import eu.etaxonomy.cdm.model.common.Language;
|
51
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
52
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
53
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
54
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
55
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
56
|
import eu.etaxonomy.cdm.model.description.Feature;
|
57
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
58
|
import eu.etaxonomy.cdm.model.description.State;
|
59
|
import eu.etaxonomy.cdm.model.description.StateData;
|
60
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
61
|
import eu.etaxonomy.cdm.model.description.TextData;
|
62
|
import eu.etaxonomy.cdm.model.location.Country;
|
63
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
64
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
65
|
import eu.etaxonomy.cdm.model.name.Rank;
|
66
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
67
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
68
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
69
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
70
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
71
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
72
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
73
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
74
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
75
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
76
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
77
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
78
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
79
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
80
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
81
|
|
82
|
/**
|
83
|
* @author a.babadshanjan, a.kohlbecker
|
84
|
* @created 04.02.2009
|
85
|
*/
|
86
|
public class TaxonServiceSearchTest extends CdmTransactionalIntegrationTest {
|
87
|
|
88
|
private static final String ABIES_BALSAMEA_UUID = "f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8";
|
89
|
|
90
|
private static final String ABIES_ALBA_UUID = "7dbd5810-a3e5-44b6-b563-25152b8867f4";
|
91
|
|
92
|
private static final String CLASSIFICATION_UUID = "2a5ceebb-4830-4524-b330-78461bf8cb6b";
|
93
|
|
94
|
private static final String CLASSIFICATION_ALT_UUID = "d7c741e3-ae9e-4a7d-a566-9e3a7a0b51ce";
|
95
|
|
96
|
private static final String D_ABIES_BALSAMEA_UUID = "900108d8-e6ce-495e-b32e-7aad3099135e";
|
97
|
|
98
|
private static final String D_ABIES_ALBA_UUID = "ec8bba03-d993-4c85-8472-18b14942464b";
|
99
|
|
100
|
private static final String D_ABIES_KAWAKAMII_SEC_KOMAROV_UUID = "e9d8c2fd-6409-46d5-9c2e-14a2bbb1b2b1";
|
101
|
|
102
|
private static final int NUM_OF_NEW_RADOM_ENTITIES = 1000;
|
103
|
|
104
|
private static Logger logger = Logger.getLogger(TaxonServiceSearchTest.class);
|
105
|
|
106
|
|
107
|
|
108
|
@SpringBeanByType
|
109
|
private ITaxonService taxonService;
|
110
|
@SpringBeanByType
|
111
|
private ITermService termService;
|
112
|
@SpringBeanByType
|
113
|
private IClassificationService classificationService;
|
114
|
@SpringBeanByType
|
115
|
private IReferenceService referenceService;
|
116
|
@SpringBeanByType
|
117
|
private IDescriptionService descriptionService;
|
118
|
@SpringBeanByType
|
119
|
private INameService nameService;
|
120
|
@SpringBeanByType
|
121
|
private ICdmMassIndexer indexer;
|
122
|
|
123
|
@SpringBeanByType
|
124
|
private ITaxonNodeService nodeService;
|
125
|
|
126
|
private static final int BENCHMARK_ROUNDS = 300;
|
127
|
|
128
|
private Set<Class<? extends CdmBase>> typesToIndex = null;
|
129
|
|
130
|
private NamedArea germany;
|
131
|
private NamedArea france ;
|
132
|
private NamedArea russia ;
|
133
|
private NamedArea canada ;
|
134
|
|
135
|
/**
|
136
|
* @throws java.lang.Exception
|
137
|
*/
|
138
|
@Before
|
139
|
public void setUp() throws Exception {
|
140
|
typesToIndex = new HashSet<Class<? extends CdmBase>>();
|
141
|
typesToIndex.add(DescriptionElementBase.class);
|
142
|
typesToIndex.add(TaxonBase.class);
|
143
|
typesToIndex.add(TaxonRelationship.class);
|
144
|
|
145
|
germany = Country.GERMANY();
|
146
|
france = Country.FRANCEFRENCHREPUBLIC();
|
147
|
russia = Country.RUSSIANFEDERATION();
|
148
|
canada = Country.CANADA();
|
149
|
|
150
|
|
151
|
}
|
152
|
|
153
|
@Test
|
154
|
public void testDbUnitUsageTest() throws Exception {
|
155
|
assertNotNull("taxonService should exist", taxonService);
|
156
|
assertNotNull("nameService should exist", nameService);
|
157
|
}
|
158
|
|
159
|
|
160
|
@SuppressWarnings("rawtypes")
|
161
|
@Test
|
162
|
@DataSet
|
163
|
public final void testPurgeAndReindex() throws CorruptIndexException, IOException, ParseException {
|
164
|
|
165
|
refreshLuceneIndex();
|
166
|
|
167
|
Pager<SearchResult<TaxonBase>> pager;
|
168
|
|
169
|
pager = taxonService.findByFullText(null, "Abies", null, null, true, null, null, null, null); // --> 8
|
170
|
Assert.assertEquals("Expecting 8 entities", 8, pager.getCount().intValue());
|
171
|
|
172
|
indexer.purge(null);
|
173
|
commitAndStartNewTransaction(null);
|
174
|
|
175
|
pager = taxonService.findByFullText(null, "Abies", null, null, true, null, null, null, null); // --> 0
|
176
|
Assert.assertEquals("Expecting no entities since the index has been purged", 0, pager.getCount().intValue());
|
177
|
|
178
|
indexer.reindex(indexer.indexedClasses(), null);
|
179
|
commitAndStartNewTransaction(null);
|
180
|
|
181
|
pager = taxonService.findByFullText(null, "Abies", null, null, true, null, null, null, null); // --> 8
|
182
|
Assert.assertEquals("Expecting 8 entities", 8, pager.getCount().intValue());
|
183
|
}
|
184
|
|
185
|
|
186
|
@SuppressWarnings("rawtypes")
|
187
|
@Test
|
188
|
@DataSet
|
189
|
public final void testFindByDescriptionElementFullText_CommonName() throws CorruptIndexException, IOException,
|
190
|
ParseException {
|
191
|
|
192
|
refreshLuceneIndex();
|
193
|
|
194
|
Pager<SearchResult<TaxonBase>> pager;
|
195
|
|
196
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Wei"+UTF8.SHARP_S+"tanne", null, null, null,
|
197
|
false, null, null, null, null);
|
198
|
Assert.assertEquals("Expecting one entity when searching for CommonTaxonName", 1,
|
199
|
pager.getCount().intValue());
|
200
|
|
201
|
// the description containing the Nulltanne has no taxon attached,
|
202
|
// taxon.id = null
|
203
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Nulltanne", null, null, null,
|
204
|
false, null, null, null, null);
|
205
|
Assert.assertEquals("Expecting no entity when searching for 'Nulltanne' ", 0, pager.getCount().intValue());
|
206
|
|
207
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Wei"+UTF8.SHARP_S+"tanne", null, null,
|
208
|
Arrays.asList(new Language[] { Language.GERMAN() }), false, null, null, null, null);
|
209
|
Assert.assertEquals("Expecting one entity when searching in German", 1, pager.getCount().intValue());
|
210
|
|
211
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Wei"+UTF8.SHARP_S+"tanne", null, null,
|
212
|
Arrays.asList(new Language[] { Language.RUSSIAN() }), false, null, null, null, null);
|
213
|
Assert.assertEquals("Expecting no entity when searching in Russian", 0, pager.getCount().intValue());
|
214
|
|
215
|
}
|
216
|
|
217
|
@SuppressWarnings("rawtypes")
|
218
|
@Test
|
219
|
@DataSet
|
220
|
public final void testFindByDescriptionElementFullText_Distribution() throws CorruptIndexException, IOException, ParseException {
|
221
|
|
222
|
refreshLuceneIndex();
|
223
|
|
224
|
Pager<SearchResult<TaxonBase>> pager;
|
225
|
// by Area
|
226
|
pager = taxonService.findByDescriptionElementFullText(null, "Canada", null, null, null, false, null, null, null, null);
|
227
|
Assert.assertEquals("Expecting one entity when searching for arae 'Canada'", 1, pager.getCount().intValue());
|
228
|
// by Status
|
229
|
pager = taxonService.findByDescriptionElementFullText(null, "present", null, null, null, false, null, null, null, null);
|
230
|
Assert.assertEquals("Expecting one entity when searching for status 'present'", 1, pager.getCount().intValue());
|
231
|
}
|
232
|
|
233
|
@SuppressWarnings("rawtypes")
|
234
|
@Test
|
235
|
@DataSet
|
236
|
public final void testFindByDescriptionElementFullText_wildcard() throws CorruptIndexException, IOException, ParseException {
|
237
|
|
238
|
refreshLuceneIndex();
|
239
|
|
240
|
Pager<SearchResult<TaxonBase>> pager;
|
241
|
|
242
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Wei"+UTF8.SHARP_S+"*", null, null, null, false, null, null, null, null);
|
243
|
Assert.assertEquals("Expecting one entity when searching for CommonTaxonName", 1, pager.getCount().intValue());
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Regression test for #3113 (hibernate search: wildcard query can cause BooleanQuery$TooManyClauses: maxClauseCount is set to 1024)
|
248
|
*
|
249
|
* @throws CorruptIndexException
|
250
|
* @throws IOException
|
251
|
* @throws ParseException
|
252
|
*/
|
253
|
@SuppressWarnings("rawtypes")
|
254
|
@Test
|
255
|
@DataSet
|
256
|
public final void testFindByDescriptionElementFullText_TooManyClauses() throws CorruptIndexException, IOException, ParseException {
|
257
|
|
258
|
// generate 1024 terms to reproduce the bug
|
259
|
TaxonDescription description = (TaxonDescription) descriptionService.find(UUID.fromString(D_ABIES_ALBA_UUID));
|
260
|
Set<String> uniqueRandomStrs = new HashSet<String>(1024);
|
261
|
while(uniqueRandomStrs.size() < 1024){
|
262
|
uniqueRandomStrs.add(RandomStringUtils.random(10, true, false));
|
263
|
}
|
264
|
for(String rndStr: uniqueRandomStrs){
|
265
|
description.addElement(CommonTaxonName.NewInstance("Rot" + rndStr, Language.DEFAULT()));
|
266
|
}
|
267
|
descriptionService.saveOrUpdate(description);
|
268
|
commitAndStartNewTransaction(null);
|
269
|
|
270
|
refreshLuceneIndex();
|
271
|
|
272
|
Pager<SearchResult<TaxonBase>> pager;
|
273
|
|
274
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Rot*", null, null, null, false, null, null, null, null);
|
275
|
Assert.assertEquals("Expecting all 1024 entities grouped into one SearchResult item when searching for Rot*", 1, pager.getCount().intValue());
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* Regression test for #3116 (fulltext search: always only one page of results)
|
280
|
*
|
281
|
* @throws CorruptIndexException
|
282
|
* @throws IOException
|
283
|
* @throws ParseException
|
284
|
*/
|
285
|
@SuppressWarnings("rawtypes")
|
286
|
@Test
|
287
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
288
|
@Ignore
|
289
|
public final void testFullText_Paging() throws CorruptIndexException, IOException, ParseException {
|
290
|
|
291
|
Reference sec = ReferenceFactory.newDatabase();
|
292
|
referenceService.save(sec);
|
293
|
|
294
|
Set<String> uniqueRandomStrs = new HashSet<String>(1024);
|
295
|
int numOfItems = 100;
|
296
|
while(uniqueRandomStrs.size() < numOfItems){
|
297
|
uniqueRandomStrs.add(RandomStringUtils.random(5, true, false));
|
298
|
}
|
299
|
|
300
|
for(String rndStr: uniqueRandomStrs){
|
301
|
|
302
|
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SERIES()), sec);
|
303
|
taxon.setTitleCache("Tax" + rndStr, true);
|
304
|
taxonService.save(taxon);
|
305
|
|
306
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
307
|
description.addElement(CommonTaxonName.NewInstance("Rot" + rndStr, Language.DEFAULT()));
|
308
|
descriptionService.saveOrUpdate(description);
|
309
|
}
|
310
|
|
311
|
commitAndStartNewTransaction(new String[]{"TAXONBASE"});
|
312
|
refreshLuceneIndex();
|
313
|
|
314
|
int pageSize = 10;
|
315
|
|
316
|
Pager<SearchResult<TaxonBase>> pager;
|
317
|
|
318
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Rot*", null, null, null, false, pageSize, null, null, null);
|
319
|
Assert.assertEquals("unexpeted number of pages", Integer.valueOf(numOfItems / pageSize), pager.getPagesAvailable());
|
320
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Rot*", null, null, null, false, pageSize, 9, null, null);
|
321
|
Assert.assertNotNull("last page must have records", pager.getRecords());
|
322
|
Assert.assertNotNull("last item on last page must exist", pager.getRecords().get(0));
|
323
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Rot*", null, null, null, false, pageSize, 10, null, null);
|
324
|
Assert.assertNotNull("last page + 1 must not have any records", pager.getRecords());
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* test for max score and sort by score of hit groups
|
329
|
* with all matches per taxon in a single TextData element
|
330
|
* see {@link #testFullText_ScoreAndOrder_2()} for the complement
|
331
|
* test with matches in multiple TextData per taxon
|
332
|
*
|
333
|
* @throws CorruptIndexException
|
334
|
* @throws IOException
|
335
|
* @throws ParseException
|
336
|
*/
|
337
|
@SuppressWarnings("rawtypes")
|
338
|
@Test
|
339
|
@DataSet
|
340
|
@Ignore // test fails, maybe the assumptions made here are not compatible with the lucene scoring mechanism see http://lucene.apache.org/core/3_6_1/scoring.html
|
341
|
public final void testFullText_ScoreAndOrder_1() throws CorruptIndexException, IOException, ParseException {
|
342
|
|
343
|
int numOfTaxa = 3;
|
344
|
|
345
|
UUID[] taxonUuids = new UUID[numOfTaxa];
|
346
|
StringBuilder text = new StringBuilder();
|
347
|
|
348
|
for(int i = 0; i < numOfTaxa; i++){
|
349
|
|
350
|
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(null), null);
|
351
|
taxon.setTitleCache("Taxon_" + i, true);
|
352
|
taxonUuids[i] = taxon.getUuid();
|
353
|
taxonService.save(taxon);
|
354
|
|
355
|
text.append(" ").append("Rot");
|
356
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
357
|
description.addElement(TextData.NewInstance(text.toString(), Language.DEFAULT(), null));
|
358
|
descriptionService.saveOrUpdate(description);
|
359
|
}
|
360
|
|
361
|
commitAndStartNewTransaction(null);
|
362
|
refreshLuceneIndex();
|
363
|
|
364
|
Pager<SearchResult<TaxonBase>> pager;
|
365
|
|
366
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Rot", null, null, null, false, null, null, null, null);
|
367
|
for(int i = 0; i < numOfTaxa; i++){
|
368
|
Assert.assertEquals("taxa should be orderd by relevance (= score)", taxonUuids[numOfTaxa - i - 1], pager.getRecords().get(i).getEntity().getUuid());
|
369
|
}
|
370
|
Assert.assertEquals("max score should be equal to the score of the first element", pager.getRecords().get(0).getMaxScore(), pager.getRecords().get(0).getScore(), 0);
|
371
|
}
|
372
|
|
373
|
/**
|
374
|
* test for max score and sort by score of hit groups
|
375
|
* with all matches per taxon in a multiple TextData elements
|
376
|
* see {@link #testFullText_ScoreAndOrder_1()} for the complement
|
377
|
* test with matches in a single TextData per taxon
|
378
|
*
|
379
|
* @throws CorruptIndexException
|
380
|
* @throws IOException
|
381
|
* @throws ParseException
|
382
|
*/
|
383
|
@SuppressWarnings("rawtypes")
|
384
|
@Test
|
385
|
@DataSet
|
386
|
@Ignore // test fails, maybe the assumptions made here are not compatible with the lucene scoring mechanism see http://lucene.apache.org/core/3_6_1/scoring.html
|
387
|
public final void testFullText_ScoreAndOrder_2() throws CorruptIndexException, IOException, ParseException {
|
388
|
|
389
|
int numOfTaxa = 3;
|
390
|
|
391
|
UUID[] taxonUuids = new UUID[numOfTaxa];
|
392
|
|
393
|
for(int i = 0; i < numOfTaxa; i++){
|
394
|
|
395
|
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(null), null);
|
396
|
taxon.setTitleCache("Taxon_" + i, true);
|
397
|
taxonUuids[i] = taxon.getUuid();
|
398
|
taxonService.save(taxon);
|
399
|
|
400
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
401
|
for(int k = 0; k < i; k++){
|
402
|
description.addElement(TextData.NewInstance("Rot", Language.DEFAULT(), null));
|
403
|
}
|
404
|
descriptionService.saveOrUpdate(description);
|
405
|
}
|
406
|
|
407
|
commitAndStartNewTransaction(null);
|
408
|
refreshLuceneIndex();
|
409
|
|
410
|
Pager<SearchResult<TaxonBase>> pager;
|
411
|
|
412
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Rot", null, null, null, false, null, null, null, null);
|
413
|
for(int i = 0; i < numOfTaxa; i++){
|
414
|
Assert.assertEquals("taxa should be orderd by relevance (= score)", taxonUuids[numOfTaxa - i - 1], pager.getRecords().get(i).getEntity().getUuid());
|
415
|
}
|
416
|
Assert.assertEquals("max score should be equal to the score of the first element", pager.getRecords().get(0).getMaxScore(), pager.getRecords().get(0).getScore(), 0);
|
417
|
}
|
418
|
|
419
|
|
420
|
/**
|
421
|
* @throws CorruptIndexException
|
422
|
* @throws IOException
|
423
|
* @throws ParseException
|
424
|
* @throws LuceneMultiSearchException
|
425
|
*/
|
426
|
@Test
|
427
|
@DataSet
|
428
|
public final void testFullText_Grouping() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
429
|
|
430
|
TaxonDescription description = (TaxonDescription) descriptionService.find(UUID.fromString(D_ABIES_ALBA_UUID));
|
431
|
Set<String> uniqueRandomStrs = new HashSet<String>(1024);
|
432
|
int numOfItems = 100;
|
433
|
while(uniqueRandomStrs.size() < numOfItems){
|
434
|
uniqueRandomStrs.add(RandomStringUtils.random(5, true, false));
|
435
|
}
|
436
|
for(String rndStr: uniqueRandomStrs){
|
437
|
description.addElement(CommonTaxonName.NewInstance("Rot" + rndStr, Language.DEFAULT()));
|
438
|
}
|
439
|
descriptionService.saveOrUpdate(description);
|
440
|
|
441
|
commitAndStartNewTransaction(new String[]{"DESCRIPTIONELEMENTBASE"});
|
442
|
|
443
|
refreshLuceneIndex();
|
444
|
|
445
|
int pageSize = 10;
|
446
|
|
447
|
Pager<SearchResult<TaxonBase>> pager;
|
448
|
boolean highlightFragments = true;
|
449
|
|
450
|
// test with findByDescriptionElementFullText
|
451
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Rot*", null, null, null, highlightFragments, pageSize, null, null, null);
|
452
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
453
|
Assert.assertEquals("All matches should be grouped into a single SearchResult element", 1, pager.getRecords().size());
|
454
|
Assert.assertEquals("The count property of the pager must be set correctly", 1, pager.getCount().intValue());
|
455
|
Map<String, String[]> highlightMap = pager.getRecords().get(0).getFieldHighlightMap();
|
456
|
// maxDocsPerGroup is defined in LuceneSearch and defaults to 10
|
457
|
int maxDocsPerGroup = 10;
|
458
|
Assert.assertEquals("expecting 10 highlighted fragments of field 'name'", maxDocsPerGroup, highlightMap.get("name").length);
|
459
|
|
460
|
// test with findByEverythingFullText
|
461
|
pager = taxonService.findByEverythingFullText( "Rot*", null, null, highlightFragments, pageSize, null, null, null);
|
462
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
463
|
Assert.assertEquals("All matches should be grouped into a single SearchResult element", 1, pager.getRecords().size());
|
464
|
Assert.assertEquals("The count property of the pager must be set correctly", 1, pager.getCount().intValue());
|
465
|
highlightMap = pager.getRecords().get(0).getFieldHighlightMap();
|
466
|
// maxDocsPerGroup is defined in LuceneSearch and defaults to 10
|
467
|
maxDocsPerGroup = 10;
|
468
|
Assert.assertEquals("expecting 10 highlighted fragments of field 'name'", maxDocsPerGroup, highlightMap.get("name").length);
|
469
|
|
470
|
}
|
471
|
|
472
|
@SuppressWarnings("rawtypes")
|
473
|
@Test
|
474
|
@DataSet
|
475
|
@Ignore
|
476
|
public final void testFindByDescriptionElementFullText_TextData() throws CorruptIndexException, IOException, ParseException {
|
477
|
|
478
|
refreshLuceneIndex();
|
479
|
|
480
|
Pager<SearchResult<TaxonBase>> pager;
|
481
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Abies", null, null, null, false, null, null, null, null);
|
482
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
483
|
Assert.assertEquals("Expecting one entity when searching for any TextData", 1, pager.getCount().intValue());
|
484
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getEntity().getTitleCache());
|
485
|
Assert.assertTrue("Expecting two docs, one for RUSSIAN and one for GERMAN", pager.getRecords().get(0).getDocs().size() == 2);
|
486
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getDocs().iterator().next().get("inDescription.taxon.titleCache"));
|
487
|
|
488
|
|
489
|
pager = taxonService.findByDescriptionElementFullText(null, "Abies", null, null, null, false, null, null, null, null);
|
490
|
Assert.assertEquals("Expecting one entity when searching for any type", 1, pager.getCount().intValue());
|
491
|
|
492
|
pager = taxonService.findByDescriptionElementFullText(null, "Abies", null, Arrays.asList(new Feature[]{Feature.UNKNOWN()}), null, false, null, null, null, null);
|
493
|
Assert.assertEquals("Expecting one entity when searching for any type and for Feature DESCRIPTION", 1, pager.getCount().intValue());
|
494
|
|
495
|
pager = taxonService.findByDescriptionElementFullText(null, "Abies", null, Arrays.asList(new Feature[]{Feature.CHROMOSOME_NUMBER()}), null, false, null, null, null, null);
|
496
|
Assert.assertEquals("Expecting no entity when searching for any type and for Feature CHROMOSOME_NUMBER", 0, pager.getCount().intValue());
|
497
|
|
498
|
pager = taxonService.findByDescriptionElementFullText(null, "Abies", null, Arrays.asList(new Feature[]{Feature.CHROMOSOME_NUMBER(), Feature.UNKNOWN()}), null, false, null, null, null, null);
|
499
|
Assert.assertEquals("Expecting no entity when searching for any type and for Feature DESCRIPTION or CHROMOSOME_NUMBER", 1, pager.getCount().intValue());
|
500
|
|
501
|
pager = taxonService.findByDescriptionElementFullText(Distribution.class, "Abies", null, null, null, false, null, null, null, null);
|
502
|
Assert.assertEquals("Expecting no entity when searching for Distribution", 0, pager.getCount().intValue());
|
503
|
|
504
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, null, Arrays.asList(new Language[]{}), false, null, null, null, null);
|
505
|
Assert.assertEquals("Expecting one entity", 1, pager.getCount().intValue());
|
506
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getEntity().getTitleCache());
|
507
|
|
508
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, null, Arrays.asList(new Language[]{Language.RUSSIAN()}), false, null, null, null, null);
|
509
|
Assert.assertEquals("Expecting one entity", 1, pager.getCount().intValue());
|
510
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getEntity().getTitleCache());
|
511
|
|
512
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
513
|
Assert.assertEquals("Expecting no entity", 0, pager.getCount().intValue());
|
514
|
|
515
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), false, null, null, null, null);
|
516
|
Assert.assertEquals("Expecting one entity", 1, pager.getCount().intValue());
|
517
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getEntity().getTitleCache());
|
518
|
}
|
519
|
|
520
|
@SuppressWarnings("rawtypes")
|
521
|
@Test
|
522
|
@DataSet
|
523
|
public final void testFindByDescriptionElementFullText_MultipleWords() throws CorruptIndexException, IOException, ParseException {
|
524
|
|
525
|
refreshLuceneIndex();
|
526
|
|
527
|
// Pflanzenart aus der Gattung der Tannen
|
528
|
long start = System.currentTimeMillis();
|
529
|
|
530
|
Pager<SearchResult<TaxonBase>> pager;
|
531
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Pflanzenart Tannen", null, null, null, false, null, null, null, null);
|
532
|
Assert.assertEquals("OR search : Expecting one entity", 1, pager.getCount().intValue());
|
533
|
|
534
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Pflanzenart Wespen", null, null, null, false, null, null, null, null);
|
535
|
Assert.assertEquals("OR search : Expecting one entity", 1, pager.getCount().intValue());
|
536
|
|
537
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "+Pflanzenart +Tannen", null, null, null, false, null, null, null, null);
|
538
|
Assert.assertEquals("AND search : Expecting one entity", 1, pager.getCount().intValue());
|
539
|
|
540
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "+Pflanzenart +Wespen", null, null, null, false, null, null, null, null);
|
541
|
Assert.assertEquals("AND search : Expecting no entity", 0, pager.getCount().intValue());
|
542
|
|
543
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "\"Pflanzenart aus der Gattung der Tannen\"", null, null, null, false, null, null, null, null);
|
544
|
Assert.assertEquals("Phrase search : Expecting one entity", 1, pager.getCount().intValue());
|
545
|
|
546
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "\"Pflanzenart aus der Gattung der Wespen\"", null, null, null, false, null, null, null, null);
|
547
|
Assert.assertEquals("Phrase search : Expecting one entity", 0, pager.getCount().intValue());
|
548
|
|
549
|
logger.info("testFindByDescriptionElementFullText_MultipleWords() duration: " + (System.currentTimeMillis() - start) + "ms");
|
550
|
|
551
|
}
|
552
|
|
553
|
|
554
|
@SuppressWarnings("rawtypes")
|
555
|
@Test
|
556
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
557
|
public final void testFindByDescriptionElementFullText_modify_DescriptionElement() throws CorruptIndexException, IOException, ParseException {
|
558
|
|
559
|
refreshLuceneIndex();
|
560
|
|
561
|
Pager<SearchResult<TaxonBase>> pager;
|
562
|
//
|
563
|
// modify the DescriptionElement
|
564
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), false, null, null, null, null);
|
565
|
Assert.assertTrue("Search did not return any results", pager.getRecords().size() > 0);
|
566
|
Assert.assertTrue("Expecting only one doc", pager.getRecords().get(0).getDocs().size() == 1);
|
567
|
Document indexDocument = pager.getRecords().get(0).getDocs().iterator().next();
|
568
|
String[] descriptionElementUuidStr = indexDocument.getValues("uuid");
|
569
|
String[] inDescriptionUuidStr = indexDocument.getValues("inDescription.uuid");
|
570
|
// is only one uuid!
|
571
|
DescriptionElementBase textData = descriptionService.getDescriptionElementByUuid(UUID.fromString(descriptionElementUuidStr[0]));
|
572
|
|
573
|
((TextData)textData).removeText(Language.GERMAN());
|
574
|
((TextData)textData).putText(Language.SPANISH_CASTILIAN(), "abeto bals"+UTF8.SMALL_A_ACUTE+"mico");
|
575
|
|
576
|
descriptionService.saveDescriptionElement(textData);
|
577
|
commitAndStartNewTransaction(null);
|
578
|
// printDataSet(System.out, new String[] {
|
579
|
// "DESCRIPTIONELEMENTBASE", "LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING" }
|
580
|
// );
|
581
|
|
582
|
//
|
583
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), false, null, null, null, null);
|
584
|
Assert.assertEquals("The german 'Balsam-Tanne' TextData should no longer be indexed", 0, pager.getCount().intValue());
|
585
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "abeto", null, null, Arrays.asList(new Language[]{Language.SPANISH_CASTILIAN()}), false, null, null, null, null);
|
586
|
Assert.assertEquals("expecting to find the SPANISH_CASTILIAN 'abeto bals"+UTF8.SMALL_A_ACUTE+"mico'", 1, pager.getCount().intValue());
|
587
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "bals"+UTF8.SMALL_A_ACUTE+"mico", null, null, null, false, null, null, null, null);
|
588
|
Assert.assertEquals("expecting to find the SPANISH_CASTILIAN 'abeto bals"+UTF8.SMALL_A_ACUTE+"mico'", 1, pager.getCount().intValue());
|
589
|
|
590
|
//
|
591
|
// modify the DescriptionElement via the Description object
|
592
|
DescriptionBase<?> description = descriptionService.find(UUID.fromString(inDescriptionUuidStr[0]));
|
593
|
Set<DescriptionElementBase> elements = description.getElements();
|
594
|
for( DescriptionElementBase elm : elements){
|
595
|
if(elm.getUuid().toString().equals(descriptionElementUuidStr[0])){
|
596
|
((TextData)elm).removeText(Language.SPANISH_CASTILIAN());
|
597
|
((TextData)elm).putText(Language.POLISH(), "Jod"+UTF8.POLISH_L+"a balsamiczna");
|
598
|
}
|
599
|
}
|
600
|
descriptionService.saveOrUpdate(description);
|
601
|
commitAndStartNewTransaction(null);
|
602
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "abeto", null, null, Arrays.asList(new Language[]{Language.SPANISH_CASTILIAN()}), false, null, null, null, null);
|
603
|
Assert.assertEquals("The spanish 'abeto bals"+UTF8.SMALL_A_ACUTE+"mico' TextData should no longer be indexed", 0, pager.getCount().intValue());
|
604
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "balsamiczna", null, null, Arrays.asList(new Language[]{Language.POLISH()}), false, null, null, null, null);
|
605
|
Assert.assertEquals("expecting to find the POLISH 'Jod"+UTF8.POLISH_L+"a balsamiczna'", 1, pager.getCount().intValue());
|
606
|
}
|
607
|
|
608
|
@SuppressWarnings("rawtypes")
|
609
|
@Test
|
610
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
611
|
public final void testFindByDescriptionElementFullText_modify_Taxon() throws CorruptIndexException, IOException, ParseException {
|
612
|
|
613
|
refreshLuceneIndex();
|
614
|
|
615
|
Pager<SearchResult<TaxonBase>> pager;
|
616
|
Taxon t_abies_balsamea = (Taxon)taxonService.find(UUID.fromString(ABIES_BALSAMEA_UUID));
|
617
|
TaxonDescription d_abies_balsamea = (TaxonDescription)descriptionService.find(UUID.fromString(D_ABIES_BALSAMEA_UUID));
|
618
|
|
619
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
620
|
Assert.assertEquals("expecting to find the GERMAN 'Balsam-Tanne'", 1, pager.getCount().intValue());
|
621
|
|
622
|
// exchange the Taxon with another one via the Taxon object
|
623
|
// 1.) remove existing description:
|
624
|
t_abies_balsamea.removeDescription(d_abies_balsamea);
|
625
|
|
626
|
taxonService.saveOrUpdate(t_abies_balsamea);
|
627
|
commitAndStartNewTransaction(null);
|
628
|
|
629
|
t_abies_balsamea = (Taxon)taxonService.find(t_abies_balsamea.getUuid());
|
630
|
|
631
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
632
|
Assert.assertEquals("'Balsam-Tanne' should no longer be found", 0, pager.getCount().intValue());
|
633
|
|
634
|
// 2.) create new description and add to taxon:
|
635
|
TaxonDescription d_abies_balsamea_new = TaxonDescription.NewInstance();
|
636
|
d_abies_balsamea_new
|
637
|
.addElement(TextData
|
638
|
.NewInstance(
|
639
|
"Die Balsamtanne ist mit bis zu 30 m Höhe ein mittelgro"+UTF8.SHARP_S+"er Baum und kann bis zu 200 Jahre alt werden",
|
640
|
Language.GERMAN(), null));
|
641
|
t_abies_balsamea.addDescription(d_abies_balsamea_new);
|
642
|
// set authorshipCache to null to avoid validation exception,
|
643
|
// this is maybe not needed in future, see ticket #3344
|
644
|
BotanicalName abies_balsamea = HibernateProxyHelper.deproxy(t_abies_balsamea.getName(), BotanicalName.class);
|
645
|
abies_balsamea.setAuthorshipCache(null);
|
646
|
printDataSet(System.err, new String[] {"LANGUAGESTRING_AUD"});
|
647
|
taxonService.saveOrUpdate(t_abies_balsamea);
|
648
|
commitAndStartNewTransaction(null);
|
649
|
|
650
|
// printDataSet(System.out, new String[] {
|
651
|
// "DESCRIPTIONBASE"
|
652
|
// });
|
653
|
|
654
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "mittelgro"+UTF8.SHARP_S+"er Baum", null, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
655
|
Assert.assertEquals("the taxon should be found via the new Description", 1, pager.getCount().intValue());
|
656
|
}
|
657
|
|
658
|
@SuppressWarnings("rawtypes")
|
659
|
@Test
|
660
|
@DataSet
|
661
|
public final void testFindByDescriptionElementFullText_modify_Classification() throws CorruptIndexException, IOException, ParseException {
|
662
|
|
663
|
refreshLuceneIndex();
|
664
|
|
665
|
Pager<SearchResult<TaxonBase>> pager;
|
666
|
|
667
|
// put taxon into other classification, new taxon node
|
668
|
Classification classification = classificationService.find(UUID.fromString(CLASSIFICATION_UUID));
|
669
|
Classification alternateClassification = classificationService.find(UUID.fromString(CLASSIFICATION_ALT_UUID));
|
670
|
|
671
|
// TODO: why is the test failing when the childNode is already retrieved here, and not after the following four lines?
|
672
|
//TaxonNode childNode = classification.getChildNodes().iterator().next();
|
673
|
|
674
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
675
|
Assert.assertEquals("expecting to find the GERMAN 'Balsam-Tanne' even if filtering by classification", 1, pager.getCount().intValue());
|
676
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", alternateClassification, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
677
|
Assert.assertEquals("GERMAN 'Balsam-Tanne' should NOT be found in other classification", 0, pager.getCount().intValue());
|
678
|
|
679
|
// check for the right taxon node
|
680
|
TaxonNode childNode = classification.getChildNodes().iterator().next();
|
681
|
Assert.assertEquals("expecting Abies balsamea sec.", childNode.getTaxon().getUuid().toString(), ABIES_BALSAMEA_UUID);
|
682
|
Assert.assertEquals("expecting default classification", childNode.getClassification().getUuid().toString(), CLASSIFICATION_UUID);
|
683
|
|
684
|
// moving the taxon around, the rootnode is only a proxy
|
685
|
alternateClassification.setRootNode(HibernateProxyHelper.deproxy(alternateClassification.getRootNode(), TaxonNode.class));
|
686
|
alternateClassification.addChildNode(childNode, null, null);
|
687
|
|
688
|
classificationService.saveOrUpdate(alternateClassification);
|
689
|
commitAndStartNewTransaction(null);
|
690
|
|
691
|
// printDataSet(System.out, new String[] {
|
692
|
// "TAXONBASE", "TAXONNODE", "CLASSIFICATION"
|
693
|
// });
|
694
|
|
695
|
// reload classification
|
696
|
classification = classificationService.find(UUID.fromString(CLASSIFICATION_UUID));
|
697
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", alternateClassification, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
698
|
Assert.assertEquals("GERMAN 'Balsam-Tanne' should now be found in other classification", 1, pager.getCount().intValue());
|
699
|
|
700
|
classification.getChildNodes().clear();
|
701
|
classificationService.saveOrUpdate(classification);
|
702
|
commitAndStartNewTransaction(null);
|
703
|
|
704
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", classification, null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
705
|
Assert.assertEquals("Now the GERMAN 'Balsam-Tanne' should NOT be found in original classification", 0, pager.getCount().intValue());
|
706
|
|
707
|
}
|
708
|
|
709
|
@SuppressWarnings("rawtypes")
|
710
|
@Test
|
711
|
@DataSet
|
712
|
public final void testFindByDescriptionElementFullText_CategoricalData() throws CorruptIndexException, IOException, ParseException {
|
713
|
|
714
|
// add CategoricalData
|
715
|
DescriptionBase d_abies_balsamea = descriptionService.find(UUID.fromString(D_ABIES_BALSAMEA_UUID));
|
716
|
// Categorical data
|
717
|
CategoricalData cdata = CategoricalData.NewInstance();
|
718
|
cdata.setFeature(Feature.DESCRIPTION());
|
719
|
State state = State.NewInstance("green", "green", "gn");
|
720
|
|
721
|
StateData statedata = StateData.NewInstance(state);
|
722
|
statedata.putModifyingText(Language.ENGLISH(), "always, even during winter");
|
723
|
cdata.addStateData(statedata);
|
724
|
d_abies_balsamea.addElement(cdata);
|
725
|
|
726
|
UUID termUUID = termService.save(state).getUuid();
|
727
|
descriptionService.save(d_abies_balsamea);
|
728
|
|
729
|
commitAndStartNewTransaction(null);
|
730
|
|
731
|
// printDataSet(System.out, new String[] {
|
732
|
// "STATEDATA", "STATEDATA_DEFINEDTERMBASE", "STATEDATA_LANGUAGESTRING", "LANGUAGESTRING"});
|
733
|
|
734
|
refreshLuceneIndex();
|
735
|
|
736
|
Pager<SearchResult<TaxonBase>> pager;
|
737
|
pager = taxonService.findByDescriptionElementFullText(CategoricalData.class, "green", null, null, null, false, null, null, null, null);
|
738
|
Assert.assertEquals("Expecting one entity", 1, pager.getCount().intValue());
|
739
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getEntity().getTitleCache());
|
740
|
Assert.assertTrue("Expecting only one doc", pager.getRecords().get(0).getDocs().size() == 1);
|
741
|
Assert.assertEquals("Abies balsamea sec. Kohlbecker, A., Testcase standart views, 2013", pager.getRecords().get(0).getDocs().iterator().next().get("inDescription.taxon.titleCache"));
|
742
|
|
743
|
|
744
|
//TODO modify the StateData
|
745
|
TaxonBase taxon = pager.getRecords().get(0).getEntity();
|
746
|
|
747
|
String newName = "Quercus robur";
|
748
|
taxon.setTitleCache(newName + " sec. ", true);
|
749
|
|
750
|
taxonService.saveOrUpdate(taxon);
|
751
|
commitAndStartNewTransaction(null);
|
752
|
|
753
|
taxon = taxonService.find(taxon.getUuid());
|
754
|
Assert.assertEquals(newName + " sec. ", taxon.getTitleCache());
|
755
|
DefinedTermBase term = termService.find(termUUID);
|
756
|
|
757
|
termService.delete(term);
|
758
|
|
759
|
}
|
760
|
|
761
|
@SuppressWarnings("rawtypes")
|
762
|
@Test
|
763
|
@DataSet
|
764
|
public final void testFindByDescriptionElementFullText_Highlighting() throws CorruptIndexException, IOException, ParseException {
|
765
|
|
766
|
refreshLuceneIndex();
|
767
|
|
768
|
Pager<SearchResult<TaxonBase>> pager;
|
769
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Abies", null, null, null, true, null, null, null, null);
|
770
|
Assert.assertEquals("Expecting one entity when searching for any TextData", 1, pager.getCount().intValue());
|
771
|
SearchResult<TaxonBase> searchResult = pager.getRecords().get(0);
|
772
|
Assert.assertTrue("the map of highlighted fragments should contain at least one item", searchResult.getFieldHighlightMap().size() > 0);
|
773
|
String[] fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
774
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].contains("<B>Abies</B>"));
|
775
|
|
776
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Pflanzenart Tannen", null, null, null, true, null, null, null, null);
|
777
|
searchResult = pager.getRecords().get(0);
|
778
|
Assert.assertTrue("Phrase search : Expecting at least one item in highlighted fragments", searchResult.getFieldHighlightMap().size() > 0);
|
779
|
fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
780
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].contains("<B>Pflanzenart</B>") || fragments[0].contains("<B>Tannen</B>"));
|
781
|
|
782
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "+Pflanzenart +Tannen", null, null, null, true, null, null, null, null);
|
783
|
searchResult = pager.getRecords().get(0);
|
784
|
Assert.assertTrue("Phrase search : Expecting at least one item in highlighted fragments", searchResult.getFieldHighlightMap().size() > 0);
|
785
|
fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
786
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].contains("<B>Pflanzenart</B>") && fragments[0].contains("<B>Tannen</B>"));
|
787
|
|
788
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "\"Pflanzenart aus der Gattung der Tannen\"", null, null, null, true, null, null, null, null);
|
789
|
searchResult = pager.getRecords().get(0);
|
790
|
Assert.assertTrue("Phrase search : Expecting at least one item in highlighted fragments", searchResult.getFieldHighlightMap().size() > 0);
|
791
|
fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
792
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].contains("<B>Pflanzenart</B> <B>aus</B> <B>der</B> <B>Gattung</B> <B>der</B> <B>Tannen</B>"));
|
793
|
|
794
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Gatt*", null, null, null, true, null, null, null, null);
|
795
|
searchResult = pager.getRecords().get(0);
|
796
|
Assert.assertTrue("Wildcard search : Expecting at least one item in highlighted fragments", searchResult.getFieldHighlightMap().size() > 0);
|
797
|
fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
798
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].contains("<B>Gatt"));
|
799
|
}
|
800
|
|
801
|
|
802
|
@Test
|
803
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
804
|
public final void testFindByFullText() throws CorruptIndexException, IOException, ParseException {
|
805
|
|
806
|
refreshLuceneIndex();
|
807
|
|
808
|
Classification europeanAbiesClassification = classificationService.find(UUID.fromString(CLASSIFICATION_UUID));
|
809
|
|
810
|
Pager<SearchResult<TaxonBase>> pager;
|
811
|
|
812
|
pager = taxonService.findByFullText(null, "Abies", null, null, true, null, null, null, null); // --> 7
|
813
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
814
|
Assert.assertEquals("Expecting 8 entities", 8, pager.getCount().intValue());
|
815
|
|
816
|
pager = taxonService.findByFullText(Taxon.class, "Abies", null, null, true, null, null, null, null); // --> 6
|
817
|
Assert.assertEquals("Expecting 7 entities", 7, pager.getCount().intValue());
|
818
|
|
819
|
pager = taxonService.findByFullText(Synonym.class, "Abies", null, null, true, null, null, null, null); // --> 1
|
820
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
821
|
|
822
|
pager = taxonService.findByFullText(TaxonBase.class, "sec", null, null, true, null, null, null, null); // --> 7
|
823
|
Assert.assertEquals("Expecting 8 entities", 9, pager.getCount().intValue());
|
824
|
|
825
|
pager = taxonService.findByFullText(null, "genus", null, null, true, null, null, null, null); // --> 1
|
826
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
827
|
|
828
|
pager = taxonService.findByFullText(Taxon.class, "subalpina", null, null, true, null, null, null, null); // --> 0
|
829
|
Assert.assertEquals("Expecting 0 entities", 0, pager.getCount().intValue());
|
830
|
|
831
|
// synonym in classification ???
|
832
|
}
|
833
|
|
834
|
@Test
|
835
|
@DataSet
|
836
|
public final void testPrepareByAreaSearch() throws IOException, ParseException {
|
837
|
|
838
|
List<PresenceAbsenceTerm> statusFilter = new ArrayList<PresenceAbsenceTerm>();
|
839
|
List<NamedArea> areaFilter = new ArrayList<NamedArea>();
|
840
|
areaFilter.add(germany);
|
841
|
areaFilter.add(canada);
|
842
|
areaFilter.add(russia);
|
843
|
|
844
|
Pager<SearchResult<TaxonBase>> pager = taxonService.findByDistribution(areaFilter, statusFilter, null, 20, 0, null, null);
|
845
|
Assert.assertEquals("Expecting 2 entities", Integer.valueOf(2), Integer.valueOf(pager.getRecords().size()));
|
846
|
|
847
|
}
|
848
|
|
849
|
@Test
|
850
|
@DataSet
|
851
|
public final void testFindTaxaAndNamesByFullText() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
852
|
|
853
|
refreshLuceneIndex();
|
854
|
|
855
|
Pager<SearchResult<TaxonBase>> pager;
|
856
|
|
857
|
Classification alternateClassification = classificationService.find(UUID.fromString(CLASSIFICATION_ALT_UUID));
|
858
|
|
859
|
|
860
|
pager = taxonService.findTaxaAndNamesByFullText(
|
861
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
862
|
"Abies", null, null, null, null, true, null, null, null, null);
|
863
|
// logPagerRecords(pager, Level.DEBUG);
|
864
|
Assert.assertEquals("doTaxa & doSynonyms", 8, pager.getCount().intValue());
|
865
|
|
866
|
pager = taxonService.findTaxaAndNamesByFullText(
|
867
|
EnumSet.allOf(TaxaAndNamesSearchMode.class),
|
868
|
"Abies", null, null, null, null, true, null, null, null, null);
|
869
|
// logPagerRecords(pager, Level.DEBUG);
|
870
|
Assert.assertEquals("all search modes", 8, pager.getCount().intValue());
|
871
|
|
872
|
pager = taxonService.findTaxaAndNamesByFullText(
|
873
|
EnumSet.allOf(TaxaAndNamesSearchMode.class),
|
874
|
"Abies", alternateClassification, null, null, null, true, null, null, null, null);
|
875
|
// logPagerRecords(pager, Level.DEBUG);
|
876
|
Assert.assertEquals("all search modes, filtered by alternateClassification", 1, pager.getCount().intValue());
|
877
|
|
878
|
pager = taxonService.findTaxaAndNamesByFullText(
|
879
|
EnumSet.of(TaxaAndNamesSearchMode.doSynonyms),
|
880
|
"Abies", null, null, null, null, true, null, null, null, null);
|
881
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
882
|
SearchResult<TaxonBase> searchResult = pager.getRecords().get(0);
|
883
|
Assert.assertEquals(Synonym.class, searchResult.getEntity().getClass());
|
884
|
// Abies subalpina sec. Kohlbecker, A., Testcase standart views, 2013
|
885
|
|
886
|
|
887
|
pager = taxonService.findTaxaAndNamesByFullText(
|
888
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxaByCommonNames),
|
889
|
"Abies", null, null, null, null, true, null, null, null, null);
|
890
|
Assert.assertEquals("Expecting 0 entity", 0, pager.getCount().intValue());
|
891
|
|
892
|
|
893
|
pager = taxonService.findTaxaAndNamesByFullText(
|
894
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxaByCommonNames),
|
895
|
"Tanne", null, null, null, null, true, null, null, null, null);
|
896
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getRecords().size());
|
897
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
898
|
|
899
|
pager = taxonService.findTaxaAndNamesByFullText(
|
900
|
EnumSet.of(TaxaAndNamesSearchMode.doMisappliedNames),
|
901
|
"kawakamii", null, null, null, null, true, null, null, null, null);
|
902
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
903
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
904
|
|
905
|
}
|
906
|
|
907
|
@Test
|
908
|
@DataSet
|
909
|
public final void testFindTaxaAndNamesByFullText_PhraseQuery() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
910
|
|
911
|
refreshLuceneIndex();
|
912
|
|
913
|
Pager<SearchResult<TaxonBase>> pager;
|
914
|
|
915
|
|
916
|
pager = taxonService.findTaxaAndNamesByFullText(
|
917
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
918
|
"\"Abies alba\"", null, null, null, null, true, null, null, null, null);
|
919
|
// logPagerRecords(pager, Level.DEBUG);
|
920
|
Assert.assertEquals("doTaxa & doSynonyms with simple phrase query", 1, pager.getCount().intValue());
|
921
|
|
922
|
pager = taxonService.findTaxaAndNamesByFullText(
|
923
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
924
|
"\"Abies al*\"", null, null, null, null, true, null, null, null, null);
|
925
|
// logPagerRecords(pager, Level.DEBUG);
|
926
|
Assert.assertEquals("doTaxa & doSynonyms with complex phrase query", 1, pager.getCount().intValue());
|
927
|
|
928
|
pager = taxonService.findTaxaAndNamesByFullText(
|
929
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
930
|
"\"Abies*\"", null, null, null, null, true, null, null, null, null);
|
931
|
// logPagerRecords(pager, Level.DEBUG);
|
932
|
Assert.assertEquals("doTaxa & doSynonyms with simple phrase query", 8, pager.getCount().intValue());
|
933
|
|
934
|
}
|
935
|
|
936
|
@Test
|
937
|
@DataSet
|
938
|
public final void testFindTaxaAndNamesByFullText_Sort() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
939
|
|
940
|
refreshLuceneIndex();
|
941
|
|
942
|
Pager<SearchResult<TaxonBase>> pager;
|
943
|
|
944
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
945
|
|
946
|
String[] docFields2log = new String[]{"id"};
|
947
|
|
948
|
// SortById
|
949
|
orderHints.addAll(OrderHint.ORDER_BY_ID.asList());
|
950
|
pager = taxonService.findTaxaAndNamesByFullText(
|
951
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa),
|
952
|
"Abies", null, null, null, null, true, null, null, orderHints, null);
|
953
|
// logSearchResults(pager, Level.DEBUG, docFields2log);
|
954
|
int lastId = -1;
|
955
|
for(SearchResult<TaxonBase> rs : pager.getRecords()){
|
956
|
if(lastId != -1){
|
957
|
Assert.assertTrue("results not sorted by id", lastId < rs.getEntity().getId());
|
958
|
}
|
959
|
lastId = rs.getEntity().getId();
|
960
|
}
|
961
|
|
962
|
orderHints.addAll(OrderHint.ORDER_BY_ID.asList());
|
963
|
pager = taxonService.findTaxaAndNamesByFullText(
|
964
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
965
|
"Abies", null, null, null, null, true, null, null, orderHints, null);
|
966
|
// logSearchResults(pager, Level.DEBUG, docFields2log);
|
967
|
|
968
|
lastId = -1;
|
969
|
for(SearchResult<TaxonBase> rs : pager.getRecords()){
|
970
|
if(lastId != -1){
|
971
|
Assert.assertTrue("results not sorted by id", lastId < rs.getEntity().getId());
|
972
|
}
|
973
|
lastId = rs.getEntity().getId();
|
974
|
}
|
975
|
|
976
|
// Sortby NOMENCLATURAL_SORT_ORDER TODO make assertions !!!
|
977
|
orderHints.clear();
|
978
|
orderHints.addAll(OrderHint.NOMENCLATURAL_SORT_ORDER.asList());
|
979
|
pager = taxonService.findTaxaAndNamesByFullText(
|
980
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
981
|
"Abies", null, null, null, null, true, null, null, orderHints, null);
|
982
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
983
|
|
984
|
}
|
985
|
|
986
|
@Test
|
987
|
@DataSet
|
988
|
public final void testFindTaxaAndNamesByFullText_AreaFilter() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
989
|
|
990
|
refreshLuceneIndex();
|
991
|
|
992
|
Pager<SearchResult<TaxonBase>> pager;
|
993
|
|
994
|
Set<NamedArea> a_germany_canada_russia = new HashSet<NamedArea>();
|
995
|
a_germany_canada_russia.add(germany);
|
996
|
a_germany_canada_russia.add(canada);
|
997
|
a_germany_canada_russia.add(russia);
|
998
|
|
999
|
Set<NamedArea> a_russia = new HashSet<NamedArea>();
|
1000
|
a_russia.add(russia);
|
1001
|
|
1002
|
Set<PresenceAbsenceTerm> present = new HashSet<PresenceAbsenceTerm>();
|
1003
|
present.add(PresenceAbsenceTerm.PRESENT());
|
1004
|
|
1005
|
Set<PresenceAbsenceTerm> present_native = new HashSet<PresenceAbsenceTerm>();
|
1006
|
present_native.add(PresenceAbsenceTerm.PRESENT());
|
1007
|
present_native.add(PresenceAbsenceTerm.NATIVE());
|
1008
|
|
1009
|
Set<PresenceAbsenceTerm> absent = new HashSet<PresenceAbsenceTerm>();
|
1010
|
absent.add(PresenceAbsenceTerm.ABSENT());
|
1011
|
|
1012
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1013
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa),
|
1014
|
"Abies", null, a_germany_canada_russia, null, null, true, null, null, null, null);
|
1015
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
1016
|
|
1017
|
// abies_kawakamii_sensu_komarov as missapplied name for t_abies_balsamea
|
1018
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1019
|
EnumSet.of(TaxaAndNamesSearchMode.doSynonyms),
|
1020
|
"Abies", null, a_germany_canada_russia, present_native, null, true, null, null, null, null);
|
1021
|
Assert.assertEquals("synonyms with matching area filter", 1, pager.getCount().intValue());
|
1022
|
|
1023
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1024
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
1025
|
"Abies", null, a_germany_canada_russia, null, null, true, null, null, null, null);
|
1026
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
1027
|
Assert.assertEquals("taxa and synonyms with matching area filter", 3, pager.getCount().intValue());
|
1028
|
|
1029
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1030
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
1031
|
"Abies", null, a_germany_canada_russia, present_native, null, true, null, null, null, null);
|
1032
|
Assert.assertEquals("taxa and synonyms with matching area & status filter 1", 3, pager.getCount().intValue());
|
1033
|
|
1034
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1035
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
1036
|
"Abies", null, a_germany_canada_russia, present, null, true, null, null, null, null);
|
1037
|
Assert.assertEquals("taxa and synonyms with matching area & status filter 2", 2, pager.getCount().intValue());
|
1038
|
|
1039
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1040
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxa, TaxaAndNamesSearchMode.doSynonyms),
|
1041
|
"Abies", null, a_russia, present, null, true, null, null, null, null);
|
1042
|
Assert.assertEquals("taxa and synonyms with non matching area & status filter", 0, pager.getCount().intValue());
|
1043
|
|
1044
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1045
|
EnumSet.of(TaxaAndNamesSearchMode.doTaxaByCommonNames),
|
1046
|
"Tanne", null, a_germany_canada_russia, present_native, null, true, null, null, null, null);
|
1047
|
Assert.assertEquals("ByCommonNames with area filter", 1, pager.getCount().intValue());
|
1048
|
|
1049
|
// abies_kawakamii_sensu_komarov as misapplied name for t_abies_balsamea
|
1050
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1051
|
EnumSet.of(TaxaAndNamesSearchMode.doMisappliedNames),
|
1052
|
"Abies", null, a_germany_canada_russia, present_native, null, true, null, null, null, null);
|
1053
|
Assert.assertEquals("misappliedNames with matching area & status filter", 1, pager.getCount().intValue());
|
1054
|
|
1055
|
|
1056
|
// 1. remove existing taxon relation
|
1057
|
Taxon t_abies_balsamea = (Taxon)taxonService.find(UUID.fromString(ABIES_BALSAMEA_UUID));
|
1058
|
Set<TaxonRelationship> relsTo = t_abies_balsamea.getRelationsToThisTaxon();
|
1059
|
Assert.assertEquals(1, relsTo.size());
|
1060
|
TaxonRelationship taxonRelation = relsTo.iterator().next();
|
1061
|
t_abies_balsamea.removeTaxonRelation(taxonRelation);
|
1062
|
taxonService.saveOrUpdate(t_abies_balsamea);
|
1063
|
commitAndStartNewTransaction(null);
|
1064
|
|
1065
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1066
|
EnumSet.of(TaxaAndNamesSearchMode.doMisappliedNames),
|
1067
|
"Abies", null, a_germany_canada_russia, present_native, null, true, null, null, null, null);
|
1068
|
Assert.assertEquals("misappliedNames with matching area & status filter, should match nothing now", 0, pager.getCount().intValue());
|
1069
|
|
1070
|
// 2. now add abies_kawakamii_sensu_komarov as misapplied name for t_abies_alba and search for misapplications in russia: ABSENT
|
1071
|
Taxon t_abies_kawakamii_sensu_komarov = (Taxon)taxonService.find(UUID.fromString(D_ABIES_KAWAKAMII_SEC_KOMAROV_UUID));
|
1072
|
Taxon t_abies_alba = (Taxon)taxonService.find(UUID.fromString(ABIES_ALBA_UUID));
|
1073
|
t_abies_alba.addMisappliedName(t_abies_kawakamii_sensu_komarov, null, null);
|
1074
|
|
1075
|
taxonService.update(t_abies_kawakamii_sensu_komarov);
|
1076
|
|
1077
|
commitAndStartNewTransaction(null);
|
1078
|
|
1079
|
pager = taxonService.findTaxaAndNamesByFullText(
|
1080
|
EnumSet.of(TaxaAndNamesSearchMode.doMisappliedNames),
|
1081
|
"Abies", null, a_germany_canada_russia, absent, null, true, null, null, null, null);
|
1082
|
Assert.assertEquals("misappliedNames with matching area & status filter, should find one", 1, pager.getCount().intValue());
|
1083
|
|
1084
|
}
|
1085
|
|
1086
|
@Test
|
1087
|
@DataSet
|
1088
|
@Ignore // remove once http://dev.e-taxonomy.eu/trac/ticket/5477 is solved
|
1089
|
public final void testFindTaxaAndNamesByFullText_AreaFilter_issue5477() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
1090
|
|
1091
|
Set<NamedArea> a_germany_canada_russia = new HashSet<NamedArea>();
|
1092
|
a_germany_canada_russia.add(germany);
|
1093
|
a_germany_canada_russia.add(canada);
|
1094
|
a_germany_canada_russia.add(russia);
|
1095
|
|
1096
|
|
1097
|
Set<PresenceAbsenceTerm> absent = new HashSet<PresenceAbsenceTerm>();
|
1098
|
absent.add(PresenceAbsenceTerm.ABSENT());
|
1099
|
|
1100
|
Taxon t_abies_kawakamii_sensu_komarov = (Taxon)taxonService.find(UUID.fromString(D_ABIES_KAWAKAMII_SEC_KOMAROV_UUID));
|
1101
|
Taxon t_abies_alba = (Taxon)taxonService.find(UUID.fromString(ABIES_ALBA_UUID));
|
1102
|
t_abies_alba.addMisappliedName(t_abies_kawakamii_sensu_komarov, null, null);
|
1103
|
|
1104
|
/* Since the upgrade from hibernate search 4 to 5.5
|
1105
|
* triggering an update of t_abies_alba is no longer sufficient to also update the
|
1106
|
* document of t_abies_kawakamii_sensu_komarov in the lucene index.
|
1107
|
* the last test in testFindTaxaAndNamesByFullText_AreaFilter() failed in this case.
|
1108
|
* This situation is reproduced here:
|
1109
|
*/
|
1110
|
taxonService.update(t_abies_alba);
|
1111
|
|
1112
|
commitAndStartNewTransaction(null);
|
1113
|
|
1114
|
Pager pager = taxonService.findTaxaAndNamesByFullText(
|
1115
|
EnumSet.of(TaxaAndNamesSearchMode.doMisappliedNames),
|
1116
|
"Abies", null, a_germany_canada_russia, absent, null, true, null, null, null, null);
|
1117
|
Assert.assertEquals("misappliedNames with matching area & status filter, should find one", 1, pager.getCount().intValue());
|
1118
|
}
|
1119
|
|
1120
|
|
1121
|
/**
|
1122
|
* Regression test for #3119: fulltext search: Entity always null whatever search
|
1123
|
*
|
1124
|
* @throws CorruptIndexException
|
1125
|
* @throws IOException
|
1126
|
* @throws ParseException
|
1127
|
* @throws LuceneMultiSearchException
|
1128
|
*/
|
1129
|
@Test
|
1130
|
@DataSet
|
1131
|
public final void testFindByEverythingFullText() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
1132
|
|
1133
|
refreshLuceneIndex();
|
1134
|
|
1135
|
Pager<SearchResult<TaxonBase>> pager;
|
1136
|
|
1137
|
// via Taxon
|
1138
|
pager = taxonService.findByEverythingFullText("Abies", null, null, true, null, null, null, null);
|
1139
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
1140
|
Assert.assertTrue("Expecting at least 7 entities for 'Abies'", pager.getCount() > 7);
|
1141
|
Assert.assertNotNull("Expecting entity", pager.getRecords().get(0).getEntity());
|
1142
|
Assert.assertEquals("Expecting Taxon entity", Taxon.class, pager.getRecords().get(0).getEntity().getClass());
|
1143
|
|
1144
|
// via DescriptionElement
|
1145
|
pager = taxonService.findByEverythingFullText("present", null, null, true, null, null, null, null);
|
1146
|
Assert.assertEquals("Expecting one entity when searching for area 'present'", 1, pager.getCount().intValue());
|
1147
|
Assert.assertNotNull("Expecting entity", pager.getRecords().get(0).getEntity());
|
1148
|
Assert.assertEquals("Expecting Taxon entity", Taxon.class, CdmBase.deproxy(pager.getRecords().get(0).getEntity()).getClass());
|
1149
|
Assert.assertEquals("Expecting Taxon ", ABIES_BALSAMEA_UUID, pager.getRecords().get(0).getEntity().getUuid().toString());
|
1150
|
|
1151
|
}
|
1152
|
|
1153
|
|
1154
|
@Test
|
1155
|
@DataSet
|
1156
|
public final void findByEveryThingFullText() throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {
|
1157
|
|
1158
|
refreshLuceneIndex();
|
1159
|
|
1160
|
Pager<SearchResult<TaxonBase>> pager;
|
1161
|
|
1162
|
pager = taxonService.findByEverythingFullText("genus", null, null, false, null, null, null, null); // --> 1
|
1163
|
Assert.assertEquals("Expecting 1 entity", 1, pager.getCount().intValue());
|
1164
|
|
1165
|
//FIXME FAILS: abies balamea is returned twice, see also testFullText_Grouping()
|
1166
|
pager = taxonService.findByEverythingFullText("Balsam", null, Arrays.asList(new Language[]{Language.GERMAN()}), false, null, null, null, null);
|
1167
|
logFreeTextSearchResults(pager, Level.DEBUG, null);
|
1168
|
Assert.assertEquals("expecting to find the Abies balsamea via the GERMAN DescriptionElements", 1, pager.getCount().intValue());
|
1169
|
|
1170
|
pager = taxonService.findByEverythingFullText("Abies", null, null, true, null, null, null, null);
|
1171
|
Assert.assertEquals("Expecting 8 entities", 8, pager.getCount().intValue());
|
1172
|
SearchResult<TaxonBase> searchResult = pager.getRecords().get(0);
|
1173
|
Assert.assertTrue("the map of highlighted fragments should contain at least one item", searchResult.getFieldHighlightMap().size() > 0);
|
1174
|
String[] fragments = searchResult.getFieldHighlightMap().values().iterator().next();
|
1175
|
Assert.assertTrue("first fragments should contains serch term", fragments[0].toLowerCase().contains("<b>abies</b>"));
|
1176
|
}
|
1177
|
|
1178
|
// @SuppressWarnings("rawtypes")
|
1179
|
// @Test
|
1180
|
// @DataSet
|
1181
|
// public final void benchmarkFindTaxaAndNamesHql() throws CorruptIndexException, IOException, ParseException {
|
1182
|
//
|
1183
|
// createRandomTaxonWithCommonName(NUM_OF_NEW_RADOM_ENTITIES);
|
1184
|
//
|
1185
|
// IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
|
1186
|
// configurator.setTitleSearchString("Wei"+UTF8.SHARP_S+"%");
|
1187
|
// configurator.setMatchMode(MatchMode.BEGINNING);
|
1188
|
// configurator.setDoTaxa(false);
|
1189
|
// configurator.setDoSynonyms(false);
|
1190
|
// configurator.setDoNamesWithoutTaxa(false);
|
1191
|
// configurator.setDoTaxaByCommonNames(true);
|
1192
|
//
|
1193
|
// Pager<IdentifiableEntity> pager;
|
1194
|
//
|
1195
|
// long startMillis = System.currentTimeMillis();
|
1196
|
// for (int indx = 0; indx < BENCHMARK_ROUNDS; indx++) {
|
1197
|
// pager = taxonService.findTaxaAndNames(configurator);
|
1198
|
// if (logger.isDebugEnabled()) {
|
1199
|
// logger.debug("[" + indx + "]" + pager.getRecords().get(0).getTitleCache());
|
1200
|
// }
|
1201
|
// }
|
1202
|
// double duration = ((double) (System.currentTimeMillis() - startMillis)) / BENCHMARK_ROUNDS;
|
1203
|
// logger.info("Benchmark result - [find taxon by CommonName via HQL] : " + duration + "ms (" + BENCHMARK_ROUNDS + " benchmark rounds )");
|
1204
|
// }
|
1205
|
|
1206
|
@SuppressWarnings("rawtypes")
|
1207
|
@Test
|
1208
|
@DataSet
|
1209
|
public final void benchmarkFindByCommonNameHql() throws CorruptIndexException, IOException, ParseException {
|
1210
|
|
1211
|
// printDataSet(System.err, new String[] { "TaxonBase" });
|
1212
|
|
1213
|
createRandomTaxonWithCommonName(NUM_OF_NEW_RADOM_ENTITIES);
|
1214
|
|
1215
|
IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl<>();
|
1216
|
configurator.setTitleSearchString("Wei"+UTF8.SHARP_S+"%");
|
1217
|
configurator.setMatchMode(MatchMode.BEGINNING);
|
1218
|
configurator.setDoTaxa(false);
|
1219
|
configurator.setDoSynonyms(false);
|
1220
|
configurator.setDoNamesWithoutTaxa(false);
|
1221
|
configurator.setDoTaxaByCommonNames(true);
|
1222
|
|
1223
|
Pager<IdentifiableEntity> pager;
|
1224
|
|
1225
|
long startMillis = System.currentTimeMillis();
|
1226
|
for (int indx = 0; indx < BENCHMARK_ROUNDS; indx++) {
|
1227
|
pager = taxonService.findTaxaAndNames(configurator);
|
1228
|
if (logger.isDebugEnabled()) {
|
1229
|
logger.debug("[" + indx + "]" + pager.getRecords().get(0).getTitleCache());
|
1230
|
}
|
1231
|
}
|
1232
|
double duration = ((double) (System.currentTimeMillis() - startMillis)) / BENCHMARK_ROUNDS;
|
1233
|
logger.info("Benchmark result - [find taxon by CommonName via HQL] : " + duration + "ms (" + BENCHMARK_ROUNDS + " benchmark rounds )");
|
1234
|
}
|
1235
|
|
1236
|
@SuppressWarnings("rawtypes")
|
1237
|
@Test
|
1238
|
@DataSet
|
1239
|
public final void benchmarkFindByCommonNameLucene() throws CorruptIndexException, IOException, ParseException {
|
1240
|
|
1241
|
createRandomTaxonWithCommonName(NUM_OF_NEW_RADOM_ENTITIES);
|
1242
|
|
1243
|
refreshLuceneIndex();
|
1244
|
|
1245
|
Pager<SearchResult<TaxonBase>> pager;
|
1246
|
|
1247
|
long startMillis = System.currentTimeMillis();
|
1248
|
for (int indx = 0; indx < BENCHMARK_ROUNDS; indx++) {
|
1249
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Wei"+UTF8.SHARP_S+"*", null, null, null, false, null, null, null, null);
|
1250
|
if (logger.isDebugEnabled()) {
|
1251
|
logger.debug("[" + indx + "]" + pager.getRecords().get(0).getEntity().getTitleCache());
|
1252
|
}
|
1253
|
}
|
1254
|
double duration = ((double) (System.currentTimeMillis() - startMillis)) / BENCHMARK_ROUNDS;
|
1255
|
logger.info("Benchmark result - [find taxon by CommonName via lucene] : " + duration + "ms (" + BENCHMARK_ROUNDS + " benchmark rounds )");
|
1256
|
}
|
1257
|
|
1258
|
/**
|
1259
|
* uncomment @Test annotation to create the dataset for this test
|
1260
|
*/
|
1261
|
@Override
|
1262
|
// @Test
|
1263
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="BlankDataSet.xml")
|
1264
|
public final void createTestDataSet() throws FileNotFoundException {
|
1265
|
|
1266
|
Classification europeanAbiesClassification = Classification.NewInstance("European Abies");
|
1267
|
europeanAbiesClassification.setUuid(UUID.fromString(CLASSIFICATION_UUID));
|
1268
|
classificationService.save(europeanAbiesClassification);
|
1269
|
|
1270
|
Classification alternativeClassification = Classification.NewInstance("Abies alternative");
|
1271
|
alternativeClassification.setUuid(UUID.fromString(CLASSIFICATION_ALT_UUID));
|
1272
|
classificationService.save(alternativeClassification);
|
1273
|
|
1274
|
Reference sec = ReferenceFactory.newBook();
|
1275
|
sec.setTitleCache("Kohlbecker, A., Testcase standart views, 2013", true);
|
1276
|
Reference sec_sensu = ReferenceFactory.newBook();
|
1277
|
sec_sensu.setTitleCache("Komarov, V. L., Flora SSSR 29", true);
|
1278
|
referenceService.save(sec);
|
1279
|
referenceService.save(sec_sensu);
|
1280
|
|
1281
|
BotanicalName n_abies = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
|
1282
|
n_abies.setNameCache("Abies", true);
|
1283
|
Taxon t_abies = Taxon.NewInstance(n_abies, sec);
|
1284
|
taxonService.save(t_abies);
|
1285
|
|
1286
|
BotanicalName n_abies_alba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1287
|
n_abies_alba.setNameCache("Abies alba", true);
|
1288
|
Taxon t_abies_alba = Taxon.NewInstance(n_abies_alba, sec);
|
1289
|
t_abies_alba.setUuid(UUID.fromString(ABIES_ALBA_UUID));
|
1290
|
taxonService.save(t_abies_alba);
|
1291
|
|
1292
|
BotanicalName n_abies_subalpina = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1293
|
n_abies_subalpina.setNameCache("Abies subalpina", true);
|
1294
|
Synonym s_abies_subalpina = Synonym.NewInstance(n_abies_subalpina, sec);
|
1295
|
taxonService.save(s_abies_subalpina);
|
1296
|
|
1297
|
BotanicalName n_abies_balsamea = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1298
|
n_abies_balsamea.setNameCache("Abies balsamea", true);
|
1299
|
Taxon t_abies_balsamea = Taxon.NewInstance(n_abies_balsamea, sec);
|
1300
|
t_abies_balsamea.setUuid(UUID.fromString(ABIES_BALSAMEA_UUID));
|
1301
|
t_abies_balsamea.addSynonym(s_abies_subalpina, SynonymType.SYNONYM_OF());
|
1302
|
taxonService.save(t_abies_balsamea);
|
1303
|
|
1304
|
BotanicalName n_abies_grandis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1305
|
n_abies_grandis.setNameCache("Abies grandis", true);
|
1306
|
Taxon t_abies_grandis = Taxon.NewInstance(n_abies_grandis, sec);
|
1307
|
taxonService.save(t_abies_grandis);
|
1308
|
|
1309
|
BotanicalName n_abies_kawakamii = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1310
|
n_abies_kawakamii.setNameCache("Abies kawakamii", true);
|
1311
|
Taxon t_abies_kawakamii = Taxon.NewInstance(n_abies_kawakamii, sec);
|
1312
|
t_abies_kawakamii.getTitleCache();
|
1313
|
taxonService.save(t_abies_kawakamii);
|
1314
|
|
1315
|
// abies_kawakamii_sensu_komarov as missapplied name for t_abies_balsamea
|
1316
|
Taxon t_abies_kawakamii_sensu_komarov = Taxon.NewInstance(n_abies_kawakamii, sec_sensu);
|
1317
|
taxonService.save(t_abies_kawakamii_sensu_komarov);
|
1318
|
t_abies_kawakamii_sensu_komarov.addTaxonRelation(t_abies_balsamea, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null);
|
1319
|
taxonService.saveOrUpdate(t_abies_kawakamii_sensu_komarov);
|
1320
|
|
1321
|
BotanicalName n_abies_lasiocarpa = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1322
|
n_abies_lasiocarpa.setNameCache("Abies lasiocarpa", true);
|
1323
|
Taxon t_abies_lasiocarpa = Taxon.NewInstance(n_abies_lasiocarpa, sec);
|
1324
|
taxonService.save(t_abies_lasiocarpa);
|
1325
|
|
1326
|
// add taxa to classifications
|
1327
|
europeanAbiesClassification.addChildTaxon(t_abies_balsamea, null, null);
|
1328
|
alternativeClassification.addChildTaxon(t_abies_lasiocarpa, null, null);
|
1329
|
classificationService.saveOrUpdate(europeanAbiesClassification);
|
1330
|
classificationService.saveOrUpdate(alternativeClassification);
|
1331
|
|
1332
|
//
|
1333
|
// Description
|
1334
|
//
|
1335
|
TaxonDescription d_abies_alba = TaxonDescription.NewInstance(t_abies_alba);
|
1336
|
TaxonDescription d_abies_balsamea = TaxonDescription.NewInstance(t_abies_balsamea);
|
1337
|
|
1338
|
d_abies_alba.setUuid(UUID.fromString(D_ABIES_ALBA_UUID));
|
1339
|
d_abies_balsamea.setUuid(UUID.fromString(D_ABIES_BALSAMEA_UUID));
|
1340
|
|
1341
|
|
1342
|
// CommonTaxonName
|
1343
|
d_abies_alba.addElement(CommonTaxonName.NewInstance("Wei"+UTF8.SHARP_S+"tanne", Language.GERMAN()));
|
1344
|
d_abies_alba.addElement(CommonTaxonName.NewInstance("silver fir", Language.ENGLISH()));
|
1345
|
d_abies_alba.addElement(Distribution
|
1346
|
.NewInstance(
|
1347
|
germany,
|
1348
|
PresenceAbsenceTerm.NATIVE()));
|
1349
|
d_abies_alba.addElement(Distribution
|
1350
|
.NewInstance(
|
1351
|
russia,
|
1352
|
PresenceAbsenceTerm.ABSENT()));
|
1353
|
|
1354
|
// TextData
|
1355
|
d_abies_balsamea
|
1356
|
.addElement(TextData
|
1357
|
.NewInstance(
|
1358
|
"Die Balsam-Tanne (Abies balsamea) ist eine Pflanzenart aus der Gattung der Tannen (Abies). Sie wächst im nordöstlichen Nordamerika, wo sie sowohl Tief- als auch Bergland besiedelt. Sie gilt als relativ anspruchslos gegenüber dem Standort und ist frosthart. In vielen Teilen des natürlichen Verbreitungsgebietes stellt sie die Klimaxbaumart dar.",
|
1359
|
Language.GERMAN(), null));
|
1360
|
d_abies_balsamea
|
1361
|
.addElement(CommonTaxonName
|
1362
|
.NewInstance(
|
1363
|
"Balsam-Tanne",
|
1364
|
Language.GERMAN(), null));
|
1365
|
|
1366
|
d_abies_balsamea
|
1367
|
.addElement(Distribution
|
1368
|
.NewInstance(
|
1369
|
canada,
|
1370
|
PresenceAbsenceTerm.PRESENT()));
|
1371
|
|
1372
|
d_abies_balsamea
|
1373
|
.addElement(Distribution
|
1374
|
.NewInstance(
|
1375
|
germany,
|
1376
|
PresenceAbsenceTerm.NATIVE()));
|
1377
|
|
1378
|
d_abies_balsamea
|
1379
|
.addElement(TextData
|
1380
|
.NewInstance(
|
1381
|
TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_LONG,
|
1382
|
Language.RUSSIAN(), null));
|
1383
|
d_abies_balsamea
|
1384
|
.addElement(CommonTaxonName
|
1385
|
.NewInstance(
|
1386
|
TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_SHORT,
|
1387
|
Language.RUSSIAN(), null));
|
1388
|
descriptionService.saveOrUpdate(d_abies_balsamea);
|
1389
|
|
1390
|
setComplete();
|
1391
|
endTransaction();
|
1392
|
|
1393
|
|
1394
|
writeDbUnitDataSetFile(new String[] {
|
1395
|
"TAXONBASE", "TAXONNAMEBASE",
|
1396
|
"TAXONRELATIONSHIP",
|
1397
|
"REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
|
1398
|
"AGENTBASE", "HOMOTYPICALGROUP",
|
1399
|
"CLASSIFICATION", "TAXONNODE",
|
1400
|
"LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING",
|
1401
|
"HIBERNATE_SEQUENCES" // IMPORTANT!!!
|
1402
|
});
|
1403
|
|
1404
|
}
|
1405
|
|
1406
|
/**
|
1407
|
*
|
1408
|
*/
|
1409
|
private void refreshLuceneIndex() {
|
1410
|
|
1411
|
// commitAndStartNewTransaction(null);
|
1412
|
commit();
|
1413
|
endTransaction();
|
1414
|
indexer.purge(DefaultProgressMonitor.NewInstance());
|
1415
|
indexer.reindex(typesToIndex, DefaultProgressMonitor.NewInstance());
|
1416
|
startNewTransaction();
|
1417
|
// commitAndStartNewTransaction(null);
|
1418
|
}
|
1419
|
|
1420
|
/**
|
1421
|
* @param numberOfNew
|
1422
|
*
|
1423
|
*/
|
1424
|
private void createRandomTaxonWithCommonName(int numberOfNew) {
|
1425
|
|
1426
|
logger.debug(String.format("creating %1$s random taxan with CommonName", numberOfNew));
|
1427
|
|
1428
|
commitAndStartNewTransaction(null);
|
1429
|
|
1430
|
Reference sec = ReferenceFactory.newBook();
|
1431
|
referenceService.save(sec);
|
1432
|
|
1433
|
for (int i = numberOfNew; i < numberOfNew; i++) {
|
1434
|
RandomStringUtils.randomAlphabetic(10);
|
1435
|
String radomName = RandomStringUtils.randomAlphabetic(5) + " " + RandomStringUtils.randomAlphabetic(10);
|
1436
|
String radomCommonName = RandomStringUtils.randomAlphabetic(10);
|
1437
|
|
1438
|
BotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
1439
|
name.setNameCache(radomName, true);
|
1440
|
Taxon taxon = Taxon.NewInstance(name, sec);
|
1441
|
taxonService.save(taxon);
|
1442
|
|
1443
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
1444
|
description.addElement(CommonTaxonName.NewInstance(radomCommonName, Language.GERMAN()));
|
1445
|
descriptionService.save(description);
|
1446
|
}
|
1447
|
|
1448
|
commitAndStartNewTransaction(null);
|
1449
|
}
|
1450
|
|
1451
|
private <T extends CdmBase> void logFreeTextSearchResults(Pager<SearchResult<T>> pager, Level level, String[] docFields){
|
1452
|
if(level == null){
|
1453
|
level = Level.DEBUG;
|
1454
|
}
|
1455
|
if(logger.isEnabledFor(level)){
|
1456
|
StringBuilder b = new StringBuilder();
|
1457
|
b.append("\n");
|
1458
|
int i = 0;
|
1459
|
for(SearchResult sr : pager.getRecords()){
|
1460
|
|
1461
|
b.append(" ").append(i++).append(" - ");
|
1462
|
b.append("score:").append(sr.getScore()).append(", ");
|
1463
|
|
1464
|
if(docFields != null){
|
1465
|
b.append("docs : ");
|
1466
|
for(Document doc : sr.getDocs()) {
|
1467
|
b.append("<");
|
1468
|
for(String f : docFields){
|
1469
|
b.append(f).append(":").append(Arrays.toString(doc.getValues(f)));
|
1470
|
}
|
1471
|
b.append(">");
|
1472
|
}
|
1473
|
}
|
1474
|
|
1475
|
CdmBase entity = sr.getEntity();
|
1476
|
if(entity == null){
|
1477
|
b.append("NULL");
|
1478
|
} else {
|
1479
|
b.append(entity.getClass().getSimpleName()).
|
1480
|
append(" [").append(entity.getId()).
|
1481
|
append(" | ").append(entity.getUuid()).append("] : ").
|
1482
|
append(entity.toString());
|
1483
|
|
1484
|
}
|
1485
|
b.append("\n");
|
1486
|
}
|
1487
|
logger.log(level, b);
|
1488
|
}
|
1489
|
}
|
1490
|
|
1491
|
}
|