Apache Solr open-source search software
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.
Daniel Naber5635cce871b1
diff --git a/src/demo/org/apache/lucene/demo/DeleteFiles.java b/src/demo/org/apache/lucene/demo/DeleteFiles.javaindex 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 <unique_term>";+ 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.javaindex 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 documentDocument 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 madediff --git a/src/demo/org/apache/lucene/demo/HTMLDocument.java b/src/demo/org/apache/lucene/demo/HTMLDocument.javaindex 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 documentDocument 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 madediff --git a/src/demo/org/apache/lucene/demo/IndexHTML.java b/src/demo/org/apache/lucene/demo/IndexHTML.javaindex 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 docsDocument 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 indexDocument 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.javaindex 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");}}