You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Arpad KATONA <a....@ever-team.com> on 2002/05/27 13:36:46 UTC

QueryParser.parse

Hello,

under http://jakarta.apache.org/lucene/docs/queryparsersyntax.html is to be
read :

+++
As an example, let's assume a Lucene index contains two fields, title and text
and text is the default field. If you want to find the document entitled "The
Right Way" which contains the text "don't go this way", you can enter: 

title:"The Right Way" AND text:go  
+++   

So i tried, see the code below :
1) first, i create a document with 3 fields: KEY, AUTEUR and TITRE, the fields
are NOT to be tokenized.
2) second, i read the index to see what it contains (see "output" below).
3) third, i would like to find a record with "jean auteur" as AUTEUR or "opus
primum" as TITRE; thus, the query string is :
«AUTEUR:"jean auteur OR TITRE:"opus primum"». But, hélas!, there is no answer.
Could you tell me, please, where the error is?

Thanks

Arpad KATONA
a.katona@ever-team.com

--

+++
try {
  Analyzer analyzer = new StandardAnalyzer();
  String sIxPath = "D:\\tempo\\lucene\\ix";
  //
  // indexing
  //
  IndexWriter writer = new IndexWriter(sIxPath, analyzer, true);
  org.apache.lucene.document.Document doc =
    new org.apache.lucene.document.Document();
  Field fd = new Field("KEY", "1", true, true, false);
  doc.add(fd); fd = null;
  fd = new Field("AUTEUR", "jean auteur", false, true, false);
  doc.add(fd); fd = null;
  fd = new Field("TITRE", "opus primum", false, true, false);
  doc.add(fd); fd = null;
  writer.addDocument(doc); doc = null;
  writer.close(); writer = null;
  //
  // checking
  //
  IndexReader reader = IndexReader.open(sIxPath);
  TermEnum tenum = reader.terms();
  for(; tenum.next(); ) {
    Term term = tenum.term();
    String sField = term.field();
    String sText = term.text();
    System.out.println("sField=«"+sField+"» sText=«"+sText+"»");
  }
  tenum.close(); tenum = null;
  reader.close(); reader=null;
  //
  // searching
  //
  String sQuery="TITRE:\"opus primum\" OR AUTEUR:\"jean auteur\"";
  Query query = QueryParser.parse(sQuery, "", analyzer);
  System.out.println("query = «"+query.toString("")+"»");
  Searcher searcher = new IndexSearcher(sIxPath);
  Hits hits = searcher.search(query);
  if(null==hits) {
    System.out.println("No answer.");
  } else {
    int nbHits = hits.length();
    System.out.println("nbHits = "+Integer.toString(nbHits));
  }
  hits = null;
  searcher.close(); searcher=null;
  query = null;
} catch (Exception ex) {
  ex.printStackTrace();
}
+++

output:

+++
sField=«AUTEUR» sText=«jean auteur»

sField=«KEY» sText=«1»

sField=«TITRE» sText=«opus primum»

query = «TITRE:"opus primum" AUTEUR:"jean auteur"»

nbHits = 0

+++

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>