extensions/net.sf.basedb.varsearch/trunk/src/net/sf/basedb/varsearch/index/DocumentCreator.java

Code
Comments
Other
Rev Date Author Line
6540 17 Jan 22 nicklas 1 package net.sf.basedb.varsearch.index;
6540 17 Jan 22 nicklas 2
6540 17 Jan 22 nicklas 3 import org.apache.lucene.document.Document;
6540 17 Jan 22 nicklas 4 import org.apache.lucene.document.FloatPoint;
6540 17 Jan 22 nicklas 5 import org.apache.lucene.document.IntPoint;
6540 17 Jan 22 nicklas 6 import org.apache.lucene.document.LongPoint;
6540 17 Jan 22 nicklas 7 import org.apache.lucene.document.NumericDocValuesField;
6540 17 Jan 22 nicklas 8 import org.apache.lucene.document.StoredField;
6540 17 Jan 22 nicklas 9 import org.apache.lucene.document.StringField;
6540 17 Jan 22 nicklas 10 import org.apache.lucene.document.Field.Store;
6540 17 Jan 22 nicklas 11 import org.apache.lucene.index.IndexableField;
6540 17 Jan 22 nicklas 12
6545 21 Jan 22 nicklas 13 import net.sf.basedb.core.ArrayDesign;
6540 17 Jan 22 nicklas 14 import net.sf.basedb.core.File;
6540 17 Jan 22 nicklas 15 import net.sf.basedb.core.RawBioAssay;
6540 17 Jan 22 nicklas 16 import net.sf.basedb.varsearch.vcf.VcfParser.VcfLine;
6540 17 Jan 22 nicklas 17
6540 17 Jan 22 nicklas 18 /**
6540 17 Jan 22 nicklas 19   Helper class for creating Document instances that
6540 17 Jan 22 nicklas 20   can be added to a Lucene index.
6540 17 Jan 22 nicklas 21
6540 17 Jan 22 nicklas 22   @author nicklas
6540 17 Jan 22 nicklas 23   @since 1.5
6540 17 Jan 22 nicklas 24 */
6540 17 Jan 22 nicklas 25 public class DocumentCreator
6540 17 Jan 22 nicklas 26 {
6540 17 Jan 22 nicklas 27   private final Document doc;
6540 17 Jan 22 nicklas 28
6540 17 Jan 22 nicklas 29   public DocumentCreator()
6540 17 Jan 22 nicklas 30   {
6540 17 Jan 22 nicklas 31     this.doc = new Document();
6540 17 Jan 22 nicklas 32   }
6540 17 Jan 22 nicklas 33   
6540 17 Jan 22 nicklas 34   /**
6540 17 Jan 22 nicklas 35     Get the document that can be added to the Lucene index.
6540 17 Jan 22 nicklas 36     Use the add*() methods to add information that should be indexed.
6540 17 Jan 22 nicklas 37   */
6540 17 Jan 22 nicklas 38   public Document doc()
6540 17 Jan 22 nicklas 39   {
6540 17 Jan 22 nicklas 40     return doc;
6540 17 Jan 22 nicklas 41   }
6540 17 Jan 22 nicklas 42
6540 17 Jan 22 nicklas 43   /**
6540 17 Jan 22 nicklas 44     Add raw bioassay information (name and id) to the index. The
6540 17 Jan 22 nicklas 45     name is stored and the id is added as a DocValuesField
6540 17 Jan 22 nicklas 46     (which is has very good performance when reading).
6540 17 Jan 22 nicklas 47   */
6540 17 Jan 22 nicklas 48   public void addRawBioAssayFields(RawBioAssay rba, String prefix)
6540 17 Jan 22 nicklas 49   {
6540 17 Jan 22 nicklas 50     doc.add(new StringField(prefix + "Name", rba.getName(), Store.YES));
6540 17 Jan 22 nicklas 51     doc.add(new IntPoint(prefix+"Id", rba.getId()));
6540 17 Jan 22 nicklas 52     doc.add(new NumericDocValuesField(prefix+"Id", rba.getId()));
6540 17 Jan 22 nicklas 53   }
6540 17 Jan 22 nicklas 54   
6540 17 Jan 22 nicklas 55   /**
6540 17 Jan 22 nicklas 56     Add file ID and line number (optional) to the index.
6540 17 Jan 22 nicklas 57   */
6545 21 Jan 22 nicklas 58   void addArrayDesignFields(ArrayDesign design)
6545 21 Jan 22 nicklas 59   {
6545 21 Jan 22 nicklas 60     doc.add(new IntPoint("designId", design.getId()));
6545 21 Jan 22 nicklas 61     doc.add(new StoredField("designId", design.getId()));
6545 21 Jan 22 nicklas 62   }
6545 21 Jan 22 nicklas 63   
6545 21 Jan 22 nicklas 64   /**
6545 21 Jan 22 nicklas 65     Add file ID and line number (optional) to the index.
6545 21 Jan 22 nicklas 66   */
6540 17 Jan 22 nicklas 67   void addFileFields(File file, VcfLine line)
6540 17 Jan 22 nicklas 68   {
6540 17 Jan 22 nicklas 69     doc.add(new IntPoint("file", file.getId()));
6540 17 Jan 22 nicklas 70     doc.add(new StoredField("file", file.getId()));
6540 17 Jan 22 nicklas 71     if (line != null) doc.add(new StoredField("line", line.lineNo()));
6540 17 Jan 22 nicklas 72   }
6540 17 Jan 22 nicklas 73
6540 17 Jan 22 nicklas 74   
6540 17 Jan 22 nicklas 75   /**
6540 17 Jan 22 nicklas 76     Add the value as a string field.
6540 17 Jan 22 nicklas 77     
6540 17 Jan 22 nicklas 78     @param name The name of field in the index
6540 17 Jan 22 nicklas 79     @param val The value to add, if null it is not added
6540 17 Jan 22 nicklas 80     @param store Store.YES or Store.NO indicating if the 
6540 17 Jan 22 nicklas 81       value should also be stored in the index
6540 17 Jan 22 nicklas 82     @return The value
6540 17 Jan 22 nicklas 83   */
6540 17 Jan 22 nicklas 84   String addStringField(String name, String val, Store store)
6540 17 Jan 22 nicklas 85   {
6540 17 Jan 22 nicklas 86     if (val != null)
6540 17 Jan 22 nicklas 87     {
6540 17 Jan 22 nicklas 88       doc.add(new StringField(name, val, store));
6540 17 Jan 22 nicklas 89     }
6540 17 Jan 22 nicklas 90     return val;
6540 17 Jan 22 nicklas 91   }
6540 17 Jan 22 nicklas 92
6540 17 Jan 22 nicklas 93   /**
6540 17 Jan 22 nicklas 94     Add the value as an integer field.
6540 17 Jan 22 nicklas 95     
6540 17 Jan 22 nicklas 96     @param name The name of field in the index
6540 17 Jan 22 nicklas 97     @param val The value to add, if null it is not added
6540 17 Jan 22 nicklas 98     @param store Store.YES or Store.NO indicating if the 
6540 17 Jan 22 nicklas 99       value should also be stored in the index
6540 17 Jan 22 nicklas 100     @return The value
6540 17 Jan 22 nicklas 101   */
6540 17 Jan 22 nicklas 102   Integer addIntField(String name, Integer val, Store store)
6540 17 Jan 22 nicklas 103   {
6540 17 Jan 22 nicklas 104     if (val != null)
6540 17 Jan 22 nicklas 105     {
6540 17 Jan 22 nicklas 106       doc.add(new IntPoint(name, val));
6540 17 Jan 22 nicklas 107       if (store == Store.YES)
6540 17 Jan 22 nicklas 108       {
6540 17 Jan 22 nicklas 109         doc.add(new StoredField(name, val));
6540 17 Jan 22 nicklas 110       }
6540 17 Jan 22 nicklas 111     }
6540 17 Jan 22 nicklas 112     return val;
6540 17 Jan 22 nicklas 113   }
6540 17 Jan 22 nicklas 114
6540 17 Jan 22 nicklas 115   /**
6540 17 Jan 22 nicklas 116     Add the value as a long field
6540 17 Jan 22 nicklas 117     
6540 17 Jan 22 nicklas 118     @param name The name of field in the index
6540 17 Jan 22 nicklas 119     @param val The value to add, if null it is not added
6540 17 Jan 22 nicklas 120     @param store Store.YES or Store.NO indicating if the 
6540 17 Jan 22 nicklas 121       value should also be stored in the index
6540 17 Jan 22 nicklas 122   */
6540 17 Jan 22 nicklas 123   public Long addLongField(String name, Long val, Store store)
6540 17 Jan 22 nicklas 124   {
6540 17 Jan 22 nicklas 125     if (val != null)
6540 17 Jan 22 nicklas 126     {
6540 17 Jan 22 nicklas 127       doc.add(new LongPoint(name, val));
6540 17 Jan 22 nicklas 128       if (store == Store.YES)
6540 17 Jan 22 nicklas 129       {
6540 17 Jan 22 nicklas 130         doc.add(new StoredField(name, val));
6540 17 Jan 22 nicklas 131       }
6540 17 Jan 22 nicklas 132     }
6540 17 Jan 22 nicklas 133     return val;
6540 17 Jan 22 nicklas 134   }
6540 17 Jan 22 nicklas 135   
6540 17 Jan 22 nicklas 136   /**
6540 17 Jan 22 nicklas 137     Add the value as a float field.
6540 17 Jan 22 nicklas 138     
6540 17 Jan 22 nicklas 139     @param name The name of field in the index
6540 17 Jan 22 nicklas 140     @param val The value to add, if null it is not added
6540 17 Jan 22 nicklas 141     @param store A string representation that should be stored in the index 
6540 17 Jan 22 nicklas 142       or null if the value should not be stored
6540 17 Jan 22 nicklas 143   */
6540 17 Jan 22 nicklas 144   public Float addFloatField(String name, Float val, String store)
6540 17 Jan 22 nicklas 145   {
6540 17 Jan 22 nicklas 146     if (val != null)
6540 17 Jan 22 nicklas 147     {
6540 17 Jan 22 nicklas 148       doc.add(new FloatPoint(name, val));
6540 17 Jan 22 nicklas 149       if (store != null)
6540 17 Jan 22 nicklas 150       {
6540 17 Jan 22 nicklas 151         doc.add(new StoredField(name, store));
6540 17 Jan 22 nicklas 152       }
6540 17 Jan 22 nicklas 153     }
6540 17 Jan 22 nicklas 154     return val;
6540 17 Jan 22 nicklas 155   }
6540 17 Jan 22 nicklas 156   
6540 17 Jan 22 nicklas 157   /**
6540 17 Jan 22 nicklas 158     Add a field to the index.
6540 17 Jan 22 nicklas 159   */
6540 17 Jan 22 nicklas 160   public void addField(IndexableField field)
6540 17 Jan 22 nicklas 161   {
6540 17 Jan 22 nicklas 162     if (field != null) doc.add(field);
6540 17 Jan 22 nicklas 163   }
6540 17 Jan 22 nicklas 164
6540 17 Jan 22 nicklas 165 }