1
|
package eu.etaxonomy.cdm.hibernate.search;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.apache.lucene.document.Document;
|
5
|
import org.apache.lucene.document.Field;
|
6
|
import org.apache.lucene.document.SortedDocValuesField;
|
7
|
import org.apache.lucene.util.BytesRef;
|
8
|
import org.hibernate.search.bridge.LuceneOptions;
|
9
|
|
10
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
11
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
12
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
13
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
14
|
|
15
|
/**
|
16
|
* The <code>GroupByTaxonClassBridge</code> adds the field
|
17
|
* <code>groupby_taxon.id</code> to the lucene document which can be used to
|
18
|
* group search results based on the taxon which is associated with the indexed
|
19
|
* cdm entity. So any cdm class which is involved in querying for taxa must
|
20
|
* used this class bridge, e.g.:
|
21
|
*
|
22
|
* <pre>
|
23
|
@ClassBridge(impl=GroupByTaxonClassBridge.class)}
|
24
|
</pre>
|
25
|
*
|
26
|
* or
|
27
|
* <pre>
|
28
|
@ClassBridges({
|
29
|
@ClassBridge(impl=GroupByTaxonClassBridge.class),
|
30
|
@ClassBridge(impl=DescriptionBaseClassBridge.class),
|
31
|
})
|
32
|
}
|
33
|
* </pre>
|
34
|
*
|
35
|
* @author a.kohlbecker
|
36
|
* @date Oct 4, 2012
|
37
|
*
|
38
|
*/
|
39
|
public class GroupByTaxonClassBridge extends AbstractClassBridge{
|
40
|
|
41
|
public static final String GROUPBY_TAXON_FIELD = "groupby_taxon.id__sort";
|
42
|
|
43
|
public static final Logger logger = Logger.getLogger(GroupByTaxonClassBridge.class);
|
44
|
|
45
|
public GroupByTaxonClassBridge() {
|
46
|
super();
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* @param entity
|
51
|
* @return
|
52
|
*/
|
53
|
protected Taxon getAssociatedTaxon(Object entity) {
|
54
|
|
55
|
if (entity instanceof DescriptionBase<?>) {
|
56
|
if (entity instanceof TaxonDescription) {
|
57
|
return ((TaxonDescription) entity).getTaxon();
|
58
|
}
|
59
|
return null;
|
60
|
}
|
61
|
if (entity instanceof TaxonBase){
|
62
|
if (entity instanceof Taxon) {
|
63
|
return (Taxon)entity;
|
64
|
}
|
65
|
return null;
|
66
|
}
|
67
|
|
68
|
throw new RuntimeException("CDM class " + entity.getClass() + " not yet supported");
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
|
73
|
|
74
|
Taxon taxon = getAssociatedTaxon(value);
|
75
|
if(taxon != null){
|
76
|
Field field = new SortedDocValuesField(GROUPBY_TAXON_FIELD, new BytesRef(String.valueOf(taxon.getId())));
|
77
|
LuceneDocumentUtility.setOrReplaceDocValueField(field, document);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
}
|