|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FileDocument | Line # 28 | 5 | 2 | 85.7% |
0.85714287
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (1) | |||
| Result | |||
|
0.85714287
|
org.apache.lucene.index.TestDoc.testIndexAndMerge
org.apache.lucene.index.TestDoc.testIndexAndMerge
|
1 PASS | |
| 1 | package org.apache.lucene.demo; | |
| 2 | ||
| 3 | /** | |
| 4 | * Copyright 2004 The Apache Software Foundation | |
| 5 | * | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 7 | * you may not use this file except in compliance with the License. | |
| 8 | * You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, software | |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 15 | * See the License for the specific language governing permissions and | |
| 16 | * limitations under the License. | |
| 17 | */ | |
| 18 | ||
| 19 | import java.io.File; | |
| 20 | import java.io.FileReader; | |
| 21 | ||
| 22 | import org.apache.lucene.document.DateTools; | |
| 23 | import org.apache.lucene.document.Document; | |
| 24 | import org.apache.lucene.document.Field; | |
| 25 | ||
| 26 | /** A utility for making Lucene Documents from a File. */ | |
| 27 | ||
| 28 | public class FileDocument { | |
| 29 | /** Makes a document for a File. | |
| 30 | <p> | |
| 31 | The document has three fields: | |
| 32 | <ul> | |
| 33 | <li><code>path</code>--containing the pathname of the file, as a stored, | |
| 34 | untokenized field; | |
| 35 | <li><code>modified</code>--containing the last modified date of the file as | |
| 36 | a field as created by <a | |
| 37 | href="lucene.document.DateTools.html">DateTools</a>; and | |
| 38 | <li><code>contents</code>--containing the full contents of the file, as a | |
| 39 | Reader field; | |
| 40 | */ | |
| 41 | 4 |
public static Document Document(File f) |
| 42 | throws java.io.FileNotFoundException { | |
| 43 | ||
| 44 | // make a new, empty document | |
| 45 | 4 | Document doc = new Document(); |
| 46 | ||
| 47 | // Add the path of the file as a field named "path". Use a field that is | |
| 48 | // indexed (i.e. searchable), but don't tokenize the field into words. | |
| 49 | 4 | doc.add(new Field("path", f.getPath(), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 50 | ||
| 51 | // Add the last modified date of the file a field named "modified". Use | |
| 52 | // a field that is indexed (i.e. searchable), but don't tokenize the field | |
| 53 | // into words. | |
| 54 | 4 | doc.add(new Field("modified", |
| 55 | DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE), | |
| 56 | Field.Store.YES, Field.Index.UN_TOKENIZED)); | |
| 57 | ||
| 58 | // Add the contents of the file to a field named "contents". Specify a Reader, | |
| 59 | // so that the text of the file is tokenized and indexed, but not stored. | |
| 60 | // Note that FileReader expects the file to be in the system's default encoding. | |
| 61 | // If that's not the case searching for special characters will fail. | |
| 62 | 4 | doc.add(new Field("contents", new FileReader(f))); |
| 63 | ||
| 64 | // return the document | |
| 65 | 4 | return doc; |
| 66 | } | |
| 67 | ||
| 68 | 0 |
private FileDocument() {} |
| 69 | } | |
| 70 | ||
|
||||||||||