Project

General

Profile

Download (3.69 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2012 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.hibernate.search;
11

    
12
import org.apache.lucene.document.Document;
13
import org.apache.lucene.document.Field;
14
import org.apache.lucene.document.Field.Index;
15
import org.apache.lucene.document.Field.Store;
16
import org.apache.lucene.document.Field.TermVector;
17
import org.hibernate.search.bridge.FieldBridge;
18
import org.hibernate.search.bridge.LuceneOptions;
19
import org.hibernate.search.bridge.TwoWayFieldBridge;
20

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

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

    
44
    public static String NULL_STRING = "";
45

    
46

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

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

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

    
72
        Field field = new Field(name,
73
                String.valueOf(value.toString()),
74
                luceneOptions.getStore(),
75
                luceneOptions.getIndex(),
76
                luceneOptions.getTermVector());
77
        field.setBoost(luceneOptions.getBoost());
78
        document.add(field);
79

    
80
        Field notNullField = new Field(notNullField(name),
81
                String.valueOf(NOT_NULL_VALUE),
82
                Store.NO,
83
                Index.NOT_ANALYZED,
84
                TermVector.NO);
85
        document.add(notNullField);
86
    }
87

    
88
    @Override
89
    public Object get(String name, Document document) {
90
        return document.get(name);
91
    }
92

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

    
105
}
(12-12/17)