1 package org.apache.lucene.document; 2 3 18 19 import java.io.Reader ; 20 import java.io.Serializable ; 21 import java.util.Date ; 22 23 import org.apache.lucene.index.IndexReader; 24 import org.apache.lucene.search.Hits; 25 import org.apache.lucene.search.Similarity; 26 import org.apache.lucene.util.Parameter; 27 28 35 36 public final class Field implements Serializable { 37 private String name = "body"; 38 39 private Object fieldsData = null; 41 42 private boolean storeTermVector = false; 43 private boolean storeOffsetWithTermVector = false; 44 private boolean storePositionWithTermVector = false; 45 private boolean omitNorms = false; 46 private boolean isStored = false; 47 private boolean isIndexed = true; 48 private boolean isTokenized = true; 49 private boolean isBinary = false; 50 private boolean isCompressed = false; 51 52 private float boost = 1.0f; 53 54 55 public static final class Store extends Parameter implements Serializable { 56 57 private Store(String name) { 58 super(name); 59 } 60 61 64 public static final Store COMPRESS = new Store("COMPRESS"); 65 66 71 public static final Store YES = new Store("YES"); 72 73 74 public static final Store NO = new Store("NO"); 75 } 76 77 78 public static final class Index extends Parameter implements Serializable { 79 80 private Index(String name) { 81 super(name); 82 } 83 84 87 public static final Index NO = new Index("NO"); 88 89 93 public static final Index TOKENIZED = new Index("TOKENIZED"); 94 95 99 public static final Index UN_TOKENIZED = new Index("UN_TOKENIZED"); 100 101 107 public static final Index NO_NORMS = new Index("NO_NORMS"); 108 109 } 110 111 112 public static final class TermVector extends Parameter implements Serializable { 113 114 private TermVector(String name) { 115 super(name); 116 } 117 118 120 public static final TermVector NO = new TermVector("NO"); 121 122 124 public static final TermVector YES = new TermVector("YES"); 125 126 131 public static final TermVector WITH_POSITIONS = new TermVector("WITH_POSITIONS"); 132 133 138 public static final TermVector WITH_OFFSETS = new TermVector("WITH_OFFSETS"); 139 140 147 public static final TermVector WITH_POSITIONS_OFFSETS = new TermVector("WITH_POSITIONS_OFFSETS"); 148 } 149 150 166 public void setBoost(float boost) { 167 this.boost = boost; 168 } 169 170 181 public float getBoost() { 182 return boost; 183 } 184 185 189 public static final Field Keyword(String name, String value) { 190 return new Field(name, value, true, true, false); 191 } 192 193 197 public static final Field UnIndexed(String name, String value) { 198 return new Field(name, value, true, false, false); 199 } 200 201 206 public static final Field Text(String name, String value) { 207 return Text(name, value, false); 208 } 209 210 214 public static final Field Keyword(String name, Date value) { 215 return new Field(name, DateField.dateToString(value), true, true, false); 216 } 217 218 223 public static final Field Text(String name, String value, boolean storeTermVector) { 224 return new Field(name, value, true, true, true, storeTermVector); 225 } 226 227 231 public static final Field UnStored(String name, String value) { 232 return UnStored(name, value, false); 233 } 234 235 239 public static final Field UnStored(String name, String value, boolean storeTermVector) { 240 return new Field(name, value, false, true, true, storeTermVector); 241 } 242 243 247 public static final Field Text(String name, Reader value) { 248 return Text(name, value, false); 249 } 250 251 256 public static final Field Text(String name, Reader value, boolean storeTermVector) { 257 Field f = new Field(name, value); 258 f.storeTermVector = storeTermVector; 259 return f; 260 } 261 262 265 public String name() { return name; } 266 267 270 public String stringValue() { return fieldsData instanceof String ? (String )fieldsData : null; } 271 272 275 public Reader readerValue() { return fieldsData instanceof Reader ? (Reader )fieldsData : null; } 276 277 280 public byte[] binaryValue() { return fieldsData instanceof byte[] ? (byte[])fieldsData : null; } 281 282 294 public Field(String name, String value, Store store, Index index) { 295 this(name, value, store, index, TermVector.NO); 296 } 297 298 315 public Field(String name, String value, Store store, Index index, TermVector termVector) { 316 if (name == null) 317 throw new NullPointerException ("name cannot be null"); 318 if (value == null) 319 throw new NullPointerException ("value cannot be null"); 320 if (index == Index.NO && store == Store.NO) 321 throw new IllegalArgumentException ("it doesn't make sense to have a field that " 322 + "is neither indexed nor stored"); 323 if (index == Index.NO && termVector != TermVector.NO) 324 throw new IllegalArgumentException ("cannot store term vector information " 325 + "for a field that is not indexed"); 326 327 this.name = name.intern(); this.fieldsData = value; 329 330 if (store == Store.YES){ 331 this.isStored = true; 332 this.isCompressed = false; 333 } 334 else if (store == Store.COMPRESS) { 335 this.isStored = true; 336 this.isCompressed = true; 337 } 338 else if (store == Store.NO){ 339 this.isStored = false; 340 this.isCompressed = false; 341 } 342 else 343 throw new IllegalArgumentException ("unknown store parameter " + store); 344 345 if (index == Index.NO) { 346 this.isIndexed = false; 347 this.isTokenized = false; 348 } else if (index == Index.TOKENIZED) { 349 this.isIndexed = true; 350 this.isTokenized = true; 351 } else if (index == Index.UN_TOKENIZED) { 352 this.isIndexed = true; 353 this.isTokenized = false; 354 } else if (index == Index.NO_NORMS) { 355 this.isIndexed = true; 356 this.isTokenized = false; 357 this.omitNorms = true; 358 } else { 359 throw new IllegalArgumentException ("unknown index parameter " + index); 360 } 361 362 this.isBinary = false; 363 364 setStoreTermVector(termVector); 365 } 366 367 375 public Field(String name, Reader reader) { 376 this(name, reader, TermVector.NO); 377 } 378 379 388 public Field(String name, Reader reader, TermVector termVector) { 389 if (name == null) 390 throw new NullPointerException ("name cannot be null"); 391 if (reader == null) 392 throw new NullPointerException ("reader cannot be null"); 393 394 this.name = name.intern(); this.fieldsData = reader; 396 397 this.isStored = false; 398 this.isCompressed = false; 399 400 this.isIndexed = true; 401 this.isTokenized = true; 402 403 this.isBinary = false; 404 405 setStoreTermVector(termVector); 406 } 407 408 413 public Field(String name, String string, 414 boolean store, boolean index, boolean token) { 415 this(name, string, store, index, token, false); 416 } 417 418 419 427 public Field(String name, byte[] value, Store store) { 428 if (name == null) 429 throw new IllegalArgumentException ("name cannot be null"); 430 if (value == null) 431 throw new IllegalArgumentException ("value cannot be null"); 432 433 this.name = name.intern(); 434 this.fieldsData = value; 435 436 if (store == Store.YES){ 437 this.isStored = true; 438 this.isCompressed = false; 439 } 440 else if (store == Store.COMPRESS) { 441 this.isStored = true; 442 this.isCompressed = true; 443 } 444 else if (store == Store.NO) 445 throw new IllegalArgumentException ("binary values can't be unstored"); 446 else 447 throw new IllegalArgumentException ("unknown store parameter " + store); 448 449 this.isIndexed = false; 450 this.isTokenized = false; 451 452 this.isBinary = true; 453 454 setStoreTermVector(TermVector.NO); 455 } 456 457 468 public Field(String name, String string, 469 boolean store, boolean index, boolean token, boolean storeTermVector) { 470 if (name == null) 471 throw new NullPointerException ("name cannot be null"); 472 if (string == null) 473 throw new NullPointerException ("value cannot be null"); 474 if (!index && storeTermVector) 475 throw new IllegalArgumentException ("cannot store a term vector for fields that are not indexed"); 476 477 this.name = name.intern(); this.fieldsData = string; 479 this.isStored = store; 480 this.isIndexed = index; 481 this.isTokenized = token; 482 this.storeTermVector = storeTermVector; 483 } 484 485 private void setStoreTermVector(TermVector termVector) { 486 if (termVector == TermVector.NO) { 487 this.storeTermVector = false; 488 this.storePositionWithTermVector = false; 489 this.storeOffsetWithTermVector = false; 490 } 491 else if (termVector == TermVector.YES) { 492 this.storeTermVector = true; 493 this.storePositionWithTermVector = false; 494 this.storeOffsetWithTermVector = false; 495 } 496 else if (termVector == TermVector.WITH_POSITIONS) { 497 this.storeTermVector = true; 498 this.storePositionWithTermVector = true; 499 this.storeOffsetWithTermVector = false; 500 } 501 else if (termVector == TermVector.WITH_OFFSETS) { 502 this.storeTermVector = true; 503 this.storePositionWithTermVector = false; 504 this.storeOffsetWithTermVector = true; 505 } 506 else if (termVector == TermVector.WITH_POSITIONS_OFFSETS) { 507 this.storeTermVector = true; 508 this.storePositionWithTermVector = true; 509 this.storeOffsetWithTermVector = true; 510 } 511 else { 512 throw new IllegalArgumentException ("unknown termVector parameter " + termVector); 513 } 514 } 515 516 519 public final boolean isStored() { return isStored; } 520 521 523 public final boolean isIndexed() { return isIndexed; } 524 525 528 public final boolean isTokenized() { return isTokenized; } 529 530 531 public final boolean isCompressed() { return isCompressed; } 532 533 541 public final boolean isTermVectorStored() { return storeTermVector; } 542 543 547 public boolean isStoreOffsetWithTermVector(){ 548 return storeOffsetWithTermVector; 549 } 550 551 554 public boolean isStorePositionWithTermVector(){ 555 return storePositionWithTermVector; 556 } 557 558 559 public final boolean isBinary() { return isBinary; } 560 561 562 public boolean getOmitNorms() { return omitNorms; } 563 564 569 public void setOmitNorms(boolean omitNorms) { this.omitNorms=omitNorms; } 570 571 572 public final String toString() { 573 StringBuffer result = new StringBuffer (); 574 if (isStored) { 575 result.append("stored"); 576 if (isCompressed) 577 result.append("/compressed"); 578 else 579 result.append("/uncompressed"); 580 } 581 if (isIndexed) { 582 if (result.length() > 0) 583 result.append(","); 584 result.append("indexed"); 585 } 586 if (isTokenized) { 587 if (result.length() > 0) 588 result.append(","); 589 result.append("tokenized"); 590 } 591 if (storeTermVector) { 592 if (result.length() > 0) 593 result.append(","); 594 result.append("termVector"); 595 } 596 if (storeOffsetWithTermVector) { 597 if (result.length() > 0) 598 result.append(","); 599 result.append("termVectorOffsets"); 600 } 601 if (storePositionWithTermVector) { 602 if (result.length() > 0) 603 result.append(","); 604 result.append("termVectorPosition"); 605 } 606 if (isBinary) { 607 if (result.length() > 0) 608 result.append(","); 609 result.append("binary"); 610 } 611 if (omitNorms) { 612 result.append(",omitNorms"); 613 } 614 result.append('<'); 615 result.append(name); 616 result.append(':'); 617 618 if (fieldsData != null) { 619 result.append(fieldsData); 620 } 621 622 result.append('>'); 623 return result.toString(); 624 } 625 626 } 627 | Popular Tags |