1 package org.enhydra.snapper.wrapper.lucene; 2 3 4 6 7 import java.io.File ; 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 11 import org.apache.lucene.document.Document; 12 import org.apache.lucene.document.Field; 13 14 15 16 17 public class FileDocument { 18 32 public static Document Document(File f, String contents, String type, String title, int maxSize, String properties, String metadata) 33 throws java.io.FileNotFoundException { 34 35 36 Document doc = new Document(); 38 39 doc.add(Field.Text("path", f.getPath())); 42 String str = f.getPath(); 43 str = replace(str, "\\","."); 44 doc.add(Field.Keyword("pt", str)); 45 46 52 Field typeField = Field.Keyword("type", type); 54 float bostFactor1 = 0.1f; 55 typeField.setBoost(bostFactor1); 56 doc.add(typeField); 57 58 doc.add(Field.Text("title", title)); 59 doc.add(Field.UnIndexed("fileName", f.getName())); 60 doc.add(Field.Text("properties", properties)); 61 doc.add(Field.Text("metadata", metadata)); 62 63 65 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd"); 66 Date last = new Date (f.lastModified()); 67 String dateString = formatter.format(last); 68 doc.add(Field.Keyword("modified", dateString)); 69 70 72 String fullcontents = ""; 78 if (contents.length() > maxSize){ 79 fullcontents = contents.substring(0,maxSize); 80 } 81 else fullcontents = contents; 82 doc.add(Field.UnStored("contents", contents)); 83 doc.add(Field.Text("fullcontents", fullcontents)); 84 85 return doc; 87 } 88 89 90 public static Document Document(long timestamp, String path, String contents, String type, String title, int maxSize, String properties, String metadata, String fileName) 91 throws java.io.FileNotFoundException { 92 93 Document doc = new Document(); 95 96 doc.add(Field.Text("path", path)); 99 String str = path; 100 str = replace(str, "\\","."); 101 doc.add(Field.Keyword("pt", str)); 102 103 107 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd"); 108 Date last = new Date (timestamp); 109 String dateString = formatter.format(last); 110 doc.add(Field.Keyword("modified", dateString)); 111 112 Field typeField = Field.Keyword("type", type); 113 float bostFactor1 = 0.1f; 114 typeField.setBoost(bostFactor1); 115 doc.add(typeField); 116 doc.add(Field.Text("title", title)); 118 doc.add(Field.Text("metadata", metadata)); 119 120 String fullcontents = ""; 126 if (contents.length() > maxSize){ 127 fullcontents = contents.substring(0,maxSize); 128 } 129 else fullcontents = contents; 130 doc.add(Field.UnStored("contents", contents)); 131 doc.add(Field.Text("properties", properties)); 132 doc.add(Field.Text("fullcontents", fullcontents)); 133 doc.add(Field.UnIndexed("fileName", fileName)); 134 135 136 return doc; 138 } 139 140 private FileDocument() {} 141 142 static String replace(String s, String one, String another) { 143 if (s.equals("")) return ""; 145 String res = ""; 146 int i = s.indexOf(one,0); 147 int lastpos = 0; 148 while (i != -1) { 149 res += s.substring(lastpos,i) + another; 150 lastpos = i + one.length(); 151 i = s.indexOf(one,lastpos); 152 } 153 res += s.substring(lastpos); return res; 155 } 156 } 157 158 | Popular Tags |