lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <...@digimem.net>
Subject RE: AW: WildcardQuery
Date Fri, 24 May 2002 15:19:53 GMT
Sorry - I said I was going to send some code ...


--
Ian.

import org.apache.lucene.queryParser.*; 
import org.apache.lucene.search.*; 
import org.apache.lucene.index.*; 
import org.apache.lucene.analysis.*; 
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*; 
import org.apache.lucene.store.*;


public class LuceneTest { 
    
    RAMDirectory ramdir;
    Analyzer analyzer;
    IndexWriter writer;
    IndexReader reader;
    Searcher searcher;

    public LuceneTest() {
	analyzer = new StandardAnalyzer(); 
	ramdir = new RAMDirectory();
    }


    public static void main(String args[]) throws Exception { 
	LuceneTest ld = new LuceneTest();
	ld.load();
	ld.search();
    }
    
    
    void load() throws Exception {
	writer = new IndexWriter(ramdir, analyzer, true); 
	add("january");
	add("february");
	add("june");
	add("july");
	writer.close();
    }	
    
    
    
    void add(String s) throws Exception {
	Document d = new Document();
	d.add(Field.Keyword("id", s));
	System.out.println("Adding "+s);
	writer.addDocument(d);
    }
    
    

    void search() throws Exception {
	reader = IndexReader.open(ramdir);
	searcher = new IndexSearcher(reader);
	search("jan*");
	search("jan*y");
	search("j*y");
	search("j*");
	search("*y");
    }


    void search(String s) throws Exception {
	Query query = QueryParser.parse(s, "id", analyzer);
	Hits hits = searcher.search(query);
	System.out.println(s+" matched "+hits.length());
	for (int i = 0; i < hits.length(); i++) {
	    System.out.println("    " + 
			       hits.doc(i).get("id"));
	}
    }
}


----------------------------------------------------------------------
Searchable personal storage and archiving from http://www.digimem.net/


Mime
View raw message