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 Andrew Norman <ki...@gmail.com> on 2014/06/16 14:43:15 UTC

Index Not Finding Results some times

Hi,

I am using Lucene 3.6.2 (I cannot upgrade due to 3rd party dependencies).

I have written the following code below to illustrate the problem. I create
a single document, add three fields, put it into the index. When I attempt
to find the document using exact matches I can find the document 2 out of 3
times. I have read the documentation and google'd but I am currently
drawing a blank as to where my mistake is. Any pointers would be gratefully
received.

Regards
Kiwicoder


public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException {
        RAMDirectory directory = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36));
        IndexWriter writer = new IndexWriter(directory, config);

        Document doc = new Document();

        doc.add(new Field("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db",
Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("type", "data", Field.Store.NO,
Field.Index.ANALYZED));
        doc.add(new Field("name", "Name1", Field.Store.NO,
Field.Index.ANALYZED));

        writer.addDocument(doc);

        writer.close();

        // -------------------------------------------
        IndexReader reader = IndexReader.open(directory);

        IndexSearcher search = new IndexSearcher(reader);
        System.out.println("ID:   " + search.search(new TermQuery(new
Term("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db")), 100).totalHits); // 1
document found
        System.out.println("TYPE: " + search.search(new TermQuery(new
Term("type", "data")), 100).totalHits); // 1 document found
        System.out.println("NAME: " + search.search(new TermQuery(new
Term("name", "Name1")), 100).totalHits); // 0 document found
        search.close();
    }

Re: Index Not Finding Results some times

Posted by Andrew Norman <ki...@gmail.com>.
Ah, that now makes sense.

Changed the code and now it works.

Thanks for the help.

On Monday, 16 June 2014, Allison, Timothy B. <ta...@mitre.org> wrote:

> The problem is that you are using an analyzer at index time but then not
> at search time.
>
> StandardAnalyzer will convert "Name1" to "name1" at index time.
>
> At search time, because you aren't using a query parser (which would by
> default lowercase your terms) you are literally searching for "Name1" which
> doesn't exist in your index.
>
> -----Original Message-----
> From: Andrew Norman [mailto:kiwicoder@gmail.com <javascript:;>]
> Sent: Monday, June 16, 2014 8:43 AM
> To: java-user@lucene.apache.org <javascript:;>
> Subject: Index Not Finding Results some times
>
> Hi,
>
> I am using Lucene 3.6.2 (I cannot upgrade due to 3rd party dependencies).
>
> I have written the following code below to illustrate the problem. I create
> a single document, add three fields, put it into the index. When I attempt
> to find the document using exact matches I can find the document 2 out of 3
> times. I have read the documentation and google'd but I am currently
> drawing a blank as to where my mistake is. Any pointers would be gratefully
> received.
>
> Regards
> Kiwicoder
>
>
> public static void main(String[] args) throws CorruptIndexException,
> LockObtainFailedException, IOException {
>         RAMDirectory directory = new RAMDirectory();
>
>         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
> new StandardAnalyzer(Version.LUCENE_36));
>         IndexWriter writer = new IndexWriter(directory, config);
>
>         Document doc = new Document();
>
>         doc.add(new Field("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db",
> Field.Store.YES, Field.Index.NOT_ANALYZED));
>         doc.add(new Field("type", "data", Field.Store.NO,
> Field.Index.ANALYZED));
>         doc.add(new Field("name", "Name1", Field.Store.NO,
> Field.Index.ANALYZED));
>
>         writer.addDocument(doc);
>
>         writer.close();
>
>         // -------------------------------------------
>         IndexReader reader = IndexReader.open(directory);
>
>         IndexSearcher search = new IndexSearcher(reader);
>         System.out.println("ID:   " + search.search(new TermQuery(new
> Term("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db")), 100).totalHits); // 1
> document found
>         System.out.println("TYPE: " + search.search(new TermQuery(new
> Term("type", "data")), 100).totalHits); // 1 document found
>         System.out.println("NAME: " + search.search(new TermQuery(new
> Term("name", "Name1")), 100).totalHits); // 0 document found
>         search.close();
>     }
>

RE: Index Not Finding Results some times

Posted by "Allison, Timothy B." <ta...@mitre.org>.
The problem is that you are using an analyzer at index time but then not at search time.

StandardAnalyzer will convert "Name1" to "name1" at index time.

At search time, because you aren't using a query parser (which would by default lowercase your terms) you are literally searching for "Name1" which doesn't exist in your index.

-----Original Message-----
From: Andrew Norman [mailto:kiwicoder@gmail.com] 
Sent: Monday, June 16, 2014 8:43 AM
To: java-user@lucene.apache.org
Subject: Index Not Finding Results some times

Hi,

I am using Lucene 3.6.2 (I cannot upgrade due to 3rd party dependencies).

I have written the following code below to illustrate the problem. I create
a single document, add three fields, put it into the index. When I attempt
to find the document using exact matches I can find the document 2 out of 3
times. I have read the documentation and google'd but I am currently
drawing a blank as to where my mistake is. Any pointers would be gratefully
received.

Regards
Kiwicoder


public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException {
        RAMDirectory directory = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36));
        IndexWriter writer = new IndexWriter(directory, config);

        Document doc = new Document();

        doc.add(new Field("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db",
Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("type", "data", Field.Store.NO,
Field.Index.ANALYZED));
        doc.add(new Field("name", "Name1", Field.Store.NO,
Field.Index.ANALYZED));

        writer.addDocument(doc);

        writer.close();

        // -------------------------------------------
        IndexReader reader = IndexReader.open(directory);

        IndexSearcher search = new IndexSearcher(reader);
        System.out.println("ID:   " + search.search(new TermQuery(new
Term("id", "c71fa7f8-32c2-4cb3-97d8-4728322079db")), 100).totalHits); // 1
document found
        System.out.println("TYPE: " + search.search(new TermQuery(new
Term("type", "data")), 100).totalHits); // 1 document found
        System.out.println("NAME: " + search.search(new TermQuery(new
Term("name", "Name1")), 100).totalHits); // 0 document found
        search.close();
    }