lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Fred Yu" <fred...@xtra.co.nz>
Subject Search Lucene documents returns 0 hits
Date Wed, 06 Oct 2004 20:24:52 GMT
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: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message