lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arpad KATONA <a.kat...@ever-team.com>
Subject QueryParser.parse
Date Mon, 27 May 2002 11:36:46 GMT
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:   <mailto:lucene-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-user-help@jakarta.apache.org>


Mime
View raw message