lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mário André <mario...@infonet.com.br>
Subject Problem with PhraseQuery
Date Tue, 05 Jan 2010 14:43:34 GMT
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-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message