Project

General

Profile

Download (3.84 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2012 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
package eu.etaxonomy.cdm.hibernate.search;
10

    
11
import org.apache.lucene.document.Document;
12
import org.apache.lucene.document.Field;
13
import org.apache.lucene.document.Field.Index;
14
import org.apache.lucene.document.Field.Store;
15
import org.apache.lucene.document.Field.TermVector;
16
import org.apache.lucene.document.SortedDocValuesField;
17
import org.apache.lucene.document.StringField;
18
import org.apache.lucene.util.BytesRef;
19
import org.hibernate.search.bridge.LuceneOptions;
20
import org.hibernate.search.bridge.TwoWayFieldBridge;
21

    
22
/**
23
 * This {@link TwoWayFieldBridge} allows to efficiently query for associated
24
 * entities which are not null. This field bridge works the following way:
25
 * <p>
26
 * It adds the id field to the document as if it would be done without the
27
 * intervention of this class, all field attributes are preserved, additionally
28
 * this field bridge also adds a field named <code>id.notNull</code> and stores
29
 * the term "true" for this field. So all associated entities which are not null
30
 * can now be queried by searching for <code>+id.notNull:true</code> which is
31
 * much more efficient than using range queries.
32
 * <p>
33
 * The <code>id.notNull</code> is stored with the following attributes :
34
 * {@link Store.NO},{@link Index.NOT_ANALYZED}, {@link TermVector.NO}.
35
 *
36
 * @author a.kohlbecker
37
 * @date Sep 21, 2012
38
 *
39
 */
40
public class NotNullAwareIdBridge implements TwoWayFieldBridge {
41

    
42
    public static final String NOT_NULL_VALUE = "1";
43
    public static final String NOT_NULL_FIELD_NAME = "notNull";
44

    
45
    public static String NULL_STRING = "";
46

    
47

    
48
    /**
49
     * @param name
50
     * @return
51
     */
52
    public static String notNullField(String name) {
53
        return name + "." + NOT_NULL_FIELD_NAME;
54
    }
55

    
56
    /* (non-Javadoc)
57
     * @see org.hibernate.search.bridge.FieldBridge#set(java.lang.String, java.lang.Object, org.apache.lucene.document.Document, org.hibernate.search.bridge.LuceneOptions)
58
     */
59
    @Override
60
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
61

    
62
        /*
63
         * DocumentBuilderIndexedEntity<T>.buildDocumentFields(Object, Document, PropertiesMetadata, Map<String,String>, Set<String>)
64
         * is adding the idField a second time even if it has already been set by an idFieldBrige. this might be fixed in a
65
         * more recent version of hibernate! TODO after hibernate update: check if we can remove this extra condition.
66
         * We are avoiding this by checking the document:
67
         *
68
         */
69
        if(name.endsWith("id") && document.getField(name) != null) { // id already set?
70
            return;
71
        }
72

    
73
        Field field = new StringField(
74
                name,
75
                String.valueOf(value.toString()),
76
                luceneOptions.getStore());
77
        document.add(field);
78

    
79
        Field sort_field = new SortedDocValuesField(
80
                name + "__sort",
81
                new BytesRef(value.toString()));
82
        LuceneDocumentUtility.setOrReplaceDocValueField(sort_field, document);
83

    
84
        Field notNullField = new StringField(
85
                notNullField(name),
86
                String.valueOf(NOT_NULL_VALUE),
87
                Store.NO
88
                );
89
        document.add(notNullField);
90
    }
91

    
92
    @Override
93
    public Object get(String name, Document document) {
94
        return document.get(name);
95
    }
96

    
97
    /* (non-Javadoc)
98
     * @see org.hibernate.search.bridge.TwoWayFieldBridge#objectToString(java.lang.Object)
99
     */
100
    @Override
101
    public String objectToString(Object object) {
102
        if(object == null){
103
            return NULL_STRING;
104
        } else {
105
            return object.toString();
106
        }
107
    }
108

    
109
}
(13-13/18)