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 <tks...@gmail.com> 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-unsubscr...@lucene.apache.org > For additional commands, e-mail: java-user-h...@lucene.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org