lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack Krupansky" <>
Subject Re: Help needed: search is returning no results
Date Tue, 18 Dec 2012 14:35:46 GMT
Maybe you wanted "text" fields that are analyzed and tokenized, as opposed 
to string fields which are not analyzed and stored and queried exactly 


But, show us some of your indexed data and queries that fail.

-- Jack Krupansky

-----Original Message----- 
From: Ramon Casha
Sent: Tuesday, December 18, 2012 9:14 AM
Subject: Help needed: search is returning no results

I have just downloaded and set up Lucene 4.0.0 to implement a search
facility for a web app I'm developing.

Creating the index seems to be successful - the files created contain
the text that I'm indexing. However, search is returning no results.
The code I'm using is fairly similar to the examples given.

Here is the search code:

    private static final File index = new File("/tmp/naturopedia-index");
    private static final Version VERSION = Version.LUCENE_40;

    public String search() throws IOException, ParseException {
        Analyzer analyzer = new StandardAnalyzer(VERSION);
        Directory directory =;

        DirectoryReader ireader =;
        IndexSearcher isearcher = new IndexSearcher(ireader);

        QueryParser parser = new QueryParser(VERSION, "labels", analyzer);

        Query q = parser.parse(getQuery());
        ScoreDoc[] hits =, 1000).scoreDocs;

        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
        return "search";
Every time I try this, the search returns zero results. I tried with
different fields (text and labels), both of which are indexed, and I
tried different words. Any help would be appreciated.

Here is the code for producing the index:

public String crawl() throws IOException {
        Analyzer analyzer = new StandardAnalyzer(VERSION);
        Directory directory =;

        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
        IndexWriter iwriter = new IndexWriter(directory, config);

        DB db = new DB(); // JPA interface class.

        for ( Taxonomy t : db.taxonomy.all() ) {
            LOG.log(Level.INFO, "Scanning {0}", t);

            Document doc = new Document();
            doc.add(new LongField("id", t.getId(), Store.YES));
            LOG.log(Level.INFO, "  id={0}", t.getId());
            StringBuilder text = new StringBuilder();
            for(Text l : t.getTexts()) {
                        .append((' '));
            if(text.length() > 0) {
                doc.add(new StringField("text", text.toString(),
                LOG.log(Level.INFO, "  text={0}", text);

            StringBuilder labels = new StringBuilder();
            toIndex(labels, t.getLabels());
            for(Image l : t.getImages()) {
                toIndex(labels, l.getLabels());
            doc.add(new StringField("labels", labels.toString(),
            LOG.log(Level.INFO, "  labels={0}", labels);
            StringBuilder sb = new StringBuilder();
            for ( Tag tag : t.getTags() ) {
                toIndex(sb, tag.getLabels());
            if(!sb.toString().isEmpty()) {
                doc.add(new StringField("tags", sb.toString(), 
                LOG.log(Level.INFO, "  tags={0}", sb);


        return "search";

    private void toIndex(StringBuilder sb, LabelGroup lg) {
        for(Label l : lg.getLabels()) {
            sb.append(" ");

Ramon Casha

To unsubscribe, e-mail:
For additional commands, e-mail: 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message