FAQ
Hi,

I am creating an index successfully and the field called "note" that I am
searching against has the term "background". But when I run the query it
returns some completely unrelated term for the first hit. Here is the code:

<code>
List<EmployeeNote> employeeNotes =
employeeDAO.getAllEmployeeNotes(clientId);

//indexDir is the directory that hosts Lucene's index files
File indexDir = new File("C:\\luceneIndex");
Analyzer luceneAnalyzer = new
StandardAnalyzer(Version.LUCENE_CURRENT);
IndexWriter indexWriter = new
IndexWriter(indexDir,luceneAnalyzer,true,MaxFieldLength.UNLIMITED);
Query query = null;
Document doc = new Document();
IndexSearcher searcher = null;
TopDocs hits = null;

try {

for(EmployeeNote employeeNote : employeeNotes){

doc.add(new Field("id", employeeNote.getId().toString(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("note", employeeNote.getNote(),
Field.Store.YES, Field.Index.ANALYZED));

indexWriter.addDocument(doc);
}

indexWriter.optimize();
indexWriter.close();

IndexReader reader =
IndexReader.open(FSDirectory.open(indexDir), true); // only searching, so
read-only=true

searcher = new IndexSearcher(reader);

QueryParser qp = new QueryParser("note", luceneAnalyzer);
query = qp.parse("note:(+background)");
hits = searcher.search(query, 50);

Document docx = searcher.doc(hits.scoreDocs[0].doc); //test to see what
first hit is
String doctitle = docx.get("note");
System.out.println("Here is the note: " + doctitle);
</code>
--
View this message in context: http://old.nabble.com/Result-of-query-not-what-I-expect-tp26958815p26958815.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


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

Search Discussions

  • Ian Lea at Dec 29, 2009 at 9:23 pm
    Hi


    You need to move the Document doc = new Document(); line to inside
    your for loop. The code as it stands is adding data to a single
    document instance that is being added to the index for each note, with
    ever increasing amounts of data.

    Or look at reusing Field instances with setValue() calls. That would
    be more efficient but is a bit more complex - for your simple program
    probably best to create a new Document for each note.

    Tip: Luke is invaluable for seeing what an index holds, and a lot more.


    --
    Ian.

    On Tue, Dec 29, 2009 at 7:28 PM, lucene-newbie123 wrote:

    Hi,

    I am creating an index successfully and the field called "note" that I am
    searching against has the term "background". But when I run the query it
    returns some completely unrelated term for the first hit. Here is the code:

    <code>
    List<EmployeeNote> employeeNotes =
    employeeDAO.getAllEmployeeNotes(clientId);

    //indexDir is the directory that hosts Lucene's index files
    File   indexDir = new File("C:\\luceneIndex");
    Analyzer luceneAnalyzer = new
    StandardAnalyzer(Version.LUCENE_CURRENT);
    IndexWriter indexWriter = new
    IndexWriter(indexDir,luceneAnalyzer,true,MaxFieldLength.UNLIMITED);
    Query query = null;
    Document doc = new Document();
    IndexSearcher searcher = null;
    TopDocs hits = null;

    try {

    for(EmployeeNote employeeNote : employeeNotes){

    doc.add(new Field("id", employeeNote.getId().toString(),
    Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("note", employeeNote.getNote(),
    Field.Store.YES, Field.Index.ANALYZED));

    indexWriter.addDocument(doc);
    }

    indexWriter.optimize();
    indexWriter.close();

    IndexReader reader =
    IndexReader.open(FSDirectory.open(indexDir), true); // only searching, so
    read-only=true

    searcher = new IndexSearcher(reader);

    QueryParser qp = new QueryParser("note", luceneAnalyzer);
    query = qp.parse("note:(+background)");
    hits = searcher.search(query, 50);

    Document docx = searcher.doc(hits.scoreDocs[0].doc); //test to see what
    first hit is
    String doctitle = docx.get("note");
    System.out.println("Here is the note: " + doctitle);
    </code>
    --
    View this message in context: http://old.nabble.com/Result-of-query-not-what-I-expect-tp26958815p26958815.html
    Sent from the Lucene - Java Users mailing list archive at Nabble.com.


    ---------------------------------------------------------------------
    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
  • Uwe Schindler at Dec 29, 2009 at 9:31 pm
    Alternatively use doc.getFields().clear() on each iteration.

    -----
    Uwe Schindler
    H.-H.-Meier-Allee 63, D-28213 Bremen
    http://www.thetaphi.de
    eMail: uwe@thetaphi.de
    -----Original Message-----
    From: Ian Lea
    Sent: Tuesday, December 29, 2009 10:18 PM
    To: java-user@lucene.apache.org
    Subject: Re: Result of query not what I expect

    Hi


    You need to move the Document doc = new Document(); line to inside
    your for loop. The code as it stands is adding data to a single
    document instance that is being added to the index for each note, with
    ever increasing amounts of data.

    Or look at reusing Field instances with setValue() calls. That would
    be more efficient but is a bit more complex - for your simple program
    probably best to create a new Document for each note.

    Tip: Luke is invaluable for seeing what an index holds, and a lot more.


    --
    Ian.

    On Tue, Dec 29, 2009 at 7:28 PM, lucene-newbie123 wrote:

    Hi,

    I am creating an index successfully and the field called "note" that I am
    searching against has the term "background". But when I run the query it
    returns some completely unrelated term for the first hit. Here is the code:
    <code>
    List<EmployeeNote> employeeNotes =
    employeeDAO.getAllEmployeeNotes(clientId);

    //indexDir is the directory that hosts Lucene's index files
    File   indexDir = new File("C:\\luceneIndex");
    Analyzer luceneAnalyzer = new
    StandardAnalyzer(Version.LUCENE_CURRENT);
    IndexWriter indexWriter = new
    IndexWriter(indexDir,luceneAnalyzer,true,MaxFieldLength.UNLIMITED);
    Query query = null;
    Document doc = new Document();
    IndexSearcher searcher = null;
    TopDocs hits = null;

    try {

    for(EmployeeNote employeeNote : employeeNotes){

    doc.add(new Field("id", employeeNote.getId().toString(),
    Field.Store.YES,
    Field.Index.NOT_ANALYZED));
    doc.add(new Field("note", employeeNote.getNote(),
    Field.Store.YES, Field.Index.ANALYZED));

    indexWriter.addDocument(doc);
    }

    indexWriter.optimize();
    indexWriter.close();

    IndexReader reader =
    IndexReader.open(FSDirectory.open(indexDir), true); // only searching, so
    read-only=true

    searcher = new IndexSearcher(reader);

    QueryParser qp = new QueryParser("note", luceneAnalyzer);
    query = qp.parse("note:(+background)");
    hits = searcher.search(query, 50);

    Document docx =
    searcher.doc(hits.scoreDocs[0].doc); //test to see what
    first hit is
    String doctitle = docx.get("note");
    System.out.println("Here is the note: " + doctitle);
    </code>
    --
    View this message in context: http://old.nabble.com/Result-of-query-not-
    what-I-expect-tp26958815p26958815.html
    Sent from the Lucene - Java Users mailing list archive at Nabble.com.


    ---------------------------------------------------------------------
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupjava-user @
categorieslucene
postedDec 29, '09 at 7:29p
activeDec 29, '09 at 9:31p
posts3
users3
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase