Hi Andre, you are using StandardAnalyzer for indexing but you search with an un-analyzed string "Lucene" (q.add(new Term("title","Lucene"));) If you pass this string to the query parser your query string will be analyzed (will most likely result in a lowercased string). The analyzed query will then match and retrieve your documents. try to search :
> PhraseQuery q = new PhraseQuery(); > q.setSlop(1); > q.add(new Term("title","lucene")); > q.add(new Term("title","for")); simon . 2010/1/5 Mário André <mario...@infonet.com.br>: > Hi, > I need search by phrase containing a particular sequence of terms , then I > am using Java Lucene 3.0, more specifically the PhraseQuery. > I'm using the code below, but does not work(PhraseQuery). Only does work > when I use the QueryParser: > Is there some problem or how can I use the PhraseQuery in Lucene 3.0? > public class Main1 { > > /** > * @param args the command line arguments > */ > public static void main(String[] args) throws IOException, > ParseException > { > StandardAnalyzer analyzer = new > StandardAnalyzer(Version.LUCENE_CURRENT); > > // 1. create the index > Directory index = new RAMDirectory(); > > IndexWriter w = new IndexWriter(index, analyzer, true, > IndexWriter.MaxFieldLength.UNLIMITED); > > addDoc(w, "Lucene in Action for about ten days here in brazil kkkkkkk > eeeeeeee nnnn xxxx tttt dddd jjjjjjj k"); > addDoc(w, "Lucene for Dummies"); > addDoc(w, "Lucene for Dummies"); > addDoc(w, "Managing Gigabytes"); > > w.close(); > > // 2. query > //Query q = new QueryParser(Version.LUCENE_CURRENT, "title", > analyzer).parse(querystr); > > PhraseQuery q = new PhraseQuery(); > q.setSlop(1); > q.add(new Term("title","Lucene")); > q.add(new Term("title","for")); > > > // 3. search > int hitsPerPage = 20; > IndexSearcher searcher = new IndexSearcher(index, true); > TopScoreDocCollector collector = > TopScoreDocCollector.create(hitsPerPage, true); > searcher.search(q, collector); > ScoreDoc[] hits = collector.topDocs().scoreDocs; > > // 4. display results > System.out.println("Found " + hits.length + " hits."); > for(int i=0;i<hits.length;++i) { > int docId = hits[i].doc; > Document d = searcher.doc(docId); > System.out.println((i + 1) + ". " + d.get("title")); > } > > searcher.close(); > } > > private static void addDoc(IndexWriter w, String value) throws IOException > { > Document doc = new Document(); > doc.add(new Field("title", value, Field.Store.YES, > Field.Index.ANALYZED)); > w.addDocument(doc); > } > > } > > --------------------------------------------------------------------- > Mário André > Instituto Federal de Educação, Ciência e Tecnologia de Sergipe - IFS > Mestrando em MCC - Universidade Federal de Alagoas - UFAL > http://www.marioandre.com.br/ > Skype: mario-fa > ---------------------------------------------------------------------------- > ---------- > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org > For additional commands, e-mail: java-user-h...@lucene.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org