From 5635cce871b148856ee67e8a3ffd62bc5b3b78e8 Mon Sep 17 00:00:00 2001 From: Daniel Naber Date: Tue, 3 Aug 2004 21:49:24 +0000 Subject: [PATCH] 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 --- .../org/apache/lucene/demo/DeleteFiles.java | 27 ++++++++++++------- .../org/apache/lucene/demo/FileDocument.java | 7 ++--- .../org/apache/lucene/demo/HTMLDocument.java | 7 ++--- .../org/apache/lucene/demo/IndexHTML.java | 4 +-- .../org/apache/lucene/demo/SearchFiles.java | 15 +++++------ 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/demo/org/apache/lucene/demo/DeleteFiles.java b/src/demo/org/apache/lucene/demo/DeleteFiles.java index 6156e5ee520..3345c21c624 100644 --- a/src/demo/org/apache/lucene/demo/DeleteFiles.java +++ b/src/demo/org/apache/lucene/demo/DeleteFiles.java @@ -16,27 +16,36 @@ package org.apache.lucene.demo; * limitations under the License. */ -import java.io.IOException; - import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; +//import org.apache.lucene.index.Term; class DeleteFiles { + public static void main(String[] args) { + String usage = "java org.apache.lucene.demo.DeleteFiles "; + if (args.length == 0) { + System.err.println("Usage: " + usage); + System.exit(1); + } try { - Directory directory = FSDirectory.getDirectory("demo index", false); + Directory directory = FSDirectory.getDirectory("index", false); IndexReader reader = IndexReader.open(directory); -// Term term = new Term("path", "pizza"); -// int deleted = reader.delete(term); + Term term = new Term("path", args[0]); + int deleted = reader.delete(term); -// System.out.println("deleted " + deleted + -// " documents containing " + term); + System.out.println("deleted " + deleted + + " documents containing " + term); - for (int i = 0; i < reader.maxDoc(); i++) - reader.delete(i); + // one can also delete documents by their internal id: + /* + for (int i = 0; i < reader.maxDoc(); i++) { + System.out.println("Deleting document with id " + i); + reader.delete(i); + }*/ reader.close(); directory.close(); diff --git a/src/demo/org/apache/lucene/demo/FileDocument.java b/src/demo/org/apache/lucene/demo/FileDocument.java index 300256b0edd..041c040600d 100644 --- a/src/demo/org/apache/lucene/demo/FileDocument.java +++ b/src/demo/org/apache/lucene/demo/FileDocument.java @@ -47,9 +47,10 @@ public class FileDocument { // make a new, empty document Document doc = new Document(); - // Add the path of the file as a field named "path". Use a Text field, so - // that the index stores the path, and so that the path is searchable - doc.add(Field.Text("path", f.getPath())); + // Add the path of the file as a field named "path". Use a + // Keyword field, so that it's searchable, but so that no attempt is made + // 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 // Keyword field, so that it's searchable, but so that no attempt is made diff --git a/src/demo/org/apache/lucene/demo/HTMLDocument.java b/src/demo/org/apache/lucene/demo/HTMLDocument.java index 5f600666193..93a73a79081 100644 --- a/src/demo/org/apache/lucene/demo/HTMLDocument.java +++ b/src/demo/org/apache/lucene/demo/HTMLDocument.java @@ -45,9 +45,10 @@ public class HTMLDocument { // make a new, empty document Document doc = new Document(); - // Add the url as a field named "url". Use an UnIndexed field, so - // that the url is just stored with the document, but is not searchable. - doc.add(Field.UnIndexed("url", f.getPath().replace(dirSep, '/'))); + // Add the url as a field named "path". Use a Keyword field, so + // that it's searchable, but so that no attempt is made + // 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 // Keyword field, so that it's searchable, but so that no attempt is made diff --git a/src/demo/org/apache/lucene/demo/IndexHTML.java b/src/demo/org/apache/lucene/demo/IndexHTML.java index 6e031436140..83316fcc157 100644 --- a/src/demo/org/apache/lucene/demo/IndexHTML.java +++ b/src/demo/org/apache/lucene/demo/IndexHTML.java @@ -144,12 +144,12 @@ class IndexHTML { uidIter.next(); // keep matching docs } else if (!deleting) { // add new docs Document doc = HTMLDocument.Document(file); - System.out.println("adding " + doc.get("url")); + System.out.println("adding " + doc.get("path")); writer.addDocument(doc); } } else { // creating a new index 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 } } diff --git a/src/demo/org/apache/lucene/demo/SearchFiles.java b/src/demo/org/apache/lucene/demo/SearchFiles.java index 27eb0d4a6fd..18bbb124ab8 100644 --- a/src/demo/org/apache/lucene/demo/SearchFiles.java +++ b/src/demo/org/apache/lucene/demo/SearchFiles.java @@ -16,7 +16,6 @@ package org.apache.lucene.demo; * limitations under the License. */ -import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -56,15 +55,13 @@ class SearchFiles { Document doc = hits.doc(i); String path = doc.get("path"); if (path != null) { - System.out.println(i + ". " + path); - } else { - String url = doc.get("url"); - if (url != null) { - System.out.println(i + ". " + url); - System.out.println(" - " + doc.get("title")); - } else { - System.out.println(i + ". " + "No path nor URL for this document"); + System.out.println((i+1) + ". " + path); + String title = doc.get("title"); + if (title != null) { + System.out.println(" Title: " + doc.get("title")); } + } else { + System.out.println((i+1) + ". " + "No path for this document"); } }