If you're indexing with a Keyword field you need to use a TermQuery.
QueryParser will only work for Text fields.

The reason for this is that both the Text field and the QueryParser use
the Analyzer to chop up the input into searchable chunks.  Depending on
the Analyzer this includes converting to lower-case, stripping trailing
"s" and "ing" and removing stopwords like "the" and "and".  The
TermQuery and Keyword field both treat the input exactly as is.

-Lars

-----Original Message-----
From: Fred Yu [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 06, 2004 3:25 PM
To: [EMAIL PROTECTED]
Subject: Search Lucene documents returns 0 hits


Hi

Does anyone know why Lucene returns 0 hits when there are in fact three
matches? The attached are two java class that repeat the problem. In the
example, I created a Keyword field "type" for each document added.
Lucene
can correctly find the documents if I use "Text" field instead of
"Keyword"
field.


Thanks in advance
Fred

package test;

import java.io.IOException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

public class IndexItems {
  public static void main(String[] args) throws IOException {
    try {
      IndexWriter writer = new IndexWriter("/test/index", new
StandardAnalyzer(), true);
      indexDocs(writer);

      writer.optimize();
      writer.close();

      System.out.println("index finished.");

    } catch (IOException e) {
      System.out.println(" caught a " + e.getClass() +
       "\n with message: " + e.getMessage());
    }
  }

  private static void indexDocs(IndexWriter writer)
    throws IOException {
    Document document=new Document();

    document.add(Field.Keyword("type", "stockSingle"));
    document.add(Field.Text("desc", "test single 1"));
    writer.addDocument(document);

    document=new Document();
    document.add(Field.Keyword("type", "stockSingle"));
    document.add(Field.Text("desc", "test single 2"));
    writer.addDocument(document);

    document=new Document();
    document.add(Field.Keyword("type", "stockItem"));
    document.add(Field.Text("desc", "test single 3"));
    writer.addDocument(document);
  }
}

package test;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryFilter;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;

public class SearchItems {
  public static void main(String[] args) {
    try {
      Searcher searcher = new IndexSearcher("/test/index");
      QueryParser qp=new QueryParser("type", new StandardAnalyzer());
      Query query=qp.parse("type:stockSingle");

      Hits hits = searcher.search(query);
      System.out.println("search found: "+hits.length() + " total
matching
documents");

      searcher.close();
    } catch (Exception e) {
      System.out.println(" caught a " + e.getClass() +
                         "\n with message: " + e.getMessage());
    }
  }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to