Dennis,

I've attached some sample code that works well for my needs. You should
find it scales very well, even when you search for a "parentpath" near
the root.

The output I get from running this program is:

query:  name:rt.jar
  D:\opt\j2sdk1.4.2\jre\lib\rt.jar

query:  name:LICENSE
  D:\opt\j2sdk1.4.2\jre\LICENSE
  D:\opt\j2sdk1.4.2\LICENSE

query:  fullpath:"D:\opt\j2sdk1.4.2\LICENSE"
  D:\opt\j2sdk1.4.2\LICENSE

query:  parentpath:"D:\opt\j2sdk1.4.2\include"
  D:\opt\j2sdk1.4.2\include\jawt.h
  D:\opt\j2sdk1.4.2\include\jni.h
  D:\opt\j2sdk1.4.2\include\jvmdi.h
  D:\opt\j2sdk1.4.2\include\jvmpi.h
  D:\opt\j2sdk1.4.2\include\win32\jawt_md.h
  D:\opt\j2sdk1.4.2\include\win32\jni_md.h



=Matt



Dennis Thrys�e wrote:
Andrzej Bialecki wrote:

What about using PhraseQuery, and store the path with all but first path separator replaced by whitespace (i.e. "/foo bar baz one two three"). Then you could query for "/foo bar", "/foo bar baz", and so on...


That sounds like a really good suggestion. I'll try that. Thanks.

-dennis


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]





import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;

import java.io.File;
import java.io.IOException;

/**
 * This code is in the public domain
 *
 * @author Matt Quail (http://madbean.com/)
 */
public class HieracicalTreeExample {
    private static final Analyzer ANALYZER = new WhitespaceAnalyzer();
    private static final File sIndexDir = new File("d:/tmp/store");

    public static void main(String[] args) throws Exception {
        File treeRoot = new File("D:\\opt\\j2sdk1.4.2");
        indexTree(treeRoot);
        IndexReader reader = IndexReader.open(sIndexDir);
        IndexSearcher searcher = new IndexSearcher(reader);

        doSearch(searcher, "name:rt.jar");
        doSearch(searcher, "name:LICENSE");
        doSearch(searcher, "fullpath:\"D:\\opt\\j2sdk1.4.2\\LICENSE\"");
        doSearch(searcher, "parentpath:\"D:\\opt\\j2sdk1.4.2\\include\"");

        searcher.close();
    }

    private static void doSearch(IndexSearcher searcher, String query) throws 
ParseException, IOException {
        System.out.println();
        System.out.println("query:  " + query);
        Query q = QueryParser.parse(query, "name", ANALYZER);
        Hits hits = searcher.search(q);
        for (int i = 0; i < hits.length(); i++) {
            Document doc = hits.doc(i);
            System.out.println(" " + doc.get("fullpath"));
        }
    }

    private static void indexTree(File treeRoot) throws IOException {
        IndexWriter w = new IndexWriter(sIndexDir, ANALYZER, true);
        indexDir(w, treeRoot);
        w.close();
    }

    private static void indexDir(IndexWriter w, File dir) throws IOException {
        File[] children = dir.listFiles();
        for (int i = 0; i < children.length; i++) {
            File child = children[i];
            if (child.isDirectory()) {
                indexDir(w, child);
            } else if (child.isFile()) {
                indexFile(w, child);
            }
        }
    }

    private static void indexFile(IndexWriter w, File file) throws IOException {
//        System.out.println("indexing " + file);
        Document doc = new Document();
        doc.add(Field.Keyword("name", file.getName()));
        doc.add(Field.Keyword("fullpath", file.getPath()));
        File parent = file.getParentFile();
        while (parent != null) {
            doc.add(new Field("parentpath", parent.getPath(), false, true, false));
            parent = parent.getParentFile();
        }
        w.addDocument(doc);
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to