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 Ramon Casha <rc...@gmail.com> on 2012/12/18 15:14:55 UTC

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 = FSDirectory.open(index);

        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher isearcher = new IndexSearcher(ireader);

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

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

        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
            System.out.println(hitDoc.getField("id"));
        }
        ireader.close();
        directory.close();
        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 = FSDirectory.open(index);

        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        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()) {
                text.append(l.getWikiText())
                        .append((' '));
            }
            if(text.length() > 0) {
                doc.add(new StringField("text", text.toString(),
Field.Store.YES));
                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(),
Field.Store.NO));
            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(), Field.Store.NO));
                LOG.log(Level.INFO, "  tags={0}", sb);
            }
            iwriter.addDocument(doc);
        }

        db.close();
        iwriter.close();

        return "search";
    }

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


--
Ramon Casha

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Help needed: search is returning no results

Posted by Ian Lea <ia...@gmail.com>.
I think you need TextField rather than StringField.  See also
http://wiki.apache.org/lucene-java/LuceneFAQ#Why_am_I_getting_no_hits_.2BAC8_incorrect_hits.3F


--
Ian.


On Tue, Dec 18, 2012 at 2:14 PM, Ramon Casha <rc...@gmail.com> wrote:
> 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 = FSDirectory.open(index);
>
>         DirectoryReader ireader = DirectoryReader.open(directory);
>         IndexSearcher isearcher = new IndexSearcher(ireader);
>
>         QueryParser parser = new QueryParser(VERSION, "labels", analyzer);
>
>         Query q = parser.parse(getQuery());
>         ScoreDoc[] hits = isearcher.search(q, 1000).scoreDocs;
>
>         for (int i = 0; i < hits.length; i++) {
>             Document hitDoc = isearcher.doc(hits[i].doc);
>             System.out.println(hitDoc.getField("id"));
>         }
>         ireader.close();
>         directory.close();
>         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 = FSDirectory.open(index);
>
>         IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
>         config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
>         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()) {
>                 text.append(l.getWikiText())
>                         .append((' '));
>             }
>             if(text.length() > 0) {
>                 doc.add(new StringField("text", text.toString(),
> Field.Store.YES));
>                 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(),
> Field.Store.NO));
>             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(), Field.Store.NO));
>                 LOG.log(Level.INFO, "  tags={0}", sb);
>             }
>             iwriter.addDocument(doc);
>         }
>
>         db.close();
>         iwriter.close();
>
>         return "search";
>     }
>
>     private void toIndex(StringBuilder sb, LabelGroup lg) {
>         for(Label l : lg.getLabels()) {
>             sb.append(l.getText());
>             sb.append(" ");
>         }
>     }
>
>
> --
> Ramon Casha
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Help needed: search is returning no results

Posted by Ramon Casha <rc...@gmail.com>.
I verified that the index was correct using the app Luke, tested some
queries using it then replicated the results via code. It seems I need
to refine the token parsing but at least I have something now.

Ramon Casha


On 18 December 2012 15:50, Ramon Casha <rc...@gmail.com> wrote:
> Hmm ok I got something.
>
>
> Ramon Casha
>
>
> On 18 December 2012 15:44, Ramon Casha <rc...@gmail.com> wrote:
>> I converted them to TextField but the result is the same.
>>
>>   doc.add(new TextField("text", text.toString(), Store.YES));
>>
>> The search always returns an empty array.
>>
>> Ramon Casha
>>
>>
>> On 18 December 2012 15:35, Jack Krupansky <ja...@basetechnology.com> wrote:
>>> Maybe you wanted "text" fields that are analyzed and tokenized, as opposed
>>> to string fields which are not analyzed and stored and queried exactly
>>> as-is.
>>>
>>> See:
>>> http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/document/TextField.html
>>>
>>> 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
>>> To: java-user@lucene.apache.org
>>> 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 = FSDirectory.open(index);
>>>
>>>        DirectoryReader ireader = DirectoryReader.open(directory);
>>>        IndexSearcher isearcher = new IndexSearcher(ireader);
>>>
>>>        QueryParser parser = new QueryParser(VERSION, "labels", analyzer);
>>>
>>>        Query q = parser.parse(getQuery());
>>>        ScoreDoc[] hits = isearcher.search(q, 1000).scoreDocs;
>>>
>>>        for (int i = 0; i < hits.length; i++) {
>>>            Document hitDoc = isearcher.doc(hits[i].doc);
>>>            System.out.println(hitDoc.getField("id"));
>>>        }
>>>        ireader.close();
>>>        directory.close();
>>>        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 = FSDirectory.open(index);
>>>
>>>        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
>>>        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
>>>        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()) {
>>>                text.append(l.getWikiText())
>>>                        .append((' '));
>>>            }
>>>            if(text.length() > 0) {
>>>                doc.add(new StringField("text", text.toString(),
>>> Field.Store.YES));
>>>                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(),
>>> Field.Store.NO));
>>>            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(),
>>> Field.Store.NO));
>>>                LOG.log(Level.INFO, "  tags={0}", sb);
>>>            }
>>>            iwriter.addDocument(doc);
>>>        }
>>>
>>>        db.close();
>>>        iwriter.close();
>>>
>>>        return "search";
>>>    }
>>>
>>>    private void toIndex(StringBuilder sb, LabelGroup lg) {
>>>        for(Label l : lg.getLabels()) {
>>>            sb.append(l.getText());
>>>            sb.append(" ");
>>>        }
>>>    }
>>>
>>>
>>> --
>>> Ramon Casha
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Help needed: search is returning no results

