Store the path as a Keyword field. Also rename HTMLDocument's "url" to "path"

and store it as a Keyword field, too. DeleteFiles now takes such a path
as its command line parameter and works on the index directory "index", like the
other demos.

This fixes bug http://issues.apache.org/bugzilla/show_bug.cgi?id=30330


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150390 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2004-08-03 21:49:24 +00:00
parent add0c0a47b
commit 5635cce871
5 changed files with 34 additions and 26 deletions

View File

@ -16,27 +16,36 @@ package org.apache.lucene.demo;
* limitations under the License. * limitations under the License.
*/ */
import java.io.IOException;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
//import org.apache.lucene.index.Term;
class DeleteFiles { class DeleteFiles {
public static void main(String[] args) { public static void main(String[] args) {
String usage = "java org.apache.lucene.demo.DeleteFiles <unique_term>";
if (args.length == 0) {
System.err.println("Usage: " + usage);
System.exit(1);
}
try { try {
Directory directory = FSDirectory.getDirectory("demo index", false); Directory directory = FSDirectory.getDirectory("index", false);
IndexReader reader = IndexReader.open(directory); IndexReader reader = IndexReader.open(directory);
// Term term = new Term("path", "pizza"); Term term = new Term("path", args[0]);
// int deleted = reader.delete(term); int deleted = reader.delete(term);
// System.out.println("deleted " + deleted + System.out.println("deleted " + deleted +
// " documents containing " + term); " documents containing " + term);
for (int i = 0; i < reader.maxDoc(); i++) // one can also delete documents by their internal id:
reader.delete(i); /*
for (int i = 0; i < reader.maxDoc(); i++) {
System.out.println("Deleting document with id " + i);
reader.delete(i);
}*/
reader.close(); reader.close();
directory.close(); directory.close();

View File

@ -47,9 +47,10 @@ public class FileDocument {
// make a new, empty document // make a new, empty document
Document doc = new Document(); Document doc = new Document();
// Add the path of the file as a field named "path". Use a Text field, so // Add the path of the file as a field named "path". Use a
// that the index stores the path, and so that the path is searchable // Keyword field, so that it's searchable, but so that no attempt is made
doc.add(Field.Text("path", f.getPath())); // to tokenize the field into words.
doc.add(Field.Keyword("path", f.getPath()));
// Add the last modified date of the file a field named "modified". Use a // Add the last modified date of the file a field named "modified". Use a
// Keyword field, so that it's searchable, but so that no attempt is made // Keyword field, so that it's searchable, but so that no attempt is made

View File

@ -45,9 +45,10 @@ public class HTMLDocument {
// make a new, empty document // make a new, empty document
Document doc = new Document(); Document doc = new Document();
// Add the url as a field named "url". Use an UnIndexed field, so // Add the url as a field named "path". Use a Keyword field, so
// that the url is just stored with the document, but is not searchable. // that it's searchable, but so that no attempt is made
doc.add(Field.UnIndexed("url", f.getPath().replace(dirSep, '/'))); // to tokenize the field into words.
doc.add(Field.Keyword("path", f.getPath().replace(dirSep, '/')));
// Add the last modified date of the file a field named "modified". Use a // Add the last modified date of the file a field named "modified". Use a
// Keyword field, so that it's searchable, but so that no attempt is made // Keyword field, so that it's searchable, but so that no attempt is made

View File

@ -144,12 +144,12 @@ class IndexHTML {
uidIter.next(); // keep matching docs uidIter.next(); // keep matching docs
} else if (!deleting) { // add new docs } else if (!deleting) { // add new docs
Document doc = HTMLDocument.Document(file); Document doc = HTMLDocument.Document(file);
System.out.println("adding " + doc.get("url")); System.out.println("adding " + doc.get("path"));
writer.addDocument(doc); writer.addDocument(doc);
} }
} else { // creating a new index } else { // creating a new index
Document doc = HTMLDocument.Document(file); Document doc = HTMLDocument.Document(file);
System.out.println("adding " + doc.get("url")); System.out.println("adding " + doc.get("path"));
writer.addDocument(doc); // add docs unconditionally writer.addDocument(doc); // add docs unconditionally
} }
} }

View File

@ -16,7 +16,6 @@ package org.apache.lucene.demo;
* limitations under the License. * limitations under the License.
*/ */
import java.io.IOException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -56,15 +55,13 @@ class SearchFiles {
Document doc = hits.doc(i); Document doc = hits.doc(i);
String path = doc.get("path"); String path = doc.get("path");
if (path != null) { if (path != null) {
System.out.println(i + ". " + path); System.out.println((i+1) + ". " + path);
} else { String title = doc.get("title");
String url = doc.get("url"); if (title != null) {
if (url != null) { System.out.println(" Title: " + doc.get("title"));
System.out.println(i + ". " + url);
System.out.println(" - " + doc.get("title"));
} else {
System.out.println(i + ". " + "No path nor URL for this document");
} }
} else {
System.out.println((i+1) + ". " + "No path for this document");
} }
} }