Posted by Ramon Casha <rc...@gmail.com>.
Hmm ok I got something.


Ramon Casha


On 18 December 2012 15:44, Ramon Casha <rc...@gmail.com> wrote:
> I converted them to TextField but the result is the same.
>
>   doc.add(new TextField("text", text.toString(), Store.YES));
>
> The search always returns an empty array.
>
> Ramon Casha
>
>
> On 18 December 2012 15:35, Jack Krupansky <ja...@basetechnology.com> wrote:
>> Maybe you wanted "text" fields that are analyzed and tokenized, as opposed
>> to string fields which are not analyzed and stored and queried exactly
>> as-is.
>>
>> See:
>> http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/document/TextField.html
>>
>> 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
>> To: java-user@lucene.apache.org
>> 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 = FSDirectory.open(index);
>>
>>        DirectoryReader ireader = DirectoryReader.open(directory);
>>        IndexSearcher isearcher = new IndexSearcher(ireader);
>>
>>        QueryParser parser = new QueryParser(VERSION, "labels", analyzer);
>>
>>        Query q = parser.parse(getQuery());
>>        ScoreDoc[] hits = isearcher.search(q, 1000).scoreDocs;
>>
>>        for (int i = 0; i < hits.length; i++) {
>>            Document hitDoc = isearcher.doc(hits[i].doc);
>>            System.out.println(hitDoc.getField("id"));
>>        }
>>        ireader.close();
>>        directory.close();
>>        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 = FSDirectory.open(index);
>>
>>        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
>>        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
>>        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()) {
>>                text.append(l.getWikiText())
>>                        .append((' '));
>>            }
>>            if(text.length() > 0) {
>>                doc.add(new StringField("text", text.toString(),
>> Field.Store.YES));
>>                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(),
>> Field.Store.NO));
>>            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(),
>> Field.Store.NO));
>>                LOG.log(Level.INFO, "  tags={0}", sb);
>>            }
>>            iwriter.addDocument(doc);
>>        }
>>
>>        db.close();
>>        iwriter.close();
>>
>>        return "search";
>>    }
>>
>>    private void toIndex(StringBuilder sb, LabelGroup lg) {
>>        for(Label l : lg.getLabels()) {
>>            sb.append(l.getText());
>>            sb.append(" ");
>>        }
>>    }
>>
>>
>> --
>> Ramon Casha
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Help needed: search is returning no results

Posted by Ramon Casha <rc...@gmail.com>.
I converted them to TextField but the result is the same.

  doc.add(new TextField("text", text.toString(), Store.YES));

The search always returns an empty array.

Ramon Casha


On 18 December 2012 15:35, Jack Krupansky <ja...@basetechnology.com> wrote:
> Maybe you wanted "text" fields that are analyzed and tokenized, as opposed
> to string fields which are not analyzed and stored and queried exactly
> as-is.
>
> See:
> http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/document/TextField.html
>
> 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
> To: java-user@lucene.apache.org
> 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 = FSDirectory.open(index);
>
>        DirectoryReader ireader = DirectoryReader.open(directory);
>        IndexSearcher isearcher = new IndexSearcher(ireader);
>
>        QueryParser parser = new QueryParser(VERSION, "labels", analyzer);
>
>        Query q = parser.parse(getQuery());
>        ScoreDoc[] hits = isearcher.search(q, 1000).scoreDocs;
>
>        for (int i = 0; i < hits.length; i++) {
>            Document hitDoc = isearcher.doc(hits[i].doc);
>            System.out.println(hitDoc.getField("id"));
>        }
>        ireader.close();
>        directory.close();
>        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 = FSDirectory.open(index);
>
>        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
>        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
>        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()) {
>                text.append(l.getWikiText())
>                        .append((' '));
>            }
>            if(text.length() > 0) {
>                doc.add(new StringField("text", text.toString(),
> Field.Store.YES));
>                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(),
> Field.Store.NO));
>            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(),
> Field.Store.NO));
>                LOG.log(Level.INFO, "  tags={0}", sb);
>            }
>            iwriter.addDocument(doc);
>        }
>
>        db.close();
>        iwriter.close();
>
>        return "search";
>    }
>
>    private void toIndex(StringBuilder sb, LabelGroup lg) {
>        for(Label l : lg.getLabels()) {
>            sb.append(l.getText());
>            sb.append(" ");
>        }
>    }
>
>
> --
> Ramon Casha
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Help needed: search is returning no results

Posted by Jack Krupansky <ja...@basetechnology.com>.
Maybe you wanted "text" fields that are analyzed and tokenized, as opposed 
to string fields which are not analyzed and stored and queried exactly 
as-is.

See:
http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/document/TextField.html

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
To: java-user@lucene.apache.org
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 = FSDirectory.open(index);

        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher isearcher = new IndexSearcher(ireader);

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

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

        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
            System.out.println(hitDoc.getField("id"));
        }
        ireader.close();
        directory.close();
        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 = FSDirectory.open(index);

        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        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()) {
                text.append(l.getWikiText())
                        .append((' '));
            }
            if(text.length() > 0) {
                doc.add(new StringField("text", text.toString(),
Field.Store.YES));
                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(),
Field.Store.NO));
            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(), 
Field.Store.NO));
                LOG.log(Level.INFO, "  tags={0}", sb);
            }
            iwriter.addDocument(doc);
        }

        db.close();
        iwriter.close();

        return "search";
    }

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


--
Ramon Casha

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